Solved

.NET Convert Console Application to referance Project

Posted on 2016-09-26
4
68 Views
Last Modified: 2016-09-26
Hi Experts,
 I have a .NET soluation contain 2 project

first Console Application
second WEB application

How do I set the Console Application as refreance to the Web Application


Thanks
0
Comment
Question by:Jamil Muammar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41816039
do you mean to create a class library and use it in your 2nd web application?
0
 

Author Comment

by:Jamil Muammar
ID: 41816044
yes
0
 
LVL 33

Expert Comment

by:ste5an
ID: 41816089
Don't do this.

Cause setting a reference means code reuse.

In this case create a new project, type class library.
Set a reference in your console application and your ASP.NET application to that class library.
Then you refactor your console application and move all code, you want to share, to that class library.
And when you want good, clean code, then you also create a UnitTest project for your class library.

Adding a reference:Capture.PNG
0
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41816150
try this:
1. start your visual studio, create a New Project
2. you can select Class Library from the options there.
snap.jpg3. so you can create a simple class with the methods you want, like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    public class Class1
    {
        int myValue = 0;

        public Class1()
        {

        }
        public Class1(Int32 v)
        {
            myValue = v;
        }

        public Int32 getValue()
        {
            return myValue;
        }
    }
}

Open in new window

4. change the namespace and class name if you want to.
5. you can press F6 to build the solution
6. you should able to find something like: ClassLibrary1.dll in the bin\debug folder

next, add in another project by doing this:
7. right click the Solution, then select Add > New Project
snap1.jpg8. select a new ASP.NET Web Application project
snap2.jpg9. depend on the version of visual studio you are using, you may select the template you want.
snap3.jpg10. so now you should see 2 projects in your solution explorer.
11. under the ASP.NET web application project, find the References, right click and click Add reference
12. click the Project tab and try find your first class library, select and click Ok to proceed.
snap4.jpg13. now set your ASP.NET web application project to Startup project.
snap5.jpg14. now in your ASP.NET web application project, create a test page and enter the script below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ClassLibrary1;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Class1 myClass = new Class1(250);

            Response.Write("myClass value is " + myClass.getValue().ToString());
        }
    }
}

Open in new window

15. try to build your solution now by pressing F6
16 and it's running now!
snap6.jpghope this is clear enough
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question