MyClass & Shared

  • Thread starter Thread starter sougata12
  • Start date Start date
S

sougata12

Guest
Hi,

In the MSDN documentation for MyClass the following statement is found:
"MyClass cannot be used inside a Shared method, but can be used inside an instance method to access a shared member of a class."
Inheritance Basics

To understand this I wrote the following piece of code:

Module Module1
Sub Main()
Dim instance As New inherited()
instance.anotherSub()
Console.ReadLine()
End Sub
End Module

Class base
Public Shared Sub someSub()
Dim a As Integer = 100
Console.WriteLine("Base class: Value of a = {0}", a)
End Sub
End Class

Class inherited : Inherits base
Public Sub anotherSub()
MyClass.someSub() 'warning message coming here
End Sub
End Class

Question:

As per my understanding, Since MyClass refers to the current instance of the class that is being executed and the SHARED sub someSub is access using the class name I am getting that warning message (green squiggly line) under MyBase.someSub(). Now if that is correct then how is it possible that MyClass be used to access a shared member (as mentioned in the MSDN documentation), because MyClass would always refer to an instance while any Shared member will be accessed by its class name......this means I shall always get this warning message when I try to do this.

Do you think I am making sense?.....or is it like I should accept this warning message as normal (since it is not hampering execution of the code) and move on?

The warning message I am getting is as follows:

1504999.png


Sougata Ghosh

Continue reading...
 
Back
Top