Results 1 to 6 of 6

Thread: [RESOLVED] Creating arrays.

  1. #1

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

    Resolved [RESOLVED] Creating arrays.

    I am still struggling with understanding the creation of objects.
    For example.
    int[] x = {4,5,6};
    int[] y = new int[x.Length];

    Why is it that for the first array, we can initialize it without the use of the new keyword yet in the second line, we need the new keyword?
    Whats the difference?
    Thanks so much.

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

    Re: Creating arrays.

    The first line intialises the value of each element of the array, so you're allowed to use that shorthand. The second line creates an Array object but doesn't intialise any of the elements. If you're not setting a value for any of the elements you certainly can't create the array in any way other than by invoking the constructor.
    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: Creating arrays.

    Assigning the values right away tells VS to allocate the memory automatically and fill those indexes. In the second line, the array is created to hold a reference to a location in memory by telling what size it has, but it has no data to fill it with yet. So the first line is a short hand way of doing line two. (newbie gibberish)
    Anyhows, a long version of the first line would be:
    int[] x = new int[3];
    int[0] = 4;
    int[1] = 5;
    int[2] = 6;

    Is all of this description somewhere in the ballpark?

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: Creating arrays.

    Your situation determines the creation of the array . Sometimes you want to create it predefined with values (ex. first line ) , or you can just create memory spaces for your array but let fill in them later after the creation..

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

    Re: Creating arrays.

    Quote Originally Posted by gjon
    Assigning the values right away tells VS to allocate the memory automatically and fill those indexes. In the second line, the array is created to hold a reference to a location in memory by telling what size it has, but it has no data to fill it with yet. So the first line is a short hand way of doing line two. (newbie gibberish)
    Anyhows, a long version of the first line would be:
    int[] x = new int[3];
    int[0] = 4;
    int[1] = 5;
    int[2] = 6;

    Is all of this description somewhere in the ballpark?
    Basically yes. The first line says "create an array containing the integers 4, 5 and 6 and assign that array to the variable". The second line says "create an array of the appropriate size to contain 3 integers and assign that array to the variable". The only difference in what happens behind the scenes is that the first method initialises the elements of the array. The fact that the code looks relatively different is a function of the C# language and how it attempts to simplify your life when you're initialising the elements of an array. It doesn't mean that your compiled application works any differently.
    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

  6. #6

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

    Re: [RESOLVED] Creating arrays.

    Thank you, that's the understanding I need to learn as well sometimes. Not just what code does what, but what is actually happening behind the scenes. That just makes more sense to me. I hope practicing it like that will give me a better understanding. or else, I'm just a goofy goober. the latter, more likely...
    Thanks again.

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