Results 1 to 4 of 4

Thread: How do you put objects into an array?

  1. #1

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    How do you put objects into an array?

    If I had a couple regions that had multiple stores each. Is there a way to put these store objects into an array of a region?
    I am trying things like:
    VB Code:
    1. static int size = 3;
    2.         static Region[] regions = new Region[size];
    3.        
    4.         static void Main(string[] args)
    5.         {
    6.             //for (int k = 0; k < size; k++)
    7.             //    regions[k] = new Region();
    8.  
    9.             double[] stores = new double[5];
    10.  
    11.             for (int k = 0; k < 5; k++)
    12.                 stores[k] = IO.GetDouble("Enter the week's sales for store " + (k+1) + ": ");
    13.  
    14.             regions[0] = stores;
    with no luck. Thanks in advance for any help.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do you put objects into an array?

    Your 'stores' variable is a double array. You 'regions' variable is a Region array. You can't assign a double array to an element of a Region array. You can only assign a Region to an element of a Region array. If you want an array to hold an array of doubles then you need to declare a variable and create an array of that type:
    Code:
    static double[][] regions = new double[5][];
    Now you have an array that can hold five double arrays.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: How do you put objects into an array?

    static double[][] regions = new double[5][];

    So, to have 3 regions with 5 store sales shown would look something like?

    regions = {{$200, $350, $450, $300, $250}, {$200, $350, $450, $300, $250}, {$200, $350, $450, $300, $250}};

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do you put objects into an array?

    The code I posted creates a jagged array, which is an array of arrays. Specifically it creates an array that can contain 5 double arrays. What you've shown above is an array that contains 3 double arrays. The number specified is the number of arrays in the outer array, not the number of elements in the inner arrays. Not also that the reason that it's called a jagged array is because there is no requirement for each inner array to be the same same length. If what you actually want is a matrix where each element is a peer and all "rows" are the same length then you can use a two-dimensional array, which is different:
    Code:
    static double[,] regions = new double[3,5];
    That now creates a two-dimensional array with 3 elements in one direction and 5 in the other. I'm not going to use the term rows and columns because there is no reason that any particular dimension is the rows and the other is the columns. Also, you can have arrays with many more dimensions than two if you want. Note that a two dimensional array is NOT the same as a jagged array. A jagged array is a one-dimensional outer array where each element is a one-dimensional array. A two-dimensional array is a single matrix where each element is a peer.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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