473,810 Members | 2,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Instantiating class variables in parent constructor.

RB
Hi guys (and gals!),

I've got 2 classes, "TypesafeConsta nt" and "Color". "Color" inherits
from "TypesafeConsta nt", and adds no new functionality. All "Color" does
is to instantiate some class variables which are public instances of
"Color".

What "TypesafeConsta nt" 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 "NullReferenceE xception", which is caused by
TypesafeConstan t.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
TypesafeConstan t each time!!

Many Thanks,

RB.
************* CODE *************** ****
Public Class TypesafeConstan t
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
TypesafeConstan t
Dim o As TypesafeConstan t
For Each o In list.ToArray(Ge tType(TypesafeC onstant))
If o.Value.ToUpper = sValue.ToUpper Then
Return o
End If
Next
End Function
End Class
Public Class Color
Inherits TypesafeConstan t

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(sSho rt, sLong)
End Sub
End Class
Oct 10 '07 #1
18 1872
You might try calling your constructors with the correct number of arguments.
--
Terry
"RB" wrote:
Hi guys (and gals!),

I've got 2 classes, "TypesafeConsta nt" and "Color". "Color" inherits
from "TypesafeConsta nt", and adds no new functionality. All "Color" does
is to instantiate some class variables which are public instances of
"Color".

What "TypesafeConsta nt" 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 "NullReferenceE xception", which is caused by
TypesafeConstan t.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
TypesafeConstan t each time!!

Many Thanks,

RB.
************* CODE *************** ****
Public Class TypesafeConstan t
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
TypesafeConstan t
Dim o As TypesafeConstan t
For Each o In list.ToArray(Ge tType(TypesafeC onstant))
If o.Value.ToUpper = sValue.ToUpper Then
Return o
End If
Next
End Function
End Class
Public Class Color
Inherits TypesafeConstan t

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(sSho rt, 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(sSho rt)
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(sSho rt)
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.com a écrit dans le message de news:
YK************* *************** **@eclipse.net. uk...
Hi guys (and gals!),

I've got 2 classes, "TypesafeConsta nt" and "Color". "Color" inherits from
"TypesafeConsta nt", and adds no new functionality. All "Color" does is to
instantiate some class variables which are public instances of "Color".

What "TypesafeConsta nt" 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 "NullReferenceE xception", which is caused by
TypesafeConstan t.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 TypesafeConstan t
each time!!

Many Thanks,

RB.
************* CODE *************** ****
Public Class TypesafeConstan t
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
TypesafeConstan t
Dim o As TypesafeConstan t
For Each o In list.ToArray(Ge tType(TypesafeC onstant))
If o.Value.ToUpper = sValue.ToUpper Then
Return o
End If
Next
End Function
End Class
Public Class Color
Inherits TypesafeConstan t

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(sSho rt, 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.com a écrit dans le message de news:
YK************* *************** **@eclipse.net. uk...
Hi guys (and gals!),

I've got 2 classes, "TypesafeConsta nt" and "Color". "Color" inherits from
"TypesafeConsta nt", and adds no new functionality. All "Color" does is to
instantiate some class variables which are public instances of "Color".

What "TypesafeConsta nt" 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 "NullReferenceE xception", which is caused by
TypesafeConstan t.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 TypesafeConstan t
each time!!

Many Thanks,

RB.
************* CODE *************** ****
Public Class TypesafeConstan t
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
TypesafeConstan t
Dim o As TypesafeConstan t
For Each o In list.ToArray(Ge tType(TypesafeC onstant))
If o.Value.ToUpper = sValue.ToUpper Then
Return o
End If
Next
End Function
End Class
Public Class Color
Inherits TypesafeConstan t

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(sSho rt, 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("Re d")
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 TypesafeConstan t 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
RB
Charlie Brown wrote:
On Oct 10, 11:18 am, RB <owmdkbqziki... @mailinator.com 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("Re d")
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 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
13230
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
1261
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 called. The constructor didnt touch the collection class and defineately didn't initialise it. Some one suggested the following reason as what is happening.
5
3657
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 parent class, and I can't make some "helper" class a friend of the parent class to help in accessing the data.) Problem: I want to derive a class that has a copy constructor that properly copies those data members.
6
2670
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 the object and set default blank/null values), and then does all the Db access and number crunching to populate the new object. The factory returns the fully populated object to the caller. All the fields in the object are private, but have...
5
4583
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 constructors (because we do not allow developers to instantiate them directly). Because our business objects are instantiated very frequently, the idea of using reflection sounds like a performance killer (I haven't done any tests on this, but the...
15
2055
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, GLint)= 0; virtual void setWidth(GLint)= 0; virtual void setHeight(GLint)= 0;
10
19628
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 Class Parent '*** HOW TO DECLARE IT *** Dim Age As String = "1/1/2000"
61
2990
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; function my_baseclass(){ $this->TableName = "";
4
4402
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 interface type (as the virtual functions match up), but there is a snag. No members are initialized, which means it can't be used as the original type if it has anything but virtual functions. It would be possible to make it work, if I could call the...
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10643
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.