Home All Groups Group Topic Archive Search About

Const Member in Base Class to be overridden in Derived Class?

Author
31 Jan 2006 10:11 PM
Joe HM
Hello -

I am trying to figure out how I can define a constant in a base class
and override it in a derived class.  The following shows a little
example ...

Module Test
    Sub Main()

        Dim A As BaseClass

        Dim lSelect As Integer = 1

        Select Case lSelect
            Case 0
                A = New BaseClass
                Console.WriteLine(A.CONSTANT)
                A.print()
            Case 1
                A = New DerivedClassA
                Console.WriteLine(A.CONSTANT)
                A.print()
            Case 2
                A = New DerivedClassB
                Console.WriteLine(A.CONSTANT)
                A.print()
        End Select
    End Sub
End Module

Public Class BaseClass
    Public Const CONSTANT As String = "BaseClass"

    Public Overridable Sub print()
        Console.WriteLine("BaseClass.print()")
    End Sub
End Class

Public Class DerivedClassA
    Inherits BaseClass

    Public Shadows Const CONSTANT As String = "DerivedClassA"

    Public Overrides Sub print()
        Console.WriteLine("DerivedClassA.print()")
    End Sub
End Class

Public Class DerivedClassB
    Inherits BaseClass

    Public Shadows Const CONSTANT As String = "DerivedClassB"

    Public Overrides Sub print()
        Console.WriteLine("DerivedClassB.print()")
    End Sub
End Class

The problem is that no matter what I set lSelect to, it will use the
appropriate print() function but not the appropriate CONSTANT.

This is part of an effort where we switch the design of one of our
utilities to use OOP.  The other problem is that we used to have some
constants defined in a module.  So all the user had to do is use the
constant once the namespace was imported.  Now it will always require
to specify the base class (i.e. A.CONSTANT).  Is there a way to do that
without the A?  Can I make constants from a class visible without the
class namespace?

Thanks for any feedback!
Joe

Author
31 Jan 2006 10:40 PM
Herfried K. Wagner [MVP]
"Joe HM" <unixve***@yahoo.com> schrieb:
> I am trying to figure out how I can define a constant in a base class
> and override it in a derived class.  The following shows a little
> example ...

Use a 'ReadOnly' property instead of the 'Const' declaration.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
1 Feb 2006 11:29 AM
Joe HM
Hello -

Thanks for you reply.  I did try the ReadOnly as follows but it is
still not working ...

Public Class BaseClass
    Public ReadOnly CONSTANT As String = "BaseClass"

    Public Overridable Sub print()
        Console.WriteLine("BaseClass.print()")
    End Sub
End Class

Public Class DerivedClassA
    Inherits BaseClass

    Public Shadows ReadOnly CONSTANT As String = "DerivedClassA"

    Public Overrides Sub print()
        Console.WriteLine("DerivedClassA.print()")
    End Sub
End Class

Public Class DerivedClassB
    Inherits BaseClass

    Public Shadows ReadOnly CONSTANT As String = "DerivedClassB"

    Public Overrides Sub print()
        Console.WriteLine("DerivedClassB.print()")
    End Sub
End Class

The following still outputs the CONSTANT from the BaseClass rather than
DerivedClassA ...

Dim A As BaseClass
A = New DerivedClassA
Console.WriteLine(A.CONSTANT)
A.print()

Thanks!
Joe
Author
1 Feb 2006 11:55 AM
Herfried K. Wagner [MVP]
"Joe HM" <unixve***@yahoo.com> schrieb:
> Thanks for you reply.  I did try the ReadOnly as follows but it is
> still not working ...
>
> Public Class BaseClass
>    Public ReadOnly CONSTANT As String = "BaseClass"

Use a /property/ instead of a constant field:

\\\
Public Overridable ReadOnly Property Foo() As String
    Get
        Return "BaseClass"
    End Get
End Property
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
1 Feb 2006 11:58 AM
Larry Lard
Joe HM wrote:
> Hello -
>
> Thanks for you reply.  I did try the ReadOnly as follows but it is
> still not working ...

Herfried said "use a ReadOnly *property*" :

Module Module1

    Sub Main()

        Dim A As BaseClass
        A = New DerivedClassA
        Console.WriteLine(A.CONSTANT)

        A.print()

        Console.ReadLine()
    End Sub

End Module

Public Class BaseClass
    Public Overridable ReadOnly Property CONSTANT() As String
        Get
            Return "BaseClass"
        End Get
    End Property

    Public Overridable Sub print()
        Console.WriteLine("BaseClass.print()")
    End Sub
End Class

Public Class DerivedClassA
    Inherits BaseClass

    Public Overrides Sub print()
        Console.WriteLine("DerivedClassA.print()")
    End Sub

    Public Overrides ReadOnly Property CONSTANT() As String
        Get
            Return "DerivedClassA"
        End Get
    End Property
End Class

' ReadOnly makes a Property have a Get but no Set (WriteOnly exists as
wel!)

--
Larry Lard
Replies to group please
Author
1 Feb 2006 12:10 PM
Joe HM
Hello -

I actually just figured out the perfect solution for my problem ...
using a Property ...

Public Class BaseClass
    Private BOX_PROMPTBase As String = "BaseClass"

    Overridable ReadOnly Property CONST() As String
        Get
            Return CONSTBase
        End Get
    End Property
....

Public Class DerivedClassA
    Private CONSTANTDerivedClassA As String = "DerivedClassA"

    Overridable ReadOnly Property CONST() As String
        Get
            Return CONSTANTDerivedClassA
        End Get
    End Property

The only thing I have not figured out is how to avoid having to use the
class instance "A" in A.CONST but that is probably something that just
has to be that way.


Joe
Author
1 Feb 2006 12:20 PM
Herfried K. Wagner [MVP]
"Joe HM" <unixve***@yahoo.com> schrieb:
> The only thing I have not figured out is how to avoid having to use the
> class instance "A" in A.CONST but that is probably something that just
> has to be that way.

Mark the property as 'Shared'.  However, when doing so you cannot override
it any more because shared methods cannot be overridden.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
1 Feb 2006 12:47 PM
Joe HM
Thanks for the tip!  I think I need to be able to override it so I
guess I have to live with the "A.".

I actually have another questions regarding properties.  I have a
function Public Sub print(Optional ByVal aParameter = XXX) where I
would like to use a Property for XXX.  I would like that XXX will be
the overridden Property in the DerivedClassA when print() is called on
an instance of DerivedClassA (which does not have print() implemented
but inherits it from BaseClass).  The compiler tells me that an
Optional argument needs to be Const.  So is there a way that this can
be set at runtime?

Thanks a lot for the feedback!
Joe
Author
1 Feb 2006 1:04 PM
Herfried K. Wagner [MVP]
"Joe HM" <unixve***@yahoo.com> schrieb:
>The compiler tells me that an
> Optional argument needs to be Const.  So is there a way that this can
> be set at runtime?

No, that's not possible.  However, you could use overloaded methods instead:

\\\
Public Overloads Sub Foo()
    Foo(Me.Bla)    ' 'Me.Bla' doesn't need to be a constant.
End Sub

Public Overloads Sub Foo(ByVal b As Bla)
    ...
End Sub
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
1 Feb 2006 2:18 PM
Joe HM
Great ... thanks so much for you help!

Joe