How do I correctly reference to code in a different project in C#?

1

In short: I have a console application with a static Dictionaries, which work fine in the project they were written in. How do I reference to this project correctly, so that I can do the same from a different project? I get an error every time I try to call the function to load the data into them.

Long story: For a school project I'm making a scheduler for a garbage collection company using local search algorithms. I have everything in a console app (project name: LocalSearch), but I wanted to make a visualiser, so I can better analyse the solutions it produces. I am using a Windows Forms Application (project name: ScheduleVisualiser) for this purpose and I wanted to reference to the console app so I don't have to load the data again.

In the console app I have all my data loading done in the Program class. I have the following code:

public static Dictionary<int, Order> Orders = new Dictionary<int, Order>();

public static void Main()
{
    LoadData();
    ...
}

public static void LoadData()
{
    ...
}

Now I have my WFA and I have added console app as a reference, but when I do LocalSearch.Program.LoadData(), Visual Studio goes into break mode and it throws the following expection:

System.IO.FileNotFoundException HResult=0x80070002 Message=Could not load file or assembly 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. Source= StackTrace:

What am I doing wrong?

c#
code-structure
asked on Stack Overflow Jul 18, 2020 by Alex Janse

2 Answers

3

You cannot use the Console application as a Library. You either have to convert it to a library type project or run as a ".exe" with proper parameters.

So technically there are 3 options:

  1. If you don't need a Console app anymore - simply change the Project type to Class Library:

Right-click Project -> Properties -> Application -> Output Type: 'Class Library'

  1. If you still need your Console app and want to share the same logic between the Console and WinForms - Move all your logic to the Class Library and use both in Console and WinForms.

  2. If you need just input/output data from the Console Application - you can run it as an external application (but it is the worst scenario)

        using (Process process = new Process())
        {
            process.StartInfo.FileName = "ipconfig.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.Start();
    
            // Synchronously read the standard output of the spawned process.
            StreamReader reader = process.StandardOutput;
            string output = reader.ReadToEnd();
    
            // Write the redirected output to this application's window.
            Console.WriteLine(output);
    
            process.WaitForExit();
        }
    
answered on Stack Overflow Jul 18, 2020 by Boris Sokolov • edited Jul 18, 2020 by Boris Sokolov
2

Ciao, in this case I have always created a Class Library Project. Then, in a Console Application I add the reference to the Class Library. Try this way and check if exception disappears.

answered on Stack Overflow Jul 18, 2020 by Giovanni Esposito

User contributions licensed under CC BY-SA 3.0