2

Not a duplicate of "Access of shared member, constant member, enum member or nested type through an instance"

VB.NET has a warning message

(BC42025): "Access of shared member through an instance; qualifying expression will not be evaluated" as documented

here.

I take the meaning of the phrase "Access of shared member through an instance" as: your code declared a Dog object name Fido, and then called Fido.Bark(), but in the Dog class, the Bark function is declared with the Shared keyword.

But I don't know what the second part of that message mean. I always thought it sounded like it was saying that VB would just skip over that line of code, but that's not true.

The documentation explain the phrase as:

Furthermore, if such access is part of an expression that performs other actions, such as a Function procedure that returns an instance of the shared member, Visual Basic bypasses the expression and any other actions it would otherwise perform.

But I cannot tell what that means, what is it that Visual Basic is bypassing? Can you explain it in code?

Code for context

Sub Main
    Dim dog1 As Dog = New Dog()
    dog1.Bark() ' this line has the warning
End Sub

Public Class Dog
    Public Shared Sub Bark()
    End Sub
End Class

EDIT

I understand that the compiler wants me to access static methods via the Class name, what I want to understand is what does it mean that "Visual Basic bypasses the expression and any other actions it would otherwise perform." What are some possible "other actions" that could by bypassed ?

Walter Stabosz
  • 7,447
  • 5
  • 43
  • 75

3 Answers3

3

Imagine that you have something like this:

Function GetADog() As Dog
    MessageBox.Show("Getting a dog!")
    Return New Dog()
End Function

And now, in your main function, instead of dog1.Bark(), you instead have GetADog().Bark(). The warning is telling you that GetADog() will not be called before Dog.Bark(), so you will not see the message box.

In the context of the documentation, GetADog() is the expression that is being bypassed, and the message box is one of the "other actions it would otherwise perform."

Craig
  • 2,248
  • 1
  • 19
  • 23
  • Thank you, exactly the answer I was looking for. I've seen that error many times, but never really appreciated what it meant. I suppose it's because `GetADog().Bark()` is such an odd way to code something, that I've never actually been bitten by the effect of the bypass. – Walter Stabosz Jan 18 '18 at 17:32
  • 1
    @WalterStabosz I think the more likely way that this would arise is with a property where the "get" routine has side effects. I wrote it up this way because it was a little more concise. – Craig Jan 18 '18 at 17:36
2

Basically Shared is a static method. It really doesn't exist on the instance. Hence, the context of Me ("qualifying expression") is not required and the compiler is warning you of that.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • I understand that the compiler wants me to access static methods via the Class name, what I want to understand is what does it mean that "Visual Basic bypasses the expression and any other actions it would otherwise perform." What are some possible "other actions" that could by bypassed ? – Walter Stabosz Jan 17 '18 at 21:03
0

The Bark() method is a shared object. Shared objects are instanced only once at runtime. So any new instances point to the same instance. And as all new instances are redundant, the Intellisense gives the warning that the instance declaration is not needed. You can make the whole class shared thus not needing to instantiate it. Or remove the shared keyword from the method.

Now it won't throw the warning message.

And remember, you do not need to instance Shared classes, Constant members (e.g Public Const appStuff As fooObject) or Enum members. They can be directly accessed as shown above.

Walter Stabosz
  • 7,447
  • 5
  • 43
  • 75
Miguel Tomás
  • 1,714
  • 1
  • 13
  • 23