Creation Of NEW Class Members That Are Classes

  • Thread starter Thread starter Tappy Tibbons
  • Start date Start date
T

Tappy Tibbons

I do not know exactly how to explain what I am asking for, but here goes...

Say I have a simple set of classes:
=========
Public Class clsPerson
Public FirstName As String
Public LastName As String
Public BillingAddress As clsAddress
Public ShippingAddress As clsAddress
End Class

Public Class clsAddress
Public AddressLine1 As String
Public AddressLine2 As String
Public City As String
Public StateCode As String
Public ZipCode As String
End Class
==========

From my calling form, I want to do something like:

Public Function DoTest()
Dim oPerson As New clsPerson()
oPerson.FirstName = "John"
oPerson.LastName = "Smith"
oPerson.BillingAddress.AddressLine1 = "12345 E 6th St"

...'do some other stuff
...

End Function

In clsPerson, I could just define "Public BillingAddress As NEW clsAddress",
but I do not always have a billing address. If I do not have one, I do not
want to create an instance of that class. This class is going to be used by
the XML serializer class, and long story short, is that I do not want the
class initialized of there is not going to be any real data in it.

Is there a way to automatically create/set that class to a NEW instance of
itself the first time it is accessed at runtime?

Is there a way I could use an error handler from within the class to trap
the "Object reference not set to an instance of an object." error, then
figure out which class member was responsible for raising the error, then
set that class member to a NEW instance of itself and then continue?

Are there other ways to do this?

Thanks!
 
Tappy Tibbons said:
I do not know exactly how to explain what I am asking for, but here goes...

Say I have a simple set of classes:
=========
Public Class clsPerson
Public FirstName As String
Public LastName As String
Public BillingAddress As clsAddress
Public ShippingAddress As clsAddress
End Class

Public Class clsAddress
Public AddressLine1 As String
Public AddressLine2 As String
Public City As String
Public StateCode As String
Public ZipCode As String
End Class
==========

From my calling form, I want to do something like:

Public Function DoTest()
Dim oPerson As New clsPerson()
oPerson.FirstName = "John"
oPerson.LastName = "Smith"
oPerson.BillingAddress.AddressLine1 = "12345 E 6th St"

...'do some other stuff
...

End Function

In clsPerson, I could just define "Public BillingAddress As NEW clsAddress",
but I do not always have a billing address. If I do not have one, I do not
want to create an instance of that class. This class is going to be used by
the XML serializer class, and long story short, is that I do not want the
class initialized of there is not going to be any real data in it.

Is there a way to automatically create/set that class to a NEW instance of
itself the first time it is accessed at runtime?


I don't know if it helps with your serialization problem, but you could add
a property and keep the field private.

Public Class clsPerson
Public FirstName As String
Public LastName As String
Private m_BillingAddress As clsAddress
Public ShippingAddress As clsAddress

Public ReadOnly Property BillingAddress() As clsAddress
Get
If m_BillingAddress Is Nothing Then
m_BillingAddress = New clsAddress
End If
Return m_BillingAddress
End Get
End Property
End Class

This code creates the instance the first time the property is read.
 
I understand what you want to do, but don't think there's a good way to do
it.

If you want to use the Person class but not instantiate the address members
at the start, e.g., you could change your code below to

Dim oPerson As New clsPerson()
oPerson.FirstName = "John"
oPerson.LastName = "Smith"
oPerson.BillingAddress = New clsAddress
oPerson.BillingAddress.AddressLine1 = "12345 E 6th St"

Then, later in the code, you'd need to test whether the person has a billing
address or shipping address before referencing it:

If Not (oPerson.BillingAddress is nothing) then
... do something with the BillingAddress
End If

Relying on exception handling to do this job for you would not be a good
practice. First, exceptions are fairly slow. Second, the Microsoft
guidelines state that exceptions should only be raised for real errors --
things you didn't anticipate. It's rather cheesy to intentionally leave
logical errors in your code and then rely on exception handling to sort
things out for you.

Just for the sake of asking, do you really need to save space in your XML
file? Things would be simpler if you just initialized the address fields,
whether or not you needed them.
 
Good idea, but it won't help with his serialization problem. When the
serializer is serializing the class, it will call the Property Get for
BillingAddress to determine how to serialize that property. When that
property get is called, it will create a new (blank) BillingAddress, which
will then be serialized.
 
Thanks for the good information. The error exception idea I had was just a
shot in the dark.

I may be able to get by with just letting the serializer insert the blank
members, but the system I am posting the XML to has really gone overboard
with classes on top of classes on top of classes on top of classes, so even
a small object generates TONS of empty nested nodes and I thought if I could
get rid of them it would at least make what I was posting smaller and
cleaner.
 
Good thought Lance, but I don't think that will fit the bill. That
attribute's used for code generators (like DesignMode for Windows Forms) to
specify the default value for a property (like the default border for a
RichTextBox.) Here, Tabby's not using a code generator -- he's just needing
to instantiate classes from his own code.
 
Back
Top