Results 1 to 6 of 6

Thread: {RESOLVED} Declaring Array in C#...

  1. #1

    Thread Starter
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524

    Resolved {RESOLVED} Declaring Array in C#...

    Hello...

    I'm confusing on how to declare array variable in C#...
    In VB, I used as below :
    Code:
    Dim myArray(10) as String
    Dim iL as Byte
    
    for iL = 0 to 9
        myArray(iL) = iL
    next iL
    how should I do the above code in C# ?

    Regards,
    ~WJ~
    Last edited by Wen Lie; Jul 20th, 2007 at 09:30 AM. Reason: Resolved....

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Declaring Array in C#...

    vb Code:
    1. String[] myArray = new String[10];

    that would be how you declare the array

  3. #3

    Thread Starter
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524

    Re: Declaring Array in C#...

    thanks.
    and to set the value ?

    Code:
    myArray[0] = "Test";
    Regards,
    ~WJ~

  4. #4
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Declaring Array in C#...

    Yes correct

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

    Re: Declaring Array in C#...

    Firstly, you should understand the difference between declaring a variable and creating an object. You are doing both in that code, but neither requires the other. This is a variable declaration:
    vb.net Code:
    1. Dim myArray As String()
    That creates a variable on the stack that can contain the memory address of a string array, but no array object is created on the heap. This is the same:
    vb.net Code:
    1. Dim myArray() As String()
    Whenever you specify a length for an array, either explicitly or implicitly, you are creating an object. These all do that:
    vb.net Code:
    1. Dim myArray1(10) As String
    2. Dim myArray2() As String = New String(10) {}
    3. Dim myArray3() As String = New String() {}
    4. Dim myArray4() As String = New String() {"string1", "string2"}
    You should also note that the code you posted is creating an array with 11 elements and then setting only 10 of them. In VB you specify the upper bound, not the length. If you want an array with 10 elements then you should specify 9 when creating the array. You're also assigning Bytes to the elements of a String array. The more appropriate code would be:
    vb.net Code:
    1. Dim length As Integer = 10
    2. Dim upperBound As Integer = length - 1
    3. Dim myArray(upperBound) As Byte
    4.  
    5. For b As Byte = 0 To upperBound Step 1
    6.     myArray(b) = b
    7. Next b
    In C# the equivalent would be:
    Code:
    int length = 10;
    byte myArray[] = new byte[length];
    
    for (byte b = 0; b < length; b++)
    {
        myArray[b] = b;
    }
    Note that in C# you DO specify the length when creating an array.
    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
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524

    Thumbs up Re: Declaring Array in C#...

    yup.
    it works.
    thanks for alll of your help.

    Regards,
    ~WJ~

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