inheritance and shared

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hey all,

i have a MustInherit class and a derived class. is it possible to have a
shared method in the base class and use it?

Note: this is not working for me but just going to explain.

i have a base class that has a shared function that returns a connection
string for a datasource. is there a way for the derived class to use that?
when i try me.connStr it says i have to instantiate it.

thanks,
rodchar
 
rodchar said:
i have a MustInherit class and a derived class. is it possible to have a
shared method in the base class and use it?

Note: this is not working for me but just going to explain.

i have a base class that has a shared function that returns a connection
string for a datasource. is there a way for the derived class to use that?
when i try me.connStr it says i have to instantiate it.

Shared methods are not "virtual" and not bound to an instance of a class.
Nevertheless you can access them using a variable that is pointing to an
instance of the type or one of its derived types. Typically shared methods
are qualified with the class name of the class they are defined in:

\\\
Public MustInherit Class Bar
Private Shared m_UserName As String = "John Doe"

Public Shared Function GetUserName() As String
Return m_UserName
End Function
End Class

Public Class FooBar
Inherits Bar

Public Sub New()
MsgBox(Bar.GetUserName())
End Sub
End Class
///
 
thanks, this helps.

Herfried K. Wagner said:
Shared methods are not "virtual" and not bound to an instance of a class.
Nevertheless you can access them using a variable that is pointing to an
instance of the type or one of its derived types. Typically shared methods
are qualified with the class name of the class they are defined in:

\\\
Public MustInherit Class Bar
Private Shared m_UserName As String = "John Doe"

Public Shared Function GetUserName() As String
Return m_UserName
End Function
End Class

Public Class FooBar
Inherits Bar

Public Sub New()
MsgBox(Bar.GetUserName())
End Sub
End Class
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top