By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,688 Members | 1,877 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,688 IT Pros & Developers. It's quick & easy.

Instantiating class variables in parent constructor.

P: n/a
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
Oct 10 '07 #1
Share this Question
Share on Google+
18 Replies


P: n/a
You might try calling your constructors with the correct number of arguments.
--
Terry
"RB" wrote:
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
Oct 10 '07 #2

P: n/a
RB
Terry wrote:
You might try calling your constructors with the correct number of arguments.
Sorry, my bad. I was trying to simplify the code for the newsgroup.

The code for Color should, of course, read

Private Sub New(ByVal sShort As String)
MyBase.New(sShort)
End Sub

Cheers,

RB.
Oct 10 '07 #3

P: n/a
It may be better if you actually show how you hoped to use the color class.
Since the constructor is private, how do you create an instance? Maybe Color
should have a shared sub Initialize with code something like:
If list is nothing then
Red = New Color("Red")
.....etc
end if
--
Terry
"RB" wrote:
Terry wrote:
You might try calling your constructors with the correct number of arguments.

Sorry, my bad. I was trying to simplify the code for the newsgroup.

The code for Color should, of course, read

Private Sub New(ByVal sShort As String)
MyBase.New(sShort)
End Sub

Cheers,

RB.
Oct 10 '07 #4

P: n/a
The list is initialized in an instance constructor and used in a shared
method. How do you use those classes ? If you try to use the shared method
without creating an instance first it could fail...

--
Patrice

"RB" <ow************@mailinator.coma écrit dans le message de news:
YK******************************@eclipse.net.uk...
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

Oct 10 '07 #5

P: n/a
.....and since the constructor is Private, you can't create an instance!
--
Terry
"Patrice" wrote:
The list is initialized in an instance constructor and used in a shared
method. How do you use those classes ? If you try to use the shared method
without creating an instance first it could fail...

--
Patrice

"RB" <ow************@mailinator.coma écrit dans le message de news:
YK******************************@eclipse.net.uk...
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


Oct 10 '07 #6

P: n/a
RB
I thought that because the instances were themselves shared, they would
be created before the Parse shared method could be called. It certainly
works that way if I don't use inheritance, and have all the code in a
single class.

I should say that I'm a Java programmer by nature, where this technique
is a recognised pattern, so I might be missing something fundemental
about VB...!

Having said that, I never tried it with inheritance in Java either, so
maybe it's just a stupid thing to do :-)

RB.

Patrice wrote:
The list is initialized in an instance constructor and used in a shared
method. How do you use those classes ? If you try to use the shared method
without creating an instance first it could fail...

--
Patrice
Oct 10 '07 #7

P: n/a
RB
Terry wrote:
It may be better if you actually show how you hoped to use the color class.
Since the constructor is private, how do you create an instance? Maybe Color
should have a shared sub Initialize with code something like:
If list is nothing then
Red = New Color("Red")
.....etc
end if
The instances are created inside the class itself - see the following
lines in class Color:

Public Shared Red As Color = New Color("Red")
Public Shared GreenAs Color = New Color("Green")
Public Shared BlueAs Color = New Color("Blue")
An example of using it would be something like:

dim oColor as Color
oColor = Color.Parse("Red")
if oColor Is Color.Red Then
Messagebox.show ("You chose Red!")
End if

Generally speaking I'm parsing values from a database, but it's along
those lines.

The point of the exercise is to have typesafe constants, similar to the
old Java alternative to enumerators.

Cheers,

RB.
Oct 10 '07 #8

P: n/a
RB
Terry wrote:
.....and since the constructor is Private, you can't create an instance!
The TypesafeConstant constructor is protected, which (I think!) means
it's inherited to the child class. The Color constructor is private, but
all instances of it are created inside the class, so that should not be
a problem.

Having said that, I don't really understand why I need a Color
constructor at all. It's just that VB forced me to have one, as it
claimed I needed a no-arg constructor.

The reason it's private by the way, is to limit the set of constants to
those defined in the Color class. Really, Color should be defined as not
inheritable, but that doesn't affect the issue I'm facing.

Cheers,

RB.
Oct 10 '07 #9

P: n/a
On Oct 10, 11:18 am, RB <owmdkbqziki...@mailinator.comwrote:
Terry wrote:
It may be better if you actually show how you hoped to use the color class.
Since the constructor is private, how do you create an instance? Maybe Color
should have a shared sub Initialize with code something like:
If list is nothing then
Red = New Color("Red")
.....etc
end if

The instances are created inside the class itself - see the following
lines in class Color:

Public Shared Red As Color = New Color("Red")
Public Shared GreenAs Color = New Color("Green")
Public Shared BlueAs Color = New Color("Blue")

An example of using it would be something like:

dim oColor as Color
oColor = Color.Parse("Red")
if oColor Is Color.Red Then
Messagebox.show ("You chose Red!")
End if

Generally speaking I'm parsing values from a database, but it's along
those lines.

The point of the exercise is to have typesafe constants, similar to the
old Java alternative to enumerators.

Cheers,

RB.
I may be missing what you are trying to accomplish, but if all you
want is color by name, the System.Drawing.Color class already provides
this function with the Color.FromName method. This would accomplish
your example usage posted above. Then again, you may need something
else I don't see becuase you simplified it for the sake of posting.

Oct 10 '07 #10

P: n/a
RB
Charlie Brown wrote:
On Oct 10, 11:18 am, RB <owmdkbqziki...@mailinator.comwrote:
>Terry wrote:
>>It may be better if you actually show how you hoped to use the color class.
Since the constructor is private, how do you create an instance? Maybe Color
should have a shared sub Initialize with code something like:
If list is nothing then
Red = New Color("Red")
.....etc
end if
The instances are created inside the class itself - see the following
lines in class Color:

Public Shared Red As Color = New Color("Red")
Public Shared GreenAs Color = New Color("Green")
Public Shared BlueAs Color = New Color("Blue")

An example of using it would be something like:

dim oColor as Color
oColor = Color.Parse("Red")
if oColor Is Color.Red Then
Messagebox.show ("You chose Red!")
End if

Generally speaking I'm parsing values from a database, but it's along
those lines.

The point of the exercise is to have typesafe constants, similar to the
old Java alternative to enumerators.

Cheers,

RB.

I may be missing what you are trying to accomplish, but if all you
want is color by name, the System.Drawing.Color class already provides
this function with the Color.FromName method. This would accomplish
your example usage posted above. Then again, you may need something
else I don't see becuase you simplified it for the sake of posting.
Yeah, if it was just colors I would use that method! It's actually
various statuses of various business objects - I just chose colors as an
easier to understand example. Probably Pets would have been a better
choice of an example :-)

Cheers,

RB.
Oct 10 '07 #11

P: n/a
You need a no-arg constructor for XML serialization. Was that where you got
the message?

"RB" wrote:
Terry wrote:
.....and since the constructor is Private, you can't create an instance!

The TypesafeConstant constructor is protected, which (I think!) means
it's inherited to the child class. The Color constructor is private, but
all instances of it are created inside the class, so that should not be
a problem.

Having said that, I don't really understand why I need a Color
constructor at all. It's just that VB forced me to have one, as it
claimed I needed a no-arg constructor.

The reason it's private by the way, is to limit the set of constants to
those defined in the Color class. Really, Color should be defined as not
inheritable, but that doesn't affect the issue I'm facing.

Cheers,

RB.
Oct 10 '07 #12

P: n/a
Give this a try:

Public Class TypesafeConstant
Private sShortType As String

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 Color
Dim o As Color
For Each o In list
If o.Value.ToUpper = sValue.ToUpper Then
Return o
End If
Next
Return Nothing
End Function
End Class
------------TEST-------------

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oColor As Color
oColor = Color.Parse("Blue")
If oColor Is Color.Blue Then
MessageBox.Show("You chose blue!")
End If
End Sub

"RB" <ow************@mailinator.comwrote in message
news:j7******************************@eclipse.net. uk...
>I thought that because the instances were themselves shared, they would be
created before the Parse shared method could be called. It certainly works
that way if I don't use inheritance, and have all the code in a single
class.

I should say that I'm a Java programmer by nature, where this technique is
a recognised pattern, so I might be missing something fundemental about
VB...!

Having said that, I never tried it with inheritance in Java either, so
maybe it's just a stupid thing to do :-)

RB.

Patrice wrote:
>The list is initialized in an instance constructor and used in a shared
method. How do you use those classes ? If you try to use the shared
method without creating an instance first it could fail...

--
Patrice

Oct 10 '07 #13

P: n/a
Hi Again,
The problem is that
'dim oClor as Color' does not create an instance of the color class and the
constructor does not get called, so you have no underlying arraylist. One
not so pretty workaround is as follows:
in your TypeSafeConstantClass add:
Protected Sub New()
If list Is Nothing Then
list = New ArrayList()
End If
End Sub

and then in your Color class add:
Public Sub New()
MyBase.New()
End Sub

then finally, the code you use should be:
Dim oColor As New Color 'you need the New to actually create an instance
so the shared list will be created
oColor = CType(Color.Parse("Red"), Color)
If oColor Is Color.Red Then
......

--
Terry
"RB" wrote:
Terry wrote:
It may be better if you actually show how you hoped to use the color class.
Since the constructor is private, how do you create an instance? Maybe Color
should have a shared sub Initialize with code something like:
If list is nothing then
Red = New Color("Red")
.....etc
end if

The instances are created inside the class itself - see the following
lines in class Color:

Public Shared Red As Color = New Color("Red")
Public Shared GreenAs Color = New Color("Green")
Public Shared BlueAs Color = New Color("Blue")
An example of using it would be something like:

dim oColor as Color
oColor = Color.Parse("Red")
if oColor Is Color.Red Then
Messagebox.show ("You chose Red!")
End if

Generally speaking I'm parsing values from a database, but it's along
those lines.

The point of the exercise is to have typesafe constants, similar to the
old Java alternative to enumerators.

Cheers,

RB.
Oct 10 '07 #14

P: n/a
RB
Terry wrote:
Hi Again,
The problem is that
'dim oClor as Color' does not create an instance of the color class and the
constructor does not get called, so you have no underlying arraylist. One
not so pretty workaround is as follows:
in your TypeSafeConstantClass add:
Protected Sub New()
If list Is Nothing Then
list = New ArrayList()
End If
End Sub

and then in your Color class add:
Public Sub New()
MyBase.New()
End Sub

then finally, the code you use should be:
Dim oColor As New Color 'you need the New to actually create an instance
so the shared list will be created
oColor = CType(Color.Parse("Red"), Color)
If oColor Is Color.Red Then
......
Hi Terry,

Many thanks for your input, but that's almost exactly what I'm not
trying to do! The point of the class is to be a typesafe constant, so
allowing people to instantiate new constants is a bad thing!

Here's a link to what it would look like in Java:
http://java.sun.com/developer/Books/...l#replaceenums

I'm trying to do broadly the same thing in VB (which works), but I'm
trying to use inheritance, so that I can save having to re-code certain
things for each enumerator I define. I'm also trying to use an array so
that my Parse method is really easy to implement.

Cheers,

Rowland.
Oct 10 '07 #15

P: n/a
But they are not instanciating a new constant as the constructor does not
take an argument. Therefore they cannot add any constants to the list. If
you don't like that approach, then add a Shared Sub to Color class that does
not do anything and call it.

like this;

Public Shared Initialize()
End Sub

then your code to use it is

Color.Initialize()
Dim oColor as Color
oColor = Color.Parse.....
--
Terry
"RB" wrote:
Terry wrote:
Hi Again,
The problem is that
'dim oClor as Color' does not create an instance of the color class and the
constructor does not get called, so you have no underlying arraylist. One
not so pretty workaround is as follows:
in your TypeSafeConstantClass add:
Protected Sub New()
If list Is Nothing Then
list = New ArrayList()
End If
End Sub

and then in your Color class add:
Public Sub New()
MyBase.New()
End Sub

then finally, the code you use should be:
Dim oColor As New Color 'you need the New to actually create an instance
so the shared list will be created
oColor = CType(Color.Parse("Red"), Color)
If oColor Is Color.Red Then
......

Hi Terry,

Many thanks for your input, but that's almost exactly what I'm not
trying to do! The point of the class is to be a typesafe constant, so
allowing people to instantiate new constants is a bad thing!

Here's a link to what it would look like in Java:
http://java.sun.com/developer/Books/...l#replaceenums

I'm trying to do broadly the same thing in VB (which works), but I'm
trying to use inheritance, so that I can save having to re-code certain
things for each enumerator I define. I'm also trying to use an array so
that my Parse method is really easy to implement.

Cheers,

Rowland.
Oct 10 '07 #16

P: n/a
Have another solution you may like better...

add this to the color class

Public Shared Shadows Function Parse(ByVal sValue As String) As Color
Dim t As TypesafeConstant = TypesafeConstant.Parse(sValue)
Return CType(t, Color)
End Function

Now the code you showed using the color class, will work.

--
Terry
"RB" wrote:
Terry wrote:
Hi Again,
The problem is that
'dim oClor as Color' does not create an instance of the color class and the
constructor does not get called, so you have no underlying arraylist. One
not so pretty workaround is as follows:
in your TypeSafeConstantClass add:
Protected Sub New()
If list Is Nothing Then
list = New ArrayList()
End If
End Sub

and then in your Color class add:
Public Sub New()
MyBase.New()
End Sub

then finally, the code you use should be:
Dim oColor As New Color 'you need the New to actually create an instance
so the shared list will be created
oColor = CType(Color.Parse("Red"), Color)
If oColor Is Color.Red Then
......

Hi Terry,

Many thanks for your input, but that's almost exactly what I'm not
trying to do! The point of the class is to be a typesafe constant, so
allowing people to instantiate new constants is a bad thing!

Here's a link to what it would look like in Java:
http://java.sun.com/developer/Books/...l#replaceenums

I'm trying to do broadly the same thing in VB (which works), but I'm
trying to use inheritance, so that I can save having to re-code certain
things for each enumerator I define. I'm also trying to use an array so
that my Parse method is really easy to implement.

Cheers,

Rowland.
Oct 10 '07 #17

P: n/a
RB
Terry wrote:
Have another solution you may like better...

add this to the color class

Public Shared Shadows Function Parse(ByVal sValue As String) As Color
Dim t As TypesafeConstant = TypesafeConstant.Parse(sValue)
Return CType(t, Color)
End Function

Now the code you showed using the color class, will work.
Hi Terry,

That's basically what I did in the end, with one slight change - because
the TypesafeConstruct class is intended to be inherited by a number of
classes, the ArrayList list had to become a list of lists, where the
parent list lists child-class-types, and the sub-list lists the
child-class-members.

One final question before listing the code - what's the easiest way to
get a System.Type from a class name in a shared method. I know I can use

System.Type.GetType("MyNamespace.Pet")

or similar, but I was wondering if there was something that looked more
like:

System.Type.GetType(MyClass.Name)

I can't use Me or MyClass though, as it is a shared method.

Anyhoo my final code looks like:

Public Class TypesafeConstant
Private sShortType

Protected Shared list As Hashtable

Protected Sub New()
If list Is Nothing Then
list = New Hashtable()
End If
End Sub
Protected Sub New(ByVal sShortType As String)
Me.sShortType = sShortType
If list Is Nothing Then
list = New Hashtable()
End If
If Not list.ContainsKey(Me.GetType) Then
list.Add(Me.GetType, New ArrayList())
End If
DirectCast(list.Item(Me.GetType), ArrayList).Add(Me)
End Sub

Public ReadOnly Property Value() As String
Get
Return sShortType
End Get
End Property

Public Shared ReadOnly Property GetList(ByVal type As
System.Type) As Object()
Get
Return DirectCast(list.Item(type), ArrayList).ToArray(type)
End Get
End Property

Public Shared Function Parse(ByVal type As System.Type, ByVal
sValue As String) As TypesafeConstant
Dim o As TypesafeConstant
For Each o In GetList(type)
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 Green As Color = New Color("Green")
Public Shared Blue As Color = New Color("Blue")

Private Sub New()
MyBase.New()
End Sub
Private Sub New(ByVal sShort As String)
MyBase.New(sShort)
End Sub

Public Overloads Shared Function Parse(ByVal sValue As String)
As Color
Return Parse(Red.GetType, sValue)
End Function
End Class
Public Class Pet
Inherits TypesafeConstant

Public Shared Cat As Pet = New Pet("Cat")
Public Shared Dog As Pet = New Pet("Dog")
Public Shared Bird As Pet = New Pet("Bird")

Private Sub New()
MyBase.New()
End Sub
Private Sub New(ByVal sShort As String)
MyBase.New(sShort)
End Sub

Public Overloads Shared Function Parse(ByVal sValue As String)
As Pet
Return Parse(Cat.GetType, sValue)
End Function
End Class
'====================== TEST ====================='
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

Dim c As Color = Color.Parse("Red")

MessageBox.Show(c.Value)
Dim p As Pet = Pet.Parse("Cat")

MessageBox.Show(p.Value)

End Sub
Thanks for all your help everyone,

Cheers,

RB.
Oct 11 '07 #18

P: n/a
RB wrote:
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?
Public Class TypesafeConstant
.. . .
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 Shared Function Parse(ByVal sValue As String) As
TypesafeConstant
For Each o As TypesafeConstant _
In list.ToArray(GetType(TypesafeConstant))
If o.Value.ToUpper = sValue.ToUpper Then
Return o
End If
Next
End Function
End Class
Don't use a Shared variable /alone/ for the List - lock it up as a
Private variable in the base class and expose it via a Protected, Shared
Property.

Private Shared s_List As ArrayList

Protected Shared ReadOnly Property List() as ArrayList
Get
If ( s_List Is Nothing ) Then
s_List = New ArrayList
End If

Return s_List
End Get
End Property

Now, whenever any of your methods go anywhere near the "list", it is
guaranteed to get a valid instance back.

(Either that or use a "static constructor" mechanism that /always/
creates the list, as in

Public Class TypesafeConstant

Private Shared s_List As New ArrayList

HTH,
Phill W.
wrap it in a
Oct 11 '07 #19

This discussion thread is closed

Replies have been disabled for this discussion.