0

I am sure that this is a simple question but I am very new to vb.net and I am struggling to figure out this. basically in VBA you can use a Sub to populate an array and as long as the array has been declared public in the module this can been seen on any module.

Public reg12bound(1 To 5) As Double

Sub region12boundary()  
'  
'  Initialize coefficients for boundary equation  
'  
    reg12bound(1) = 348.05185628969  
    reg12bound(2) = -1.1671859879975  
    reg12bound(3) = 1.0192970039326E-03  
    reg12bound(4) = 572.54459862746  
    reg12bound(5) = 13.91883977887  
'  
End Sub

then this can be seen in another module using the code below

Private Function boundary23P(Temp)  
 Call region12boundary  
 boundary23P = (reg12bound(1) + reg12bound(2) * Temp + reg12bound(3) * (Temp ^ 2)) * 1000000  
End Function

essentially I wish to repeat this functionality in VB.net but when I try the following

Public reg12bound(5) As Double

Sub region12boundary()
    '
    '  Initialize coefficients for boundary equation
    '
    ReDim reg12bound(5)
    reg12bound(1) = 348.05185628969
    reg12bound(2) = -1.1671859879975
    reg12bound(3) = 0.0010192970039326
    reg12bound(4) = 572.54459862746
    reg12bound(5) = 13.91883977887
    '
End Sub

Public Function boundary23P(ByVal Temp As Double) As Double

    call region12boundary()  
    boundary23P = (reg12bound(1) + reg12bound(2) * Temp + reg12bound(3) * (Temp ^ 2)) * 1000000  

End Function

The array is populated with 0 instead of the values. The functionality seems to work fine when I populate within a function. But I would rather avoid this as most of the functions reference the same array of 34 variable which would need to be copied into each function.

Also I do realise that the reg12bound(0) value is not populated this didn't seem to be a problem in the self contained version so I am assuming this is fine from outside as well

edited to add the call to the function

2
  • 1
    Collections are zero based in .NET, so they start with index 0 not 1. Commented Nov 11, 2013 at 9:05
  • You have to show how you are calling boundary23P to understand what might be the problem. Also reg12bound(0) is not populated because you are not populating it. Commented Nov 11, 2013 at 9:06

1 Answer 1

1

In VB.NET the arrays will always have a lower bound of 0. This is fixed. I agree that VB6 and earlier versions had that facility to specify the bounds. But in .NET there isn't. You can no longer specify the lower bound of arrays.

Having said that, there is no reason why you can't achieve a comparable functionality yourself. Just a bit of work is required.

A. Create a class to handle the LBound offset. I have shown it with generics, but you can remove it if your array will always be of same type.

Public Class MyArray(Of T)
    Public LBound, UBound As Integer
    Private theArray() As T

    Public Sub New(ByVal lBound As Integer, ByVal uBound As Integer)
        Me.LBound = lBound
        Me.UBound = uBound
        ReDim theArray(uBound - lBound)
    End Sub

    Default Public Property Items(ByVal item As Integer) As T
        Get
            Return theArray(item - LBound)
        End Get
        Set(ByVal value As T)
            theArray(item - LBound) = value
        End Set
    End Property
End Class

B. Replace your array declaration with this class declaration:

Public arr As New MyArray(Of String)(5, 10)

C. Now you can set values in your code, as you did before:

    arr(5) = "item 5"
    arr(6) = "item 6"
    arr(7) = "item 7"
    arr(8) = "item 8"
    arr(9) = "item 9"
    arr(10) = "item 10"
    arr(11) = "item 11"  '<-- this wil cause index out of bounds error.

D. And access the values stored in the array as you did before:

    For i = 5 To 10
        Debug.Print(arr(i))
    Next

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this wasn't quite what I was looking for but it led me to the right direction. I thought that if I didn't define the (0) value it would automatically give it a value of 0. Replacing the value with a 0 seems to have solved the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.