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

J

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
 
H

Herfried K. Wagner [MVP]

Joe HM said:
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.
 
J

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
 
H

Herfried K. Wagner [MVP]

Joe HM said:
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
///
 
L

Larry Lard

Joe said:
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!)
 
J

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
 
H

Herfried K. Wagner [MVP]

Joe HM said:
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.
 
J

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
 
H

Herfried K. Wagner [MVP]

Joe HM said:
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
///
 

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

Top