array of objects inside a class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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.
 
CLEAR-RCIC said:
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.
 
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!
 
Peter van der Goes said:
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.
Yes Peter. The following calling code causes the problem:

myMugshotImageSearchResponse.myMugshotImage(0).strPicture = "test"

Do you know the correct way to do this?
 
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
 
Yes Peter. The following calling code causes the problem:
myMugshotImageSearchResponse.myMugshotImage(0).strPicture = "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!
 
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).strPictureName)
 
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
 
Is there a problem doing it this way?

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

Dim myInmate as new Inmate
myInmate.Add(myMugshotImage)
 
Back
Top