locked
Stuck. How to compile a class.cs to a dll and use it in another project? RRS feed

  • Question

  • User-1128377612 posted

    Hi all,

    I am using vs2013  update 4

    I'm having some difficulties and thinking my approach is wrong from the get go.
    I have made a "business logic" class in C# console app thinking ahead that I could compile this class to a dll an then
    in an MVC project (or any other .Net project for that matter), just add a reference to that dll and a using statement and 
    the class and it's members would be exposed for me to use.

    What I tried:
    Created a class library project > added the class I made and tweaked in the console app > compiled it to a dll.
    I then Added reference to it in an existing MVC project, added a using statement same name as the class which is also the same name as the dll. Rebalance.dll.
    I had no luck. I got the msg (are you missing a directive or assembly reference).

    I googled and tried every approach I could find on the web, from using an Imports statement to Unsafe code, using an [attribute] line of code to tell VS I want to use this dll. I tried Unmanaged , interop, and unsafe unmanaged code examples to no avail. Then it hit me that maybe my overall approach was wrong from the beginning. I tried all kinds of approaches. even ILMerge to merge the dll with an exe in a console app just to test that(which defeats the purpose anyway of using an independent dll.) It was just a stab at it. 

    I know this cannot be that hard. 
    So please correct me in my initial thinking.
    I thought I would just write and test the class in a console app. compile it to a dll. Then add a reference to it in another project with a using statement.
    And voowallaa. I could go:


    using...;
    using NewBalance;

    namespace FOOBAR
    {

    class Test

    {

    Rebalance nb = new ReBalance();

    int RemainingDays = nb.GetRemainingDays(ReceivedDate,ReplenishDate);

    };
    };


    But all I got was the msg (are you missing a directive or assembly reference).

    How do I need to do this?

    Thanks in advance,
    Adam

    Monday, December 1, 2014 6:27 PM

Answers

  • User1751268424 posted

    Hi,

    I think the problem you create a "console application" template instead of "class library" template.

    Here I tested:

    1. Create a "Class Library" named "DemoClassLib" and "NewBalance" Class on it. Here the class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DemoClassLib
    {
        public class NewBalance
        {
            public NewBalance()
            {
                PreviousBalance = 10.00;
                Transaction = 2.34;
                Balance = PreviousBalance + Transaction;
            }
            public double PreviousBalance { get; set; }
            public double Transaction { get; set; }
            public double Balance { get; set; }
        }
    }
    

    2. On other project I "Add Reference", I add solution "DemoClassLib" as a reference, then add "Using DemoClassLib;" on other project, here the code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using DemoClassLib;
    
    namespace GettingStartedWithMVC.Controllers
    {
        public class RoutesDemoController : Controller
        {
            public ActionResult One()
            {
                return View();
            }
    
            public ActionResult Two(int donuts = 1)
            {
                NewBalance nb = new NewBalance();
                ViewBag.NewBalance = nb.Balance;
                donuts = Convert.ToInt32(nb.Balance);
                ViewBag.Donuts = donuts;
                return View();
            }
    
            //[Authorize]
            public ActionResult Three()
            {
                return View();
            }
    
            public ActionResult Four()
            {
                return View();
            }
        }
    }

    3. My view:

    <h2>This is route Dua</h2>
    <h2>New Balance: @ViewBag.NewBalance - Donuts: @ViewBag.Donuts </h2>
    @for (var i = 0; i < ViewBag.Donuts; i++)
    {
        <p>Mmm, donuts @i...</p>
    }

    The result is this (copy paste from browser):

    This is route Dua
    
    New Balance: 12.34 - Donuts: 12
    
    Mmm, donuts 0...
    
    Mmm, donuts 1...
    
    Mmm, donuts 2...
    
    Mmm, donuts 3...
    
    Mmm, donuts 4...
    
    Mmm, donuts 5...
    
    Mmm, donuts 6...
    
    Mmm, donuts 7...
    
    Mmm, donuts 8...
    
    Mmm, donuts 9...
    
    Mmm, donuts 10...
    
    Mmm, donuts 11...

    Hope can help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 1, 2014 7:17 PM
  • User-434868552 posted

    @Rousseau10    welcome to forums.asp.net

    the process that i think you want to use is straight forward and relatively simple as i hope this simplified example with show.

    Step 1:  i open visual studio and create a c# class library:

    namespace Rousseau10
    {
        public class DateArithmetic
        {
            public static System.DateTime Tomorrow()
            {
                return System.DateTime.Now.AddDays(1.0);
            }
        }
    }

    This compiles to Rousseau10.dll ; i get the full path to this dll; alternatively, i can copy/move the .dll to a special folder, for example: c:\myDLLs

    Note:  there is no using System; because i've explicitly typed System.DateTime.

    Now i want to consume my Tomorrow() method.  For the purpose of this simplified example, i'll code a Console program.

    Note:  after i create an empty console program and before i write code for it, in Visual Studio Solution Explorer, i right-click References and using the path to my .dll, i add a reference to Rosseau10.dll.

    Now all i need do it this:

    namespace Rousseau10demo
    {
        class Program
        {
            static void Main(string[] args)
            {
                System.Console.WriteLine(Rousseau10.DateArithmetic.Tomorrow());
                System.Console.ReadLine();
            }
        }
    }

    Note how i reference my Tomorrow() method.

    alternatively, i can add a using Rousseau10 statement and modify my code like this:

    using Rousseau10;
    namespace Rousseau10demo
    {
        class Program
        {
            static void Main(string[] args)
            {
                System.Console.WriteLine(DateArithmetic.Tomorrow());
                System.Console.ReadLine();
            }
        }
    }

    if, instead of writing a console program, i wanted to use my Tomorrow() method some other way, for instance, in an ASP.NET MVC program, the principle is the same, i.e., code that wants to use a .dll that is compiled separately needs to add a reference that points to that .dll.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 1, 2014 7:35 PM

All replies

  • User1751268424 posted

    Hi,

    I think the problem you create a "console application" template instead of "class library" template.

    Here I tested:

    1. Create a "Class Library" named "DemoClassLib" and "NewBalance" Class on it. Here the class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DemoClassLib
    {
        public class NewBalance
        {
            public NewBalance()
            {
                PreviousBalance = 10.00;
                Transaction = 2.34;
                Balance = PreviousBalance + Transaction;
            }
            public double PreviousBalance { get; set; }
            public double Transaction { get; set; }
            public double Balance { get; set; }
        }
    }
    

    2. On other project I "Add Reference", I add solution "DemoClassLib" as a reference, then add "Using DemoClassLib;" on other project, here the code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using DemoClassLib;
    
    namespace GettingStartedWithMVC.Controllers
    {
        public class RoutesDemoController : Controller
        {
            public ActionResult One()
            {
                return View();
            }
    
            public ActionResult Two(int donuts = 1)
            {
                NewBalance nb = new NewBalance();
                ViewBag.NewBalance = nb.Balance;
                donuts = Convert.ToInt32(nb.Balance);
                ViewBag.Donuts = donuts;
                return View();
            }
    
            //[Authorize]
            public ActionResult Three()
            {
                return View();
            }
    
            public ActionResult Four()
            {
                return View();
            }
        }
    }

    3. My view:

    <h2>This is route Dua</h2>
    <h2>New Balance: @ViewBag.NewBalance - Donuts: @ViewBag.Donuts </h2>
    @for (var i = 0; i < ViewBag.Donuts; i++)
    {
        <p>Mmm, donuts @i...</p>
    }

    The result is this (copy paste from browser):

    This is route Dua
    
    New Balance: 12.34 - Donuts: 12
    
    Mmm, donuts 0...
    
    Mmm, donuts 1...
    
    Mmm, donuts 2...
    
    Mmm, donuts 3...
    
    Mmm, donuts 4...
    
    Mmm, donuts 5...
    
    Mmm, donuts 6...
    
    Mmm, donuts 7...
    
    Mmm, donuts 8...
    
    Mmm, donuts 9...
    
    Mmm, donuts 10...
    
    Mmm, donuts 11...

    Hope can help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 1, 2014 7:17 PM
  • User-434868552 posted

    @Rousseau10    welcome to forums.asp.net

    the process that i think you want to use is straight forward and relatively simple as i hope this simplified example with show.

    Step 1:  i open visual studio and create a c# class library:

    namespace Rousseau10
    {
        public class DateArithmetic
        {
            public static System.DateTime Tomorrow()
            {
                return System.DateTime.Now.AddDays(1.0);
            }
        }
    }

    This compiles to Rousseau10.dll ; i get the full path to this dll; alternatively, i can copy/move the .dll to a special folder, for example: c:\myDLLs

    Note:  there is no using System; because i've explicitly typed System.DateTime.

    Now i want to consume my Tomorrow() method.  For the purpose of this simplified example, i'll code a Console program.

    Note:  after i create an empty console program and before i write code for it, in Visual Studio Solution Explorer, i right-click References and using the path to my .dll, i add a reference to Rosseau10.dll.

    Now all i need do it this:

    namespace Rousseau10demo
    {
        class Program
        {
            static void Main(string[] args)
            {
                System.Console.WriteLine(Rousseau10.DateArithmetic.Tomorrow());
                System.Console.ReadLine();
            }
        }
    }

    Note how i reference my Tomorrow() method.

    alternatively, i can add a using Rousseau10 statement and modify my code like this:

    using Rousseau10;
    namespace Rousseau10demo
    {
        class Program
        {
            static void Main(string[] args)
            {
                System.Console.WriteLine(DateArithmetic.Tomorrow());
                System.Console.ReadLine();
            }
        }
    }

    if, instead of writing a console program, i wanted to use my Tomorrow() method some other way, for instance, in an ASP.NET MVC program, the principle is the same, i.e., code that wants to use a .dll that is compiled separately needs to add a reference that points to that .dll.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 1, 2014 7:35 PM
  • User-1128377612 posted

    Thanks so much guys.

    After looking closely at your code, I realized I forgot to put the access specifier of "public" before my class so it defaulted to "private" and therefore inaccessible to the other project.

    I had

    class NewBalance{ 

    instead of

    public class NewBalance {

    devils in the details !

    Thanks so much .

    Adam

    Thursday, December 4, 2014 11:00 PM