vb.net 如何在类和模块中定义 Main 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6667821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:36:12  来源:igfitidea点击:

How do I define a Main method in a Class versus a Module?

vb.netclassmodule

提问by nhat

I was just practicing some coding and noticed that I was able to use class instead of Module in VB .NET. So I replaced my module to class and I got this error message:

我只是在练习一些编码,并注意到我能够在 VB .NET 中使用类而不是模块。所以我将我的模块替换为 class 并收到此错误消息:

No accessible 'Main' method with an appropriate signature was found in 'practicevb'.practicevb

在“practicevb”.practicevb 中找不到具有适当签名的可访问“Main”方法

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

我确保启动对象在Properties > Application > Startup Objects.

The error message disappears if I change it back to Module but I would like to keep it class since the other parts of my code I changed to class and didn't return and error messages.

如果我将其改回 Module ,错误消息就会消失,但我想保留它的类,因为我的代码的其他部分更改为类并且没有返回和错误消息。

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

回答by N0Alias

My guess is that your application is a command line application. Make the class Public and Shared...

我的猜测是您的应用程序是命令行应用程序。使类公开和共享...

Public Shared Sub Main()

End Sub

回答by Sudantha

If you are NEWto Classes

如果你是来的课程

Classes is a different concept than a modulemodule is a collection of functions , but classis a template which should be instantiating an object and use.

类是一个与module模块不同的概念,模块是函数的集合,而是class一个模板,应该实例化一个对象并使用。

First go though the OOP basics in VB.NET here

首先在此处了解 VB.NET 中的 OOP 基础知识

If your are a Pro

如果您是专业人士

Use Shared Sub Main()...

使用Shared Sub Main()...

回答by dotNET

For any future reader, if your Main()is in a Module(not Class Module) and you're still getting this error, make sure the method does not take any parameters. Unlike C++, VB.NET doesn't take command-line arguments as a Main method's parameters. Instead you should define a zero-parameter Main() method and use My.Application.CommandLineArgsto access the supplied parameters. I banged head for some time before I realized this.

对于任何未来的读者,如果您Main()模块(而不是类模块)中并且仍然收到此错误,请确保该方法不带任何参数。与 C++ 不同,VB.NET 不将命令行参数作为 Main 方法的参数。相反,您应该定义一个零参数 Main() 方法并用于My.Application.CommandLineArgs访问提供的参数。在我意识到这一点之前,我撞了一段时间的头。

回答by Jord?o

Modulesare just classes where all members are shared (static in C#).

模块只是共享所有成员的类(C# 中的静态)。

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

如果要将模块更改为类,只需在Shared其成员中添加修饰符:

Shared Sub Main() ...

Although, I really think modules are a good ideaand a perfect place to put your Mainfunction.

虽然,我真的认为模块是一个好主意,也是放置Main功能的理想场所。

回答by Jon Egerton

The Main method is required as the entry point of your applications. I needs to be calleable without instantiating an object as there is no way of doing this before your program starts.

Main 方法需要作为应用程序的入口点。我需要在不实例化对象的情况下可调用,因为在程序启动之前无法执行此操作。

Either just make this method and the class static as has bene said, or better have a module just for this entry point, then instantiate an object and go from there.

要么像 bene 所说的那样将这个方法和类设为静态,要么最好为这个入口点创建一个模块,然后实例化一个对象并从那里开始。

回答by bluetata

Define a Public Sub Mainprocedure for your project. Declare it as Sharedif and only if you define it inside a class.

Public Sub Main为您的项目定义一个过程。如同Shared且仅当您在类中定义它时声明它。