开发者

Shared method in VB.NET cannot handle this why?

开发者 https://www.devze.com 2022-12-09 09:38 出处:网络
(I tried with this question but this code isolates the problem better.) I have this code: Public Shared Sub PopulateTextFields(ByRef stuffList As List(Of Stuff))

(I tried with this question but this code isolates the problem better.)

I have this code:

Public Shared Sub PopulateTextFields(ByRef stuffList As List(Of Stuff))
    Dim aStuff As New Stuff
    For Each aStuff In stuffList
        DoStuff(aStuff)
    Next
End Sub

Private Sub DoStuff(ByRef theStuff as Stuff)
    ....
End Sub

I get the following error highlighting DoStuff(aStuff):

Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.

Didn't I get an explicit instance of Stuff when I wrote the Dim statement?

Don't understand wha开发者_JS百科t's wrong. Thanks in advance!


I think the problem lies with the Subroutine DoStuff. If both your subs lie in the same class, you are trying to refer to DoStuff from within PopulateTextFields, which is a shared sub.

In order to achieve this, you need to declare DoStuff as Shared as well.


Yes you did, but you aren't referencing aStuff you are trying to call it on the static implementation of the class, furthermore you are resetting aStuff to a separate instance through each loop iteration.. change your code to:

Public Shared Sub PopulateTextFields(ByRef stuffList As List(Of Stuff))
    Dim aStuff As New Stuff
    For Each aStuff In stuffList
        aStuff.DoStuff(aStuff)
    Next
End Sub

Private Sub DoStuff(ByRef theStuff as Stuff)
    ....
End Sub

And it should work, but maybe not as expected, I don't really know your intent of having a private member that handles changing a separate reference of it's own type.

It may be appropriate to change the signature of DoStuff to:

Private Sub DoStuff()
    ....
    'Use the Me reference here to change myself
    ....
End Sub

and then call it as:

aStuff.DoStuff() 'Will modify this instance


You didn't share which type these methods belong to. From your confusion, I'm guessing it's part of the "Stuff" class. But it doesn't really matter. It sounds like you're forgetting one of two things:

  • Creating an instance of the type in the shared method doesn't somehow attach the shared method to that instance. You could create 10 or 1000 instances in the method, after all.

  • Passing an instance as a parameter doesn't associate the function with an instance. A parameter is not a call site.

Either way, it comes down to providing an instanced call site. Your DoStuff function is not shared, and so the compiler thinks it needs access to state provided by a specific instance of your type. That instance is the method's call site. You either need an instance of the type to call it from: SomeInstance.DoStuff(aStuff) , or if the method doesn't really need access to any type state you need to mark it shared and call it like this: Stuff.DoStuff(aStuff)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号