Click to See Complete Forum and Search --> : Array of objects
rjv_rnjn
Apr 7th, 2006, 03:14 PM
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.
kleinma
Apr 7th, 2006, 03:24 PM
just declare an array variable of the type your class is...
for example lets say your class is call Class1
Dim MyArray(1) as Class1
MyArray(0) = New Class1
MyArray(1) = New Class1
rjv_rnjn
Apr 7th, 2006, 03:36 PM
Thanks for that @Kleinma.
But I think I didn't put my problem properly. The initialisation works fine but when I do this 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.
francisstokes
Apr 7th, 2006, 03:40 PM
this might be irrelevent but i thought you had to use standard brackets in VB. the () brackets:
myarry(0).property1
kleinma
Apr 7th, 2006, 03:52 PM
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
kleinma
Apr 7th, 2006, 03:53 PM
this might be irrelevent but i thought you had to use standard brackets in VB. the () brackets:
myarry(0).property1
yeah you do, I'm guessing he just used [] in his post, as using them in the IDE would cause a syntax error
rjv_rnjn
Apr 7th, 2006, 03:57 PM
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. :)
francisstokes
Apr 7th, 2006, 04:05 PM
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):
Dim mypicboxes(4) As New PictureBox
mypicboxes(0).Image = mypicboxes.Image.FromFile("1stpic.jpg")
mypicboxes(1).Image = mypicboxes.Image.FromFile("2ndpic.jpg")
...
rjv_rnjn
Apr 7th, 2006, 04:11 PM
Got it. I instantiated each object of the array with new operator. Sorry couldn't derive the meaning early.
I've donefor (int j=0; j <= objFundPerf.GetUpperBound(0); j++)
{
objFundPerf[j] = new FundPerf();
}
Sorry folks for putting C# code here. :)
Cheers!
francisstokes
Apr 7th, 2006, 04:13 PM
I dont know C#....sorry.
rjv_rnjn
Apr 7th, 2006, 04:14 PM
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.
kleinma
Apr 7th, 2006, 04:17 PM
I'm going to move it anyway
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.