对非共享成员的引用需要 VB.net 中的对象引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30338944/
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-17 19:08:34  来源:igfitidea点击:

Reference to a non-shared member requires an object reference in VB.net

vb.net

提问by Alice Meade

I have a VB.net program that I got from someone else. It is comprised of a main form and 6 other modules (all .vb files). These files all have a "VB" icon next to them in the Explorer pane. I am trying to make a call to a sub-routine in one of the modules from the main form. My line of code is:

我有一个从别人那里得到的 VB.net 程序。它由一个主窗体和 6 个其他模块(所有 .vb 文件)组成。这些文件在资源管理器窗格中的旁边都有一个“VB”图标。我正在尝试从主窗体调用其中一个模块中的子例程。我的代码行是:

QuoteMgr.StartGettingQuotesLevel2(sSym)

where QuoteMgr is the name of the module and StartGettingQuotesLevel2(sSym) is the name of the sub-routine. When I enter this, I get the error message:

其中 QuoteMgr 是模块的名称,StartGettingQuotesLevel2(sSym) 是子例程的名称。当我输入这个时,我收到错误消息:

Reference to a non-shared member requires an object reference.  

The sub-routine is defined in the QuoteMgr Module as follows:

该子程序在 QuoteMgr 模块中定义如下:

 Public Sub StartGettingQuotesLevel2(ByVal oSymbol As String)

What is strange is when I enter:

奇怪的是当我输入时:

QuoteMgr.

(the name of the module with a period), it does not show me all the sub-routines and functions in the module. It only shows:

(模块名称带句点),它没有显示模块中的所有子例程和函数。它只显示:

Update_Level1
Update_Level12
Update_Level2

These are Public Const in the module.

这些是模块中的公共常量。

Can you tell me what I need to do?

你能告诉我我需要做什么吗?

回答by Alex

What the compiler is trying to tell you with this error message

编译器试图通过此错误消息告诉您什么

Reference to a non-shared member requires an object reference

对非共享成员的引用需要对象引用

is that the StartGettingQuotesLevel2subroutine is an instance methodnot a sharedor class method, see a more detailed explanation here

StartGettingQuotesLevel2子程序是实例方法而不是共享或类方法,请参阅此处的更详细说明

To call an instance method, you need to have an object instanceto call it on. In your case, an object instance of the class typeQuoteMgr. Like in the example below:

要调用实例方法,您需要有一个对象实例来调用它。在您的情况下,类类型的对象实例QuoteMgr。就像下面的例子:

' create a new QuoteMgr object instance
Dim myQuoteMgr As QuoteMgr = New QuoteMgr()

' call its instance method with "abc" as its oSymbol argument.
myQuoteMgr.StartGettingQuotesLevel2("abc")

It is possible that you only want a single QuoteMgrobject instance to be created and used by your main form. In that case, you can make it a member variableof your main form and create it once.

您可能只希望QuoteMgr主窗体创建和使用单个对象实例。在这种情况下,您可以将其设为主窗体的成员变量并创建一次。

Public Partial Class MainForm

    ' Create it as a private member variable of the main form
    Private m_QuoteMgr As QuoteMgr = New QuoteMgr()

    ' Use it when "some" button is pressed
    Private Sub btnSome_Click(sender As Object, e As EventArgs) Handles btnSome.Click
        m_QuoteMgr.StartGettingQuotesLevel2(txtSymbol.Text)
        ' And possibly do something with the results.
    End Sub        

End Class

Also, if instances of your QuoteMgrclass depend on other object instances for their tasks, you will have to supply these to the constructormethod of the QuoteMgrclass as the argumentsfor its constructor's method parameters. Constructors (Sub New(...)) look like this:

另外,如果您的实例QuoteMgr类取决于他们的任务其他物体的情况下,你将不得不提供这些的构造函数的方法QuoteMgr类作为参数的构造函数的方法参数。构造函数 ( Sub New(...)) 看起来像这样:

Public Class QuoteMgr

    ' This is a constructor that takes two arguments
    ' - oMainSymbol: a string value
    ' - oKernel: an instance of the type Kernel
    Public Sub New(oMainSymbol As String, ByRef oKernel As Kernel)

        ' ....

    End Sub

End Class

That means, that when you create a QuoteMgrinstance, you have to call its constructor method with the things it need, for example

这意味着,当你创建一个QuoteMgr实例时,你必须用它需要的东西调用它的构造函数方法,例如

' There must be an instance of Kernel created somewhere. 
Dim myKernel As Kernel = ....

' create a new QuoteMgr object instance with these arguments:
' - oMainSymbol = "SYMABC"
' - oKernel = myKernel
Dim myQuoteMgr As QuoteMgr = New QuoteMgr("SYMABC", myKernel)

Some other recommendations

其他一些建议

  • The explanations I have provided, are about basic VB.NET language features (e.g. the terms highlighted in bold). I suggest that before you make any changes to the code you have, you (1) make a backup of it, and (2) try to read a tutorial and practice on something smaller.
  • The compiler is (virtually) always right. When it gives you an error message, read it carefully, it will indicate the line where something is wrong and a message that tells you what it needs or is missing.
  • It is not the purpose of Stack Overflow to provide tutorials or code. It is a Q&A site where the best questions and answers deal with specific, delineated programming problems, for which succinct answers are possible.
  • 我提供的解释是关于基本的 VB.NET 语言特性(例如,以粗体突出显示的术语)。我建议在对现有代码进行任何更改之前,您 (1) 对其进行备份,以及 (2) 尝试阅读教程并在较小的内容上进行练习。
  • 编译器(实际上)总是正确的。当它给你一条错误信息时,仔细阅读它,它会指出错误所在的行,以及一条消息,告诉你它需要什么或缺少什么。
  • Stack Overflow 的目的不是提供教程或代码。这是一个问答网站,其中最好的问题和答案处理特定的、描述的编程问题,对于这些问题可以有简洁的答案。

回答by Neonito Perez Jr

Right click your application and go to Properties. Make sure your application type is "Windows Forms Application".

右键单击您的应用程序并转到“属性”。确保您的应用程序类型是“Windows 窗体应用程序”。

回答by SSS

It means that the routine you are trying to call needs to reference an instance of the form to access the routine. You can either reference an instance as Alex says, or you can make the routine 'Shared', so it doesn't need an instance. To do this, change the definition in QuoteMgr.vb to

这意味着您尝试调用的例程需要引用表单的实例才能访问该例程。您可以像 Alex 所说的那样引用实例,也可以将例程设为“共享”,因此它不需要实例。为此,请将 QuoteMgr.vb 中的定义更改为

Friend Shared Sub StartGettingQuotesLevel2(ByVal oSymbol As String)

Switching it to `Shared' may start showing compiler errors, if the routine accesses form controls or module-level variables. These will need to be added to the parameter list.

如果例程访问表单控件或模块级变量,将其切换到“共享”可能会开始显示编译器错误。这些将需要添加到参数列表中。