How to define the Main method in a class or module?

I just practiced some encoding and noticed that I was able to use the class instead of the module in VB.NET. So I replaced my module with a class, and I got this error message:

In 'practicevb'.practicevb

no accessible "Main" method with matching signature found.

I made sure that the startup object was set correctly in Properties > Application > Startup Objects .

The error message will disappear if I return it back to the module, but I would like to save it in the class, since I changed the other parts of my code to the class and did not return error messages either.

 Class Atic Sub Main() Console.WriteLine("Hello, this proram will calcaulate the quadratic forumla ax^2 + bx + c") Dim Quads As New Quads Quads.Calc() Console.ReadKey() End Sub End Class 
+6
source share
5 answers

I assume your application is a command line application. Make the class public and shared ...

 Public Shared Sub Main() End Sub 
+12
source

If you are NEW for classes

Classes are a different concept than module is a set of functions, but class is a template that must instantiate an object and use it.

First go to the basics of OOP in VB.NET here

If you are pro

Use Shared Sub Main() ...

+2
source

Modules are simply classes in which all participants are separated (static in C #).

If you want to change a module into a class, just add the Shared modifier to its members:

 Shared Sub Main() ... 

Although, I really think that modules are a good idea and an ideal place to host your Main function.

0
source

The main method is needed as an entry point for your applications. I need to be programmed without creating an instance of the object, as there is no way to do this before starting your program.

Either just make this method and a static class, as he said, or it is better to have a module only for this entry point, then create an instance of the object and go from there.

0
source

For any future reader, if your Main() is in the Module (and not in the class module) and you still get this error, make sure that the method does not accept any parameters. Unlike C ++, VB.NET does not accept command line arguments as the main parameters of the method. Instead, you should define a Main () method with a null parameter and use My.Application.CommandLineArgs to access the provided parameters. I hit my head for some time before I realized this.

0
source

Source: https://habr.com/ru/post/892596/


All Articles