VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 300,326 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,967 people online right now. Registration is fast and FREE... Join Now!




Making an array of a class

 

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

lwstory

29 Oct, 2007 - 02:44 PM
Post #1

New D.I.C Head
*

Joined: 29 Oct, 2007
Posts: 10



Thanked: 1 times
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
+Quote Post


Martyr2

RE: Making An Array Of A Class

29 Oct, 2007 - 02:53 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,243



Thanked: 820 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
+Quote Post

lwstory

RE: Making An Array Of A Class

29 Oct, 2007 - 02:54 PM
Post #3

New D.I.C Head
*

Joined: 29 Oct, 2007
Posts: 10



Thanked: 1 times
My Contributions
Im using .net
User is offlineProfile CardPM
+Quote Post

Martyr2

RE: Making An Array Of A Class

29 Oct, 2007 - 03:08 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,243



Thanked: 820 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 - 03:09 PM
User is online!Profile CardPM
+Quote Post

lwstory

RE: Making An Array Of A Class

29 Oct, 2007 - 03:12 PM
Post #5

New D.I.C Head
*

Joined: 29 Oct, 2007
Posts: 10



Thanked: 1 times
My Contributions
AWESOME! Thank you very much! biggrin.gif icon_up.gif
User is offlineProfile CardPM
+Quote Post

denniswong288

RE: Making An Array Of A Class

12 May, 2009 - 07:07 PM
Post #6

New D.I.C Head
*

Joined: 12 May, 2009
Posts: 1

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






QUOTE(Martyr2 @ 29 Oct, 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.

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


User is offlineProfile CardPM
+Quote Post

staffsguy50

RE: Making An Array Of A Class

6 Jun, 2009 - 02:34 AM
Post #7

New D.I.C Head
*

Joined: 6 Jun, 2009
Posts: 1

nice but you need to put
CODE
Dim mycreature As creatures = New creatures()


outside the fore next loop or it will dump the values every time it loops

User is offlineProfile CardPM
+Quote Post

Martyr2

RE: Making An Array Of A Class

11 Jun, 2009 - 05:20 PM
Post #8

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,243



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

My Contributions
QUOTE(staffsguy50 @ 6 Jun, 2009 - 02:34 AM) *

nice but you need to put
CODE
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. smile.gif
User is online!Profile CardPM
+Quote Post

Cookiesliyr

RE: Making An Array Of A Class

12 Jun, 2009 - 09:15 AM
Post #9

D.I.C Head
**

Joined: 16 May, 2009
Posts: 102



Thanked: 6 times
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

donvdp

RE: Making An Array Of A Class

19 Jun, 2009 - 12:07 AM
Post #10

New D.I.C Head
*

Joined: 19 Jun, 2009
Posts: 1

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

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 03:02PM

Live VB.NET Help!

Be Social

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

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month