Home All Groups Group Topic Archive Search About

inheritance and shared

Author
25 Mar 2005 12:49 AM
rodchar
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

Author
25 Mar 2005 1:03 AM
Herfried K. Wagner [MVP]
"rodchar" <rodc***@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/>
Author
25 Mar 2005 8:29 PM
rodchar
thanks, this helps.

Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> "rodchar" <rodc***@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/>
>
>