By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

array of objects inside a class

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.