By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,256 Members | 1,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,256 IT Pros & Developers. It's quick & easy.

inheritance and shared

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"rodchar" <ro*****@discussions.microsoft.com> schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2

P: n/a
thanks, this helps.

"Herfried K. Wagner [MVP]" wrote:
"rodchar" <ro*****@discussions.microsoft.com> schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.