473,405 Members | 2,279 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,405 software developers and data experts.

Creation Of NEW Class Members That Are Classes

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!
Nov 20 '05 #1
6 1298
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.

"Tappy Tibbons" <ta**********@sbcglobal.net> wrote in message
news:el**************@TK2MSFTNGP11.phx.gbl...
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!

Nov 20 '05 #2

"Tappy Tibbons" <ta**********@sbcglobal.net> schrieb im Newsbeitrag
news:el**************@TK2MSFTNGP11.phx.gbl...
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.
--
Armin

Nov 20 '05 #3
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.
"Armin Zingler" <az*******@freenet.de> wrote in message
news:u%******************@TK2MSFTNGP10.phx.gbl...


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.
--
Armin

Nov 20 '05 #4
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.

"Robert Jacobson" <rj**********************@nospam.com> wrote in message
news:ey****************@tk2msftngp13.phx.gbl...
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.

"Tappy Tibbons" <ta**********@sbcglobal.net> wrote in message
news:el**************@TK2MSFTNGP11.phx.gbl...
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!


Nov 20 '05 #5
Would it be possible to use
System.ComponentModel.DefaultValue?
Nov 20 '05 #6
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.

"Lance" <zi***@hotmail.com> wrote in message
news:11****************************@phx.gbl...
Would it be possible to use
System.ComponentModel.DefaultValue?

Nov 20 '05 #7

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

Similar topics

16
by: 4Space | last post by:
I've hit something of a snag. Problem: In a DSP application we have data profiles with a varying number of points. We have a number of Discrete Fourier transforms that are optimised for a...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
5
by: Tappy Tibbons | last post by:
I have a class I am serializing, and need the resultant XML to skip/omit classes that are not initialized, or their member variables have not been set. Is this possible? Say for the following...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
10
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base classes.( I already have the ReflectionPermission )...
10
by: John | last post by:
Hi, How can I achieve that a childs constructor is only callable from it's parent? Must I declare the class Private within A? Thanks in Advance Public MustInherit Class A Protected Sub...
1
by: D Witherspoon | last post by:
Coming up with a scenario here. For example there is the standard .NET MailMessage class. I am creating a project (let's call it CommonBase) that has the following 2 classes ...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
31
by: Tom P. | last post by:
I am doing quite a bit of custom painting and it means I have to create a lot of brushes (think one for every file system object in a directory) per paint. How expensive is this? Should I find a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
0
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...
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...
0
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...
0
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,...
0
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...

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.