none
MyClass & Shared RRS feed

  • Question

  • 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:


    Sougata Ghosh

    Sunday, November 17, 2019 12:15 PM

Answers

  • The documentation is correct but to add to this, the correct way to write this code is shown below.  SomeSub is know to Class Inherited from the base class so there is no need to use MyClass as this is implied.

    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()
            SomeSub() 'warning message coming here
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by sougata12 Sunday, November 17, 2019 3:19 PM
    Sunday, November 17, 2019 12:42 PM
    Moderator
  • Hi,

    I accept your point that MyClass is not required in this case.

    However can you please give me an example where the "MyClass" keyword  is being explicitly used to access a  Shared member of a class? That will help me clearly understand the meaning of the statement in the MSDN documentation. Because I am still unable to understand how MyClass & Shared will be used together as the former refers to the current INSTANCE while the shared member needs to be accessed using the class name


    Sougata Ghosh



    I believe this is a good conversionation on using MyClass. Personally never used MyClass since starting with VB.NET.

    https://stackoverflow.com/questions/2849329/mybase-and-myclass-usage-in-vb-net


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, November 17, 2019 1:47 PM
    Moderator

All replies

  • The documentation is correct but to add to this, the correct way to write this code is shown below.  SomeSub is know to Class Inherited from the base class so there is no need to use MyClass as this is implied.

    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()
            SomeSub() 'warning message coming here
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by sougata12 Sunday, November 17, 2019 3:19 PM
    Sunday, November 17, 2019 12:42 PM
    Moderator
  • Hi,

    I accept your point that MyClass is not required in this case.

    However can you please give me an example where the "MyClass" keyword  is being explicitly used to access a  Shared member of a class? That will help me clearly understand the meaning of the statement in the MSDN documentation. Because I am still unable to understand how MyClass & Shared will be used together as the former refers to the current INSTANCE while the shared member needs to be accessed using the class name


    Sougata Ghosh



    • Edited by sougata12 Sunday, November 17, 2019 1:05 PM
    Sunday, November 17, 2019 1:04 PM
  • Maybe your code is an example of MyClass that does not produce any compile-time and run-time error.

    Sunday, November 17, 2019 1:40 PM
  • Hi,

    I accept your point that MyClass is not required in this case.

    However can you please give me an example where the "MyClass" keyword  is being explicitly used to access a  Shared member of a class? That will help me clearly understand the meaning of the statement in the MSDN documentation. Because I am still unable to understand how MyClass & Shared will be used together as the former refers to the current INSTANCE while the shared member needs to be accessed using the class name


    Sougata Ghosh



    I believe this is a good conversionation on using MyClass. Personally never used MyClass since starting with VB.NET.

    https://stackoverflow.com/questions/2849329/mybase-and-myclass-usage-in-vb-net


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, November 17, 2019 1:47 PM
    Moderator
  • Maybe your code is an example of MyClass that does not produce any compile-time and run-time error.

    Thanks...:)

    Sougata Ghosh

    Sunday, November 17, 2019 3:19 PM
  • Hi,

    I accept your point that MyClass is not required in this case.

    However can you please give me an example where the "MyClass" keyword  is being explicitly used to access a  Shared member of a class? That will help me clearly understand the meaning of the statement in the MSDN documentation. Because I am still unable to understand how MyClass & Shared will be used together as the former refers to the current INSTANCE while the shared member needs to be accessed using the class name


    Sougata Ghosh



    I believe this is a good conversionation on using MyClass. Personally never used MyClass since starting with VB.NET.

    https://stackoverflow.com/questions/2849329/mybase-and-myclass-usage-in-vb-net


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thanks for the stack-overflow link 

    Sougata Ghosh

    Sunday, November 17, 2019 3:20 PM