Hello...
I'm confusing on how to declare array variable in C#...
In VB, I used as below :
how should I do the above code in C# ?Code:Dim myArray(10) as String
Dim iL as Byte
for iL = 0 to 9
myArray(iL) = iL
next iL
Regards,
~WJ~
Printable View
Hello...
I'm confusing on how to declare array variable in C#...
In VB, I used as below :
how should I do the above code in C# ?Code:Dim myArray(10) as String
Dim iL as Byte
for iL = 0 to 9
myArray(iL) = iL
next iL
Regards,
~WJ~
vb Code:
String[] myArray = new String[10];
that would be how you declare the array :)
thanks.
and to set the value ?
Regards,Code:myArray[0] = "Test";
~WJ~
Yes correct :)
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: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:
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:
Dim myArray() As String()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:
Dim myArray1(10) As String Dim myArray2() As String = New String(10) {} Dim myArray3() As String = New String() {} Dim myArray4() As String = New String() {"string1", "string2"}In C# the equivalent would be:vb.net Code:
Dim length As Integer = 10 Dim upperBound As Integer = length - 1 Dim myArray(upperBound) As Byte For b As Byte = 0 To upperBound Step 1 myArray(b) = b Next bNote that in C# you DO specify the length when creating an array.Code:int length = 10;
byte myArray[] = new byte[length];
for (byte b = 0; b < length; b++)
{
myArray[b] = b;
}
yup.
it works.
thanks for alll of your help.
Regards,
~WJ~