473,509 Members | 3,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array of objects inside a class

How would I create an array of objects inside a class? When I do it the
following way I get an error that says: 'Object reference not set to an
instance of an object'.

Public Class Inmate
Private strName As String
Private strSSN As String
Private strDOB As String
Private myMugshotPicture() As MugshotPicture
End Class

Public Class MugshotPicture
Private strPictureName As String
Private strDateTaken As String
End Class
Jul 21 '05 #1
10 1425
Cam
My VB.NET is pretty rusty, but I think your problem is that an Array is
actually an object itself, and you'll need to create an instance of it
(probably in the contructor for Inmate) before accessing it.
How would I create an array of objects inside a class? When I do it the
following way I get an error that says: 'Object reference not set to an
instance of an object'.

Public Class Inmate
Private strName As String
Private strSSN As String
Private strDOB As String
Private myMugshotPicture() As MugshotPicture
End Class

Public Class MugshotPicture
Private strPictureName As String
Private strDateTaken As String
End Class


Jul 21 '05 #2

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
How would I create an array of objects inside a class? When I do it the
following way I get an error that says: 'Object reference not set to an
instance of an object'.

Public Class Inmate
Private strName As String
Private strSSN As String
Private strDOB As String
Private myMugshotPicture() As MugshotPicture
End Class

Public Class MugshotPicture
Private strPictureName As String
Private strDateTaken As String
End Class


As putting the code you provide in a class library project and compiling it
produces no errors, I'll guess you're getting the error when attempting to
reference myMugShotPicture from other code?
At the time of the attempt, have you populated the array with actual
MugshotPicture object variables? If not, that's your problem.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Jul 21 '05 #3
That's what I was thinking. I'm googling it right now but does anyone know
that syntax of the top of their head. Thanks for the help Cam!

"Cam" wrote:
My VB.NET is pretty rusty, but I think your problem is that an Array is
actually an object itself, and you'll need to create an instance of it
(probably in the contructor for Inmate) before accessing it.
How would I create an array of objects inside a class? When I do it the
following way I get an error that says: 'Object reference not set to an
instance of an object'.

Public Class Inmate
Private strName As String
Private strSSN As String
Private strDOB As String
Private myMugshotPicture() As MugshotPicture
End Class

Public Class MugshotPicture
Private strPictureName As String
Private strDateTaken As String
End Class

Jul 21 '05 #4
Clearic,

Have a look at this page, it has a complete (a little bit complex) sample in
it.

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps

Cor
Jul 21 '05 #5
"Peter van der Goes" wrote:

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
How would I create an array of objects inside a class? When I do it the
following way I get an error that says: 'Object reference not set to an
instance of an object'.

Public Class Inmate
Private strName As String
Private strSSN As String
Private strDOB As String
Private myMugshotPicture() As MugshotPicture
End Class

Public Class MugshotPicture
Private strPictureName As String
Private strDateTaken As String
End Class


As putting the code you provide in a class library project and compiling it
produces no errors, I'll guess you're getting the error when attempting to
reference myMugShotPicture from other code?
At the time of the attempt, have you populated the array with actual
MugshotPicture object variables? If not, that's your problem.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Yes Peter. The following calling code causes the problem:

myMugshotImageSearchResponse.myMugshotImage(0).str Picture = "test"

Do you know the correct way to do this?
Jul 21 '05 #6
Cam
> That's what I was thinking. I'm googling it right now but does anyone know
that syntax of the top of their head. Thanks for the help Cam!


My guess below, apologies in advance for syntax issues, I could write it in
c# for you ;)

Public Class Inmate
Private strName As String
Private strSSN As String
Private strDOB As String
Private myMugshotPicture() As MugshotPicture

Public Sub Inmate()
myMugshotPicture = new MugshotPicture()
End Sub
End Class
Jul 21 '05 #7
Cam
> Yes Peter. The following calling code causes the problem:

myMugshotImageSearchResponse.myMugshotImage(0).str Picture = "test"

Do you know the correct way to do this?


You'll need to create a MugshotPicture object first and then add it to the
array:

dim x as MugshotPicture = new MugshotPicture
x.strPicture = "test"
myMugshotImageSearchResponse.myMugshotImage.Add(x)

should do it I think!
Jul 21 '05 #8
Cor,

Thanks for the link. This link gave me the answer I was looking for.
Thanks everyone else for he help too. For others who may reference this
question in the future, my final code is as follows:

Public Class Inmate
Private strName As String
Private strSSN As String
Private strDOB As String
Private myMugshotPicture() As MugshotPicture
Default Public Property myMugshotImage(ByVal index As Integer) As
MugshotImage
Get
Return CType(List(index), MugshotImage)
End Get
Set(ByVal Value As MugshotImage)
List(index) = Value
End Set
End Property
Public Function Add(ByVal value As MugshotImage) As Integer
Return List.Add(value)
End Function 'Add
Public Sub Remove(ByVal Value As MugshotImage)
Me.List.Remove(Value)
End Sub
End Class

Public Class MugshotPicture
Private strPictureName As String
Private strDateTaken As String
End Class

Calling code is as follows:

Dim myMugshotImage As New MugshotImage
myMugshotImage.strPictureName = "test"

Dim myInmate as new Inmate
myInmate.Add(myMugshotImage)

MessageBox.Show(myInmate.myMugshotImage(0).strPict ureName)

"Cor Ligthert" wrote:
Clearic,

Have a look at this page, it has a complete (a little bit complex) sample in
it.

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps

Cor

Jul 21 '05 #9
Use

Private myMugshotPicture(12) As New MugshotPicture

You can specify the size later too with

Private myMugshotPicture() As MugshotPicture

---in some other function

myMugshotPicture=new MugshotPicture(10)

The New keyword actually provides a reference to an object.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #10
Is there a problem doing it this way?

Dim myMugshotImage As New MugshotImage
myMugshotImage.strPictureName = "test"

Dim myInmate as new Inmate
myInmate.Add(myMugshotImage)
"Ravichandran J.V." wrote:
Use

Private myMugshotPicture(12) As New MugshotPicture

You can specify the size later too with

Private myMugshotPicture() As MugshotPicture

---in some other function

myMugshotPicture=new MugshotPicture(10)

The New keyword actually provides a reference to an object.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 21 '05 #11

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

Similar topics

2
10841
by: Phil... | last post by:
I am trying to figure out how to create an array that contains objects and not references to objects. I know how to do the latter, but not the former. Any help is appreciated. I have the...
8
2995
by: Marco | last post by:
Hi all, I have a base class and some subclasses; I need to define an array of objects from these various subclasses. What I have is something like: { //I have a base class, something like:...
10
305
by: CLEAR-RCIC | last post by:
How would I create an array of objects inside a class? When I do it the following way I get an error that says: 'Object reference not set to an instance of an object'. Public Class Inmate...
4
1313
by: Miro | last post by:
Vb2003, im still learning vb.net but I do not understand my output from this logic. If someone can help me out here. Cor Ligthert, you I believe were on the right track of what Im trying to...
13
1982
by: Jack | last post by:
I have a class called "Base". This class has a protected member variable "m_base" which can be retrieved using the public member function "GetBaseMember". "m_base" is initialized to "1" and is...
23
7366
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
3
3191
by: Spoon | last post by:
Hello everyone, I want to create an array of objects at run-time. AFAIU, operator new will call the default constructor for each object in the array. In other words, the following program will...
8
4094
by: Chris Portka | last post by:
I need to be able to allocate large numbers of elements at a time but then delete them one at a time. I have settled on the design of using new to allocate many items at once, but am unsure what...
5
3618
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
0
7234
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
7344
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
7505
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...
0
5652
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,...
1
5060
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...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3216
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...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.