Lower Bounds not 0 in VB.NET

14. June 2006 17:31 by Mrojas in General  //  Tags:   //   Comments (0)
The other day a friend asked me. How could he created an array in VB with a lower bound different from 0.

In VB6 you could do something just like this:

Dim b(6 To 12) As Integer 

But in VB.NET it does not work that way. The LowerBound in VB.NET must be always 0. But there is a way to have a lower bound different from 0. It is unclear why this is not very documented but the following code shows how to do it: 

Dim b As Array = Array.CreateInstance(GetType(String), New Integer() {6}, New Integer() {6})

b(6) = 20

b(7) = 1

 

I received this comment:

Hi Mauricio RojaI am converting the VB6 programs to VB.Net2008. When I declare the array in VB.Net the lower bound always "0". When I searched internet got some clue from you. Thanks.Could you please help me to solve the array problem. I have declared a array as "public socketbins(1 to DUT, 1 to SOCK, 1 to 1000) as CBin" in VB6. When I convert this declaration to VB.Net2008 it is not accepted. can you please tell me how to declare the above declaration in VB.Net2008Thanks & RegardsUdhay

And this is my answer:

 

Hi.

Sorry for my late response.

I don’t know what you mean for CBin

But let's says the array type is byte

Then you can do this:

Dim socketBins4 = Array.CreateInstance(GetType(Byte), New Integer() {DUT, SOCK, 1000}, New Integer() {1, 1, 1})

And I am sending some code for your tests

 

Module Module1

Const DUT = 100

Const SOCK = 100

'taken and modified from http://blogs.msdn.com/vbfaq/archive/2004/04/20/116660.aspx

Public Class VBArray(Of T)

Private _lbound As Integer

Private _myitems() As T

Public Sub New(ByVal LBound As Integer, ByVal UBound As Integer)

Me.ReDim(LBound, UBound)

End Sub

Public Sub [ReDim](ByVal LBound As Integer, ByVal UBound As Integer)

_lbound = LBound

ReDim _myitems(UBound - LBound + 1)

End Sub

Default Public Property Item(ByVal Index As Integer) As T

Get

Return _myitems(Index - _lbound)

End Get

Set(ByVal Value As T)

_myitems(Index - _lbound) = Value

End Set

End Property

Public Function ToArray() As T()

Return _myitems

End Function

End Class

Sub Main()

 

Dim socketBins4 = Array.CreateInstance(GetType(Byte), New Integer() {DUT, SOCK, 1000}, New Integer() {1, 1, 1})

Dim arrayLenghts() As Integer = {DUT, SOCK, 1000}

Dim arrayLowerBounds() As Integer = {1, 1, 1}

Dim socketBins3 = Array.CreateInstance(GetType(Byte), arrayLenghts, arrayLowerBounds)

socketBins3(1, 1, 1) = 10

MsgBox("LBound for FirstDimension = " & LBound(socketBins3, 1))

MsgBox("LBound for SecondDimension = " & LBound(socketBins3, 2))

MsgBox("LBound for ThirdDimension = " & LBound(socketBins3, 3))

 

MsgBox("UBound for FirstDimension = " & UBound(socketBins3, 1))

MsgBox("UBound for SecondDimension = " & UBound(socketBins3, 2))

MsgBox("UBound for ThirdDimension = " & UBound(socketBins3, 3))

 

socketBins3(0, 0, 0) = 10 ' <--- This will throw an exception

 

'This are other possibilities

'This is not possible Dim socketbins(1 to DUT, 1 to SOCK, 1 to 1000) as Byte

Dim socketbins(0 To DUT, 0 To SOCK, 0 To 1000) As Byte

Dim socketBins2 As New VBArray(Of VBArray(Of VBArray(Of Byte)))(1, DUT)

'And then some init code here

 

End Sub

End Module