Making an array of a class
Page 1 of 1
Making an array of a class Is it possible to make an array of a class in vb?
#1
Posted 29 October 2007 - 02:44 PM
I was wondering if it was possible to make an array of a class. I made a class called creatures (for a game of course) and wanted to make an array of that class. So I tried "public creature() as new creatures", and it had a problem with using an array with new. Is there a way around this. All I am using the class for is variables. i.e.: in my class all i have is dim health, defense,... as integer and dim name as string and no get commands because I forgot how to use them. Also, I'm not sure if there is some other way i could go about doing this.
#45
Posted 29 October 2007 - 03:08 PM
Ok, here is a good sample for you using your creatures. I created a class called "creatures" and filled the array of creatures with 10 creatures named MonsterMan0 - MonsterMan9. Just read the comments and follow along.
I hope you get the idea now. We simply create an array of creatures and then we create a new creature in our loop and store it in the next slot of our array. At the end we have an array of ten creatures starting in slot 0 through slot 9. Then we can get at each creature by using the array's index like I did in the messagebox.
Btw, in the future put your .NET questions in the VB.NET forum. This forum is dedicated to previous versions of VB (4/5/6). Thanks!
Enjoy!
' Create an array of creature classes
Dim creatureArray(10) As creatures
Dim i As Integer = 0
' Load up the array with 10 creatures named MonsterMan0 - MonsterMan9
For i = 0 To 9
Dim mycreature As creatures = New creatures()
' Set his name and his age (all our monsters are clones, so they have the same age! (you could assign whatever data you wanted)
mycreature.name = "MonsterMan" & i.ToString
mycreature.age = 100
' Store our copy of the creature into the array of creatures
creatureArray(i) = mycreature
Next
' Show the name of the 5th monster who is in slot 4 because the array starts at 0, so his name is MonsterMan4
MessageBox.Show("The 5th monster is named: " & creatureArray(4).name)
I hope you get the idea now. We simply create an array of creatures and then we create a new creature in our loop and store it in the next slot of our array. At the end we have an array of ten creatures starting in slot 0 through slot 9. Then we can get at each creature by using the array's index like I did in the messagebox.
Btw, in the future put your .NET questions in the VB.NET forum. This forum is dedicated to previous versions of VB (4/5/6). Thanks!
Enjoy!
This post has been edited by Martyr2: 29 October 2007 - 03:09 PM
#47
Posted 12 May 2009 - 07:07 PM
Hi Martyr2,
This is truly excellent!!
I'd been searching for this for a long time, and finally found it.
Just really curious why there's no such articles on this.
I strongly suggest that you put this up on your blog, or contribute to someone's blog, so it's an article/tutorial, instead of just a forum posting.
Thanks once again!
This is truly excellent!!
I'd been searching for this for a long time, and finally found it.
Just really curious why there's no such articles on this.
I strongly suggest that you put this up on your blog, or contribute to someone's blog, so it's an article/tutorial, instead of just a forum posting.
Thanks once again!
Martyr2, on 29 Oct, 2007 - 03:08 PM, said:
Ok, here is a good sample for you using your creatures. I created a class called "creatures" and filled the array of creatures with 10 creatures named MonsterMan0 - MonsterMan9. Just read the comments and follow along.
I hope you get the idea now. We simply create an array of creatures and then we create a new creature in our loop and store it in the next slot of our array. At the end we have an array of ten creatures starting in slot 0 through slot 9. Then we can get at each creature by using the array's index like I did in the messagebox.
Btw, in the future put your .NET questions in the VB.NET forum. This forum is dedicated to previous versions of VB (4/5/6). Thanks!
Enjoy!
' Create an array of creature classes
Dim creatureArray(10) As creatures
Dim i As Integer = 0
' Load up the array with 10 creatures named MonsterMan0 - MonsterMan9
For i = 0 To 9
Dim mycreature As creatures = New creatures()
' Set his name and his age (all our monsters are clones, so they have the same age! (you could assign whatever data you wanted)
mycreature.name = "MonsterMan" & i.ToString
mycreature.age = 100
' Store our copy of the creature into the array of creatures
creatureArray(i) = mycreature
Next
' Show the name of the 5th monster who is in slot 4 because the array starts at 0, so his name is MonsterMan4
MessageBox.Show("The 5th monster is named: " & creatureArray(4).name)
I hope you get the idea now. We simply create an array of creatures and then we create a new creature in our loop and store it in the next slot of our array. At the end we have an array of ten creatures starting in slot 0 through slot 9. Then we can get at each creature by using the array's index like I did in the messagebox.
Btw, in the future put your .NET questions in the VB.NET forum. This forum is dedicated to previous versions of VB (4/5/6). Thanks!
Enjoy!
#49
Posted 11 June 2009 - 05:20 PM
staffsguy50, on 6 Jun, 2009 - 02:34 AM, said:
nice but you need to put
outside the fore next loop or it will dump the values every time it loops
Dim mycreature As creatures = New creatures()
outside the fore next loop or it will dump the values every time it loops
No actually it won't. You are creating a creatures class each time and then storing it in the array (which is outside the for loop). We are creating multiple creatures objects here and stashing it in the array.
This is setup just fine.
Page 1 of 1

Add Reply




MultiQuote

| 


