Welcome to Dream.In.Code
Click Here
Getting VB.NET Help is Easy!

Join 117,617 VB.NET Programmers for FREE! Ask your question and get quick answers from experts. There are 1,958 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Making an array of a class

 
Reply to this topicStart new topic

Making an array of a class, Is it possible to make an array of a class in vb?

lwstory
post 29 Oct, 2007 - 03:44 PM
Post #1


New D.I.C Head

*
Joined: 29 Oct, 2007
Posts: 10


My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page


Martyr2
post 29 Oct, 2007 - 03:53 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 4,613



Thanked 116 times

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

My Contributions


Are you using visual basic 6 or .NET? Sounds like .NET but I just wanted to make sure before providing you an example. Thanks. smile.gif
User is online!Profile CardPM

Go to the top of the page

lwstory
post 29 Oct, 2007 - 03:54 PM
Post #3


New D.I.C Head

*
Joined: 29 Oct, 2007
Posts: 10


My Contributions


Im using .net
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 29 Oct, 2007 - 04:08 PM
Post #4


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 4,613



Thanked 116 times

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

My Contributions


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.

CODE

' 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! smile.gif

This post has been edited by Martyr2: 29 Oct, 2007 - 04:09 PM
User is online!Profile CardPM

Go to the top of the page

lwstory
post 29 Oct, 2007 - 04:12 PM
Post #5


New D.I.C Head

*
Joined: 29 Oct, 2007
Posts: 10


My Contributions


AWESOME! Thank you very much! biggrin.gif icon_up.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/8/08 12:01AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month