J
John Brock
I have a base class with several derived classes (I'm writing in
VB.NET). I want each derived class to have a unique class ID (a
String), and I want the derived classes to inherit from the base
class *Shared* methods which make use of the class ID.
So I gave the base class a classID field, and then I gave the
derived classes Shared constructors, which I used to set the classID
field to the appropriate values for each derived class. But this
doesn't work! In the example below the method
DerivedClass.PrintClassID()
prints the class ID for the base class, rather than the derived
class. So the Shared constructor for the derived class, which is
needed to properly initialize the inherited Shared methods of that
class, is not getting called.
However if I create an instance (xyz in the example) of the derived
class the Shared constructor *does* get called, even if I do nothing
with the class instance. This just seems wrong. One would expect
that the Shared fields of a class should get initialized before
any class Shared methods get called, and that this initialization
should not depend on the creation of an actual instance of the class.
So what is going on? Is this an outstanding bug, or a misfeature
that people have resigned themselves to living with, or have I
simply misunderstood a feature? And what should I do to get the
result I want? I can Shadow the PrintClassID method, and then it
works as expected. But I don't want to have to Shadow every method
which uses the classID field -- the whole point was that all that
functionality was to be handled entirely by the base class, without
the derived classes needing to know anything abou it.
Any thoughts?
'===== Begin Example =====
Imports System
Class BaseClass
Protected Shared classID As String = "BaseClass!"
Public Shared Sub PrintClassID()
Console.WriteLine("classID = " & classID)
End Sub
End Class
Class DerivedClass
Inherits BaseClass
Shared Sub New()
classID = "DerivedClass!"
End Sub
End Class
Module TestModule
Public Sub Main()
'Dim xyz As New DerivedClass
DerivedClass.PrintClassID()
End Sub
End Module
'===== End Example =====
VB.NET). I want each derived class to have a unique class ID (a
String), and I want the derived classes to inherit from the base
class *Shared* methods which make use of the class ID.
So I gave the base class a classID field, and then I gave the
derived classes Shared constructors, which I used to set the classID
field to the appropriate values for each derived class. But this
doesn't work! In the example below the method
DerivedClass.PrintClassID()
prints the class ID for the base class, rather than the derived
class. So the Shared constructor for the derived class, which is
needed to properly initialize the inherited Shared methods of that
class, is not getting called.
However if I create an instance (xyz in the example) of the derived
class the Shared constructor *does* get called, even if I do nothing
with the class instance. This just seems wrong. One would expect
that the Shared fields of a class should get initialized before
any class Shared methods get called, and that this initialization
should not depend on the creation of an actual instance of the class.
So what is going on? Is this an outstanding bug, or a misfeature
that people have resigned themselves to living with, or have I
simply misunderstood a feature? And what should I do to get the
result I want? I can Shadow the PrintClassID method, and then it
works as expected. But I don't want to have to Shadow every method
which uses the classID field -- the whole point was that all that
functionality was to be handled entirely by the base class, without
the derived classes needing to know anything abou it.
Any thoughts?
'===== Begin Example =====
Imports System
Class BaseClass
Protected Shared classID As String = "BaseClass!"
Public Shared Sub PrintClassID()
Console.WriteLine("classID = " & classID)
End Sub
End Class
Class DerivedClass
Inherits BaseClass
Shared Sub New()
classID = "DerivedClass!"
End Sub
End Class
Module TestModule
Public Sub Main()
'Dim xyz As New DerivedClass
DerivedClass.PrintClassID()
End Sub
End Module
'===== End Example =====