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
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