Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple exe files in one solution, all referencing a single public class

I have a c# console project (vs2012) that is getting big. Currently I am calling multiple 'methods' one after the other. As I add more methods I would like to be able to break them out into multiple executable files, to be ran as scheduled apps, but still be able to reference (and change using tortoise SVN) a public class of rules of which they all will reference. Do I have to create a dll to accomplish this or is there a simpler way? Sorry, I'm new to all of this. I have found similar questions but I either did not understand them or they weren't quite what I was looking for. Thanks!

like image 666
Milne Avatar asked Jan 14 '23 17:01

Milne


2 Answers

Yes you need to build a DLL and reference it from whatever project needs the code stored there.
It is just a matter to add a new Library project to your solution, create a public class and insert the methods that should be called from the other projects

Click the Menu File and choose Add Project enter image description here

Then go to your Solution Explorer Window, click the Console Application and right click on references
enter image description here

and select from the Solution folder the Project of your class library enter image description here

Now go to the predefined Class1 and add the following code

namespace ConsoleApplication1
{
    // Class that contains methods and data to be used/shared by various applications
    public class Class1
    {
        public Class1()
        { }

        public void Show(string message)
        {
            Console.WriteLine(message);
        }
    }
}

and in your ConsoleApplication1 add the following code

namespace ConsoleApplication1
{
    class Program
    {
        // A sample console application that uses the library project....
        static void Main(string[] args)
        {
            Class1 c=new Class1();
            c.Show("Hello World");
        }
    }
}
like image 193
Steve Avatar answered Jan 16 '23 07:01

Steve


Do I have to create a dll to accomplish this or is there a simpler way?

If you want to achieve shared logic and modularity then the answer is no, no simpler way and you should create assembly (DLL) using the Class Library Project template in VS, write your public classes, build it and reference it from your EXE's projects.

I would recommend you to have a solution with the Executables and the Class Library project together so you'll be able to enjoy maintainability and good synchronization with your Class Library by referencing it by Project and not by Assembly.

like image 29
Yair Nevet Avatar answered Jan 16 '23 07:01

Yair Nevet