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

前端 未结 3 1328
我寻月下人不归
我寻月下人不归 2020-12-21 14:25

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 i

相关标签:
3条回答
  • 2020-12-21 14:45

    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 StartGettingQuotesLevel2 subroutine is an instance method not a shared or class method, see a more detailed explanation here

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

    ' 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 QuoteMgr object instance to be created and used by your main form. In that case, you can make it a member variable of your main form and create it once.

    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 QuoteMgr class depend on other object instances for their tasks, you will have to supply these to the constructor method of the QuoteMgr class as the arguments for its constructor's method parameters. Constructors (Sub New(...)) look like this:

    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 QuoteMgr instance, you have to call its constructor method with the things it need, for example

    ' 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.
    0 讨论(0)
  • 2020-12-21 14:52

    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

    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.

    0 讨论(0)
  • 2020-12-21 14:54

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

    0 讨论(0)
提交回复
热议问题