R
RB
Hi guys (and gals!),
I've got 2 classes, "TypesafeConstant" and "Color". "Color" inherits
from "TypesafeConstant", and adds no new functionality. All "Color" does
is to instantiate some class variables which are public instances of
"Color".
What "TypesafeConstant" does is to create a shared list, and add every
declaration of itself to that list. I can then query that list with a
string (e.g. "Red") to return Color.Red.
Now, if I take out the inheritance, and just have "Color" instantiating
itself, everything works fine, but with the inheritance the Parse
function errors with a "NullReferenceException", which is caused by
TypesafeConstant.list being a null reference.
Any ideas how I could re-write this to make it work?
Oh, and just to say, I want to use this technique a lot, hence the
inheritance - I don't want to have to re-write the entire
TypesafeConstant each time!!
Many Thanks,
RB.
************* CODE *******************
Public Class TypesafeConstant
Private sShortType
Protected Shared list As ArrayList
Protected Sub New(ByVal sShortType As String)
Me.sShortType = sShortType
If list Is Nothing Then
list = New ArrayList()
End If
list.Add(Me)
End Sub
Public ReadOnly Property Value() As String
Get
Return sShortType
End Get
End Property
Public Shared Function Parse(ByVal sValue As String) As
TypesafeConstant
Dim o As TypesafeConstant
For Each o In list.ToArray(GetType(TypesafeConstant))
If o.Value.ToUpper = sValue.ToUpper Then
Return o
End If
Next
End Function
End Class
Public Class Color
Inherits TypesafeConstant
Public Shared Red As Color = New Color("Red")
Public Shared GreenAs Color = New Color("Green")
Public Shared BlueAs Color = New Color("Blue")
Private Sub New(ByVal sShort As String, ByVal sLong As String)
MyBase.New(sShort, sLong)
End Sub
End Class
I've got 2 classes, "TypesafeConstant" and "Color". "Color" inherits
from "TypesafeConstant", and adds no new functionality. All "Color" does
is to instantiate some class variables which are public instances of
"Color".
What "TypesafeConstant" does is to create a shared list, and add every
declaration of itself to that list. I can then query that list with a
string (e.g. "Red") to return Color.Red.
Now, if I take out the inheritance, and just have "Color" instantiating
itself, everything works fine, but with the inheritance the Parse
function errors with a "NullReferenceException", which is caused by
TypesafeConstant.list being a null reference.
Any ideas how I could re-write this to make it work?
Oh, and just to say, I want to use this technique a lot, hence the
inheritance - I don't want to have to re-write the entire
TypesafeConstant each time!!
Many Thanks,
RB.
************* CODE *******************
Public Class TypesafeConstant
Private sShortType
Protected Shared list As ArrayList
Protected Sub New(ByVal sShortType As String)
Me.sShortType = sShortType
If list Is Nothing Then
list = New ArrayList()
End If
list.Add(Me)
End Sub
Public ReadOnly Property Value() As String
Get
Return sShortType
End Get
End Property
Public Shared Function Parse(ByVal sValue As String) As
TypesafeConstant
Dim o As TypesafeConstant
For Each o In list.ToArray(GetType(TypesafeConstant))
If o.Value.ToUpper = sValue.ToUpper Then
Return o
End If
Next
End Function
End Class
Public Class Color
Inherits TypesafeConstant
Public Shared Red As Color = New Color("Red")
Public Shared GreenAs Color = New Color("Green")
Public Shared BlueAs Color = New Color("Blue")
Private Sub New(ByVal sShort As String, ByVal sLong As String)
MyBase.New(sShort, sLong)
End Sub
End Class