• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 95
  • Last Modified:

.NET Convert Console Application to referance Project

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
Jamil Muammar
Asked:
Jamil Muammar
  • 2
1 Solution
 
Ryan ChongCommented:
do you mean to create a class library and use it in your 2nd web application?
0
 
Jamil MuammarAuthor Commented:
yes
0
 
ste5anSenior DeveloperCommented:
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
 
Ryan ChongCommented:
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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now