S
S
splunk2016-02-28 02:18:15
C++ / C#
splunk, 2016-02-28 02:18:15

C#. Why can't you refer to a class in another project if that project doesn't have a Main() method?

Created solutions. I added 2 projects to it - A and B. For A, I indicated a link to B.
Project A:

class ClassStart
    {
        static void Main(string[] args)
        {
            Class2.Run();
        }
    }

Project B:
class Class1
    {
       static void Main(string[] args)  {  }
    }


    public class Class2
    {
        public static void Run()
        {
            System.Console.WriteLine("hello");
            System.Console.ReadLine();
        }
    }

If you remove the Main () method from the Class1 class, then an error occurs:
Program "..." does not contain a static method "Main" suitable for entry point B

Explain to the newbie why this is happening? Why does the other project need an entry point when I am accessing another Class2 class? In this case, Class1 does not interact with Class2.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Kulakov, 2016-03-03
@splunk

Look, I see two situations
1) Project A is an application, that is, it has an EXE extension. Project B is a plug-in library, that is, a dll.
2) Project A and Project B are both applications, that is, they have the EXE extension. (I'm assuming you are).
In order for the OS to start the application, it must find a MAIN method with a certain signature in it. This is a common entry point, so to speak, and therefore in the second case, both projects must have this method.
Let me give you a little extra. I will send you information about .net. The fact is that in .NET both .exe and .dll files are assemblies and they have an almost identical structure, the only difference is that a valid .exe assembly must have this same MAIN method.
I might blow your mind right now, but an application project can use references to both .dll and .exe .net assemblies, and I'm pretty sure I can refer the .exe assembly to the project's .dll assembly. such things
are usually in a solution (that is, in a solution) there is a project of a certain application that communicates with the user and there are library projects where some common code is taken out. but there are also solutions where all projects are library projects and there are no applications at all.
you can easily change the type of one of the projects to a class library and you will be happy.

R
Roman, 2016-02-28
@yarosroman

Because you have a startup project that compiles to EXE, create a second project of the Class Library type and you will be happy (it will compile to a DLL).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question