VB.Net


In VB.Net the number inside the bracket represents the index number of the last element.
When isize = 5, there are 6 elements, myArray(0) to myArray(5)

Dim myArray(5) As Long 

Dim isize As Integer = 5
Dim myArray(isize) As Long

You cannot specify a dimension on the array type in VB.Net.
In VB.Net you can use curly brackets and these can come after the type or after the variable
This is very confusing because if you are declaring a fixed-size array then you must put the curly brackets after the variable.

Dim myArray() As Long 
Dim myArray As Long()

In VB.Net it is good practice to include "New" when declaring and initialising an array on one line.
You can only use "New" when initiating with curly brackets.

Dim myArray() As String = New String() {"one","two","three"} 
Dim myArray() As String = {"one","two","three"}
Dim myArray(2) As String = {"one","two","three"} ??

Option Base 1

In .NET all arrays start at zero. In VBA you can use the Option Base 1 to start your arrays from 1 instead but this is not possible in .NET.


The following line will not work in C#

int[] 

The following line will not work in VB

Dim myarray(1 To 10) As Integer 

You will have to make all your indexes zero based
To get the total number of elements use ArrayName.Length
Replace LBound and UBound with GetUpperBound(0) and GetLength(0) - 1
In .NET an array variable is a Reference type


Declaration and Creation

The following 2 lines are identical in VB.NET

Dim myarray() As String 
Dim myarray As String()

The following lines are all allowed

Dim myarray(6) As Integer 
Dim myarray() As Integer = New Integer() {}
Dim myarray() As Integer = {1,2,3,4,5}
Dim myarray() As Integer = New Integer() {1,2,3,4,5}

Declaration and creation can be done in 2 steps or combined into 1 line
Values can be intialised or left as the defaults


Rank

One of the problems with VB was that using the ReDim statement allowed the dimesions of an array to be changed.
This created inefficient code because it always had to check how many dimensions the array had before accessing any of its elements.
To create more efficient code VB.NET introduces the concept of a rank, which is the number dimensions in the array.
VB.NET allows you to change the number of items in an array but it does not let you change the rank of the array.
You specify the rank (i.e. the number of dimensions) when you declare the array.

Dim aiArrayIntegers() As Integer 
Dim aiArrayIntegers(,) As Integer
Dim aiArrayIntegers(,,) As Integer



© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrev