VB.NET question
-
In C# I can do the following:
int nSize = 24; object[] pObjArray = new object[nSize];
How do I do the same in VB.NET? Thanks. -
In C# I can do the following:
int nSize = 24; object[] pObjArray = new object[nSize];
How do I do the same in VB.NET? Thanks.Integer nSize = 24 Object() pObjArray = new Object() {nSize}
-
In C# I can do the following:
int nSize = 24; object[] pObjArray = new object[nSize];
How do I do the same in VB.NET? Thanks. -
Integer nSize = 24 Object() pObjArray = new Object() {nSize}
Wooster2006 wrote:
Integer nSize = 24 Object() pObjArray = new Object() {nSize}
This is not correct. This creates an array containing 1 element whose value is 24.
-
Dim nSize As Integer = 24 Dim pObjArray(nSize) As Object '// And to test it: pObjArray(24) = "Test" MsgBox(CType(pObjArray(24), String))
There you go! ;) -- modified at 11:38 Tuesday 23rd May, 2006j-on wrote:
Dim nSize As Integer = 24 Dim pObjArray(nSize) As Object
Not quite right. This array contains 25 elements. Not 24. In VB.NET, the value that you use to set the array size does not represent the "Capacity" of the array like how it's done in C#. It represents the max "Index" of the array. And remember that arrays in VB.NET are zero based. So, if you count the elements from 0 to 24, you have a total of 25 elements. What you want to do is this instead.
Dim nSize As Integer = 24 Dim pObjArray(nSize - 1) As Object
This is one of the major difference between C# and VB.NET's handling of arrays. -- modified at 18:51 Tuesday 23rd May, 2006 -
In C# I can do the following:
int nSize = 24; object[] pObjArray = new object[nSize];
How do I do the same in VB.NET? Thanks.Hello Blue Bird. The first 2 responses that you received were not quite correct. Please look at my responses to them. The following C# code...
int nSize = 24;
object[] pObjArray = new object[nSize];Will look like this in VB.NET...
Dim nSize As Integer = 24
Dim pObjArray(nSize - 1) As ObjectThe big difference here is that the array initializer in VB.NET represents the largest index (ie: upper bound) and not the capacity of the array. That's why you should tack on a "-1" as shown above. You can also generate an array like this via the "ReDim" statement.
Dim pObjArray() As Object ' Array not created yet. ReDim pObjArray(nSize - 1) ' This create a new array. ReDim pObjArray(1) ' This creates a new array having 2 elements.
You can also create an array and initialize it at the same time like this...Dim pObjArray() As Object = {1, 2, 3}
Or like this...Dim pObjArray() As Object pObjArray = New Object() {1, 2, 3}
I hope this helps! :) -
In C# I can do the following:
int nSize = 24; object[] pObjArray = new object[nSize];
How do I do the same in VB.NET? Thanks.Our Instant VB C# to VB.NET converter produces: Dim nSize As Integer = 24 Dim pObjArray As Object() = New Object(nSize - 1){} David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C# to C++ converter and VB to C++ converter Instant J#: VB to J# converter Clear VB: Cleans up VB.NET code Clear C#: Cleans up C# code