Results 1 to 12 of 12

Thread: Array of objects

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    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.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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:
    1. Dim MyArray(1) as Class1
    2.  
    3. MyArray(0) = New Class1
    4. MyArray(1) = New Class1

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    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:
    1. objMyClass[0].Property1 = value
    2. 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.

  4. #4
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: Array of objects

    this might be irrelevent but i thought you had to use standard brackets in VB. the () brackets:

    VB Code:
    1. myarry(0).property1

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Array of objects

    Quote Originally Posted by francisstokes
    this might be irrelevent but i thought you had to use standard brackets in VB. the () brackets:

    VB Code:
    1. 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

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Array of objects

    Quote 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.

  8. #8
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    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:
    1. Dim mypicboxes(4) As New PictureBox
    2.  
    3. mypicboxes(0).Image = mypicboxes.Image.FromFile("1stpic.jpg")
    4. mypicboxes(1).Image = mypicboxes.Image.FromFile("2ndpic.jpg")
    5. ...

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    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:
    1. for (int j=0; j <= objFundPerf.GetUpperBound(0); j++)
    2.             {
    3.                 objFundPerf[j] = new FundPerf();
    4.             }
    Sorry folks for putting C# code here.
    Cheers!

  10. #10
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: Array of objects

    I dont know C#....sorry.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    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.

  12. #12
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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
  •  



Click Here to Expand Forum to Full Width