School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,136 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,914 people online right now. Registration is fast and FREE... Join Now!



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? Rate Topic: -----

#1 lwstory  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 10
  • Joined: 29-October 07


Dream Kudos: 0

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.
Was This Post Helpful? 0
  • +
  • -


#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 7,676
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Posted 29 October 2007 - 02: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? 0
  • +
  • -

#3 lwstory  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 10
  • Joined: 29-October 07


Dream Kudos: 0

Posted 29 October 2007 - 02:54 PM

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

#45 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 7,676
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

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.

' 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

Was This Post Helpful? 1

#46 lwstory  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 10
  • Joined: 29-October 07


Dream Kudos: 0

Posted 29 October 2007 - 03:12 PM

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

#47 denniswong288  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 1
  • Joined: 12-May 09


Dream Kudos: 0

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






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 staffsguy50  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 1
  • Joined: 06-June 09


Dream Kudos: 0

Posted 06 June 2009 - 02:34 AM

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
Was This Post Helpful? 0
  • +
  • -

#49 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 7,676
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Posted 11 June 2009 - 05: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
  • +
  • -

#50 Cookiesliyr  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 108
  • Joined: 16-May 09


Dream Kudos: 0

Posted 12 June 2009 - 09: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
  • +
  • -

#51 donvdp  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 1
  • Joined: 19-June 09


Dream Kudos: 0

Posted 19 June 2009 - 12: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
  • +
  • -

Page 1 of 1


Fast Reply

  

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month