473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Instantiating class variables in parent constructor.

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
18 1842
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
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
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
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
.....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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
1
by: thechaosengine | last post by:
Hi everyone, I posted a question earlier about a collection class that I'd made seemingly getting instantiated magically after a class that contained a variable of that type had its constructor...
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
6
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
15
by: Manuel | last post by:
The parent is an abstract class, with default implicit constructor: ----------------------------------- class mhwidget { public: virtual void draw()= 0; virtual void setPosition(GLint,...
10
by: Goran Djuranovic | last post by:
Hi all, Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class? For example: ************* CODE ***************** Public...
61
by: Sanders Kaufman | last post by:
I'm wondering if I'm doing this right, as far as using another class object as a PHP class property. class my_baseclass { var $Database; var $ErrorMessage; var $TableName; var $RecordSet;...
4
by: dascandy | last post by:
Hi, For a project I'm working on I'm kind-of-hacking my way around deriving a class from an interface or such to create a mock, but instead creating the mock directly. It is usable as the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.