|
-
Apr 7th, 2006, 03:14 PM
#1
Thread Starter
Fanatic Member
Array of objects
Hi,
I've created a class with two properties. Now I want to create an array of that class and then retrieve the values later. Any idea how to do it? I tried searching for it but couldn't find a proper article on this. Any links would be helpful as well.
Thanks.
-
Apr 7th, 2006, 03:24 PM
#2
Re: Array of objects
just declare an array variable of the type your class is...
for example lets say your class is call Class1
VB Code:
Dim MyArray(1) as Class1
MyArray(0) = New Class1
MyArray(1) = New Class1
-
Apr 7th, 2006, 03:36 PM
#3
Thread Starter
Fanatic Member
Re: Array of objects
Thanks for that @Kleinma.
But I think I didn't put my problem properly. The initialisation works fine but when I do this
VB Code:
objMyClass[0].Property1 = value
objMyClass[0].Property2 = value
The code breaks while trying to do the first assignment saying "Object Reference not set to an instance of an object." Hence I thought there is some problem with declaration.
Thanks.
-
Apr 7th, 2006, 03:40 PM
#4
Hyperactive Member
Re: Array of objects
this might be irrelevent but i thought you had to use standard brackets in VB. the () brackets:
-
Apr 7th, 2006, 03:52 PM
#5
Re: Array of objects
each object in the array needs to be property instanciated with the New operator (or assinged to an existing object) before it can be used
-
Apr 7th, 2006, 03:53 PM
#6
Re: Array of objects
 Originally Posted by francisstokes
this might be irrelevent but i thought you had to use standard brackets in VB. the () brackets:
yeah you do, I'm guessing he just used [] in his post, as using them in the IDE would cause a syntax error
-
Apr 7th, 2006, 03:57 PM
#7
Thread Starter
Fanatic Member
Re: Array of objects
 Originally Posted by kleinma
each object in the array needs to be property instanciated with the New operator (or assinged to an existing object) before it can be used
Didn't get you completely?
Actually, I'm currently using C# but have always refered to this site in times of trouble hence thought of posting it here. Hope you people don't mind.
-
Apr 7th, 2006, 04:05 PM
#8
Hyperactive Member
Re: Array of objects
There is a C# forum here. Scroll down on the main page to find it.
What kleinma means is that you basicly have to create th array before you can use it so (for example purposes ill use pictureboxes):
VB Code:
Dim mypicboxes(4) As New PictureBox
mypicboxes(0).Image = mypicboxes.Image.FromFile("1stpic.jpg")
mypicboxes(1).Image = mypicboxes.Image.FromFile("2ndpic.jpg")
...
-
Apr 7th, 2006, 04:11 PM
#9
Thread Starter
Fanatic Member
Re: Array of objects
Got it. I instantiated each object of the array with new operator. Sorry couldn't derive the meaning early.
I've done
VB Code:
for (int j=0; j <= objFundPerf.GetUpperBound(0); j++)
{
objFundPerf[j] = new FundPerf();
}
Sorry folks for putting C# code here. 
Cheers!
-
Apr 7th, 2006, 04:13 PM
#10
Hyperactive Member
-
Apr 7th, 2006, 04:14 PM
#11
Thread Starter
Fanatic Member
Re: Array of objects
there is nothing very dynamically different between C# and VB.Net. I started with VB.Net but the current project requires me to use c#. Also, the reason for me posting things here is that this site attracts more hits than the C# forum here.
-
Apr 7th, 2006, 04:17 PM
#12
Re: Array of objects
I'm going to move it anyway
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|