11 Replies - 895 Views - Last Post: 19 July 2011 - 11:06 AM Rate Topic: -----

#1 lwstory   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 29-October 07

Making An Array Of A Class

Posted 29 October 2007 - 03: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.

Is This A Good Question/Topic? 0
  • +

Replies To: Making An Array Of A Class

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5315
  • View blog
  • Posts: 14,184
  • Joined: 18-April 07

Re: Making An Array Of A Class

Posted 29 October 2007 - 03:53 PM

Are you using visual basic 6 or .NET? Sounds like .NET but I just wanted to make sure before providing you an example. Thanks. :)
Was This Post Helpful? 1
  • +
  • -

#3 lwstory   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 29-October 07

Re: Making An Array Of A Class

Posted 29 October 2007 - 03:54 PM

Im using .net
Was This Post Helpful? 0
  • +
  • -

#45 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5315
  • View blog
  • Posts: 14,184
  • Joined: 18-April 07

Re: Making An Array Of A Class

Posted 29 October 2007 - 04: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.

' 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 - 04:09 PM

Was This Post Helpful? 2

#46 lwstory   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 29-October 07

Re: Making An Array Of A Class

Posted 29 October 2007 - 04:12 PM

AWESOME! Thank you very much! :D :^:
Was This Post Helpful? 1
  • +
  • -

#47 denniswong288   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-May 09

Re: Making An Array Of A Class

Posted 12 May 2009 - 08: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! :)






View PostMartyr2, 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.

' 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! :)

Was This Post Helpful? 0
  • +
  • -

#48 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5315
  • View blog
  • Posts: 14,184
  • Joined: 18-April 07

Re: Making An Array Of A Class

Posted 11 June 2009 - 06:20 PM

View Poststaffsguy50, on 6 Jun, 2009 - 02:34 AM, said:

nice but you need to put
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. :)
Was This Post Helpful? 0
  • +
  • -

#49 Cookiesliyr   User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 16-May 09

Re: Making An Array Of A Class

Posted 12 June 2009 - 10:15 AM

u guys mean array of objects i think ... cause in Java u can make an array of classes (don't asks me why) i think it is easier to overwrite them in that way.
Was This Post Helpful? 0
  • +
  • -

#50 donvdp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-June 09

Re: Making An Array Of A Class

Posted 19 June 2009 - 01:07 AM

Then, what would be the correct wat of doing this in VB6 ?

because i have tried this example in vb but it gives an error at compile time.
Was This Post Helpful? 0
  • +
  • -

#51 andersonwl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-July 11

Re: Making An Array Of A Class

Posted 19 July 2011 - 09:43 AM

If you are using .Net then you might want to try using "List Of" instead of Arrays. Here's an example of how they work:


'Define the layout for your items
Public Structure Person
   Dim LastName as String
   Dim FirstName as String
   Dim Address as String
   Dim City as String
   Dim State as String
   Dim Zip as String
End Structure

'Create a List to contain the items 
Dim Persons as List Of(Person) 

'Create a new item and populate it. 
Dim p as New Person
p.FirstName = "John"
p.LastName = "Smith"
p.Address = "123 Any St
p.City = "Dallas"
P.St = "TX"
p.Zip = "12345"

'Add it to the list
Persons.Add p 

'Repeat this for the rest of your items. 

'Loop thru the list and display each

For each p in Persons
   Debug.Print p.FirstName & vbTab & p.LastName & p.Address ' And so on....
Next



To see how many items are in the list use Persons.Count
Was This Post Helpful? 0
  • +
  • -

#52 andersonwl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-July 11

Re: Making An Array Of A Class

Posted 19 July 2011 - 10:09 AM

If you are using .Net then you might want to try using "List Of" instead of Arrays.

Here's an example of how they work:
'Define the layout for your items
Dim Structure Person
   Dim LastName as String
   Dim FirstName as String
   Dim Address as String
   Dim City as String
   Dim State as String
   Dim Zip as String
End Structure

'Create a List to contain the items 
Dim Persons as New List(Of Person) 

'Create a new item and populate it. 
Dim p as New Person
p.FirstName = "John"
p.LastName = "Smith"
p.Address = "123 Any St
p.City = "Dallas"
P.St = "TX"
p.Zip = "12345"

'Add it to the list
Persons.Add p 

'Repeat this for the rest of your items. 

'To loop thru the list and display each use one of these For Loops. 
For each p in Persons
   Debug.Print p.FirstName & vbTab & p.LastName & p.Address ' And so on....
Next

For x = 0 to Persons.Count -1 ' Index srarts as 0
  Debug.Print Persons.Item(x).FirstName & vbTab & Persons.Item(x).LastName & p.Address ' And so on....
Next 

'To find and remove a specific item to this
Dim ndx as Integer = 0
Dim NameToFind as String = "John Smith" 
For x = 0 to Persons.Count -1
   If Persons.Item(x).FirstName & " " & Persons.Item(x).LastName = NameToFind Then
      ndx = x
      Exit For
   End If
   ndx = -1 ' Name not found
Next

If ndx >= 0 then
   Persons.Item(ndx).remove
End If


This works great for single items too. Click here to see the Microsoft definition including an example for single item or 1-diminsional arrays.
Was This Post Helpful? 0
  • +
  • -

#53 chls89   User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 47
  • Joined: 19-September 09

Re: Making An Array Of A Class

Posted 19 July 2011 - 11:06 AM

Not sure this warrants an article :) I've seen Martyr2 say some much, much more interesting things :) Nevertheless, I've not understood why there is no functionality to create an instantiated array. I'm sure it was a design decision. Maybe 'they' didn't want people ruining all performance subtleties by just throw around memory-allocated arrays like it's the cool thing to do. "I'll just create an instantiated array of <massiveobjects> with 100000 elements JUST IN CASE." :P

Semi-apologies for the irrelevant post, it's usually not my practice do to so.

View Postdenniswong288, on 12 May 2009 - 09:07 PM, said:

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! :)






View PostMartyr2, 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.

' 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! :)

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1