none
Passing arrays to subroutines with Option Strict On RRS feed

  • Question

  • Is it possible to pass a two-dimensional array of real numbers BYREF to a subroutine, with Option Strict turned on?

    With Option Strict OFF the subroutine can accept the array name argument as an Object, and do what it likes - but with Option Strict ON, it objects to the narrowing of an object to a real array, and also to the late bindings that happen when the subroutine writes to the array.

    One-dimensional arrays work fine - the problem with 2 dimensions, of course, is telling the subroutine the size of the first dimension...

    Any thoughts gratefully received!

    Thursday, February 23, 2017 4:40 PM

Answers

  • I tried to explain that late binding IS used in my hacked solution, because the subroutine accepts the array as an Object, and subsequently treats it as a two-dimensional array.

    But in the meantime, I have found the answer on another thread - I just refer to the array as
    ByRef X(,) as Double in the subroutine arguments.
    Thursday, February 23, 2017 5:00 PM
  • Assuming that you are not passing jagged arrays this might be helpful.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            'one dimesion
            Dim myArr1(2) As String
            For x As Integer = 0 To 2
                myArr1(x) = Chr(Asc("A") + x)
            Next
            ShVals(myArr1)
    
            'two dimensions
            Dim myArr2(2, 4) As Integer
            For i As Integer = myArr2.GetLowerBound(0) To myArr2.GetUpperBound(0)
                For j As Integer = myArr2.GetLowerBound(1) To myArr2.GetUpperBound(1)
                    myArr2(i, j) = (i + 1) * (j + 1)
                Next j
            Next i ' Displays the properties of the Array.
            ShVals(myArr2)
        End Sub
    
        Private Sub ShVals(anArray As Array)
            Dim cols As Integer = anArray.GetLength(anArray.Rank - 1) - 1
            Dim curCol As Integer = 0
            Dim curRow As Integer = 0
            Dim ie As IEnumerator = anArray.GetEnumerator
    
            Do While ie.MoveNext
                Debug.WriteLine("Row: {0}, Col: {1}, Value: {2}", curRow, curCol, ie.Current)
                If curCol < cols Then
                    curCol += 1
                Else
                    curCol = 0
                    curRow += 1
                    Debug.WriteLine("")
                End If
            Loop
        End Sub
    



    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    Thursday, February 23, 2017 5:34 PM

All replies

  • Option strict On does not change code, it only gives errors if late binding is used.

    Therefore you must have done something more. 


    Success
    Cor


    • Edited by Cor Ligthert Thursday, February 23, 2017 4:50 PM
    Thursday, February 23, 2017 4:49 PM
  • I tried to explain that late binding IS used in my hacked solution, because the subroutine accepts the array as an Object, and subsequently treats it as a two-dimensional array.

    But in the meantime, I have found the answer on another thread - I just refer to the array as
    ByRef X(,) as Double in the subroutine arguments.
    Thursday, February 23, 2017 5:00 PM
  • For instance, 

    However, be aware that a double is a strange value type if you don't understand it completely. If it is real a number you can better take an Int64

    A number is mostly used as an identifier. 


    Success
    Cor

    Thursday, February 23, 2017 5:09 PM
  • Assuming that you are not passing jagged arrays this might be helpful.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            'one dimesion
            Dim myArr1(2) As String
            For x As Integer = 0 To 2
                myArr1(x) = Chr(Asc("A") + x)
            Next
            ShVals(myArr1)
    
            'two dimensions
            Dim myArr2(2, 4) As Integer
            For i As Integer = myArr2.GetLowerBound(0) To myArr2.GetUpperBound(0)
                For j As Integer = myArr2.GetLowerBound(1) To myArr2.GetUpperBound(1)
                    myArr2(i, j) = (i + 1) * (j + 1)
                Next j
            Next i ' Displays the properties of the Array.
            ShVals(myArr2)
        End Sub
    
        Private Sub ShVals(anArray As Array)
            Dim cols As Integer = anArray.GetLength(anArray.Rank - 1) - 1
            Dim curCol As Integer = 0
            Dim curRow As Integer = 0
            Dim ie As IEnumerator = anArray.GetEnumerator
    
            Do While ie.MoveNext
                Debug.WriteLine("Row: {0}, Col: {1}, Value: {2}", curRow, curCol, ie.Current)
                If curCol < cols Then
                    curCol += 1
                Else
                    curCol = 0
                    curRow += 1
                    Debug.WriteLine("")
                End If
            Loop
        End Sub
    



    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    Thursday, February 23, 2017 5:34 PM
  • Thanks - I didn't know how to look at the shape of an array.

    And yes, I have used DoEvents in the past (because it solved a problem) - so clearly I have no idea what it actually does!  :-)
    • Edited by Aylmer51 Thursday, February 23, 2017 6:32 PM
    Thursday, February 23, 2017 6:29 PM


  • And yes, I have used DoEvents in the past (because it solved a problem) - so clearly I have no idea what it actually does!  :-)

    What it does is send all WM messages to the active window. 

    The trouble with it, is that it can be at the wrong time. 

    However, the problem is the same if you use multi threading without taking action not to send WM messages at the wrong moment.

    Therefore he could have written as well. "Those who use Multi Threading who have no ideas what it does and those who never use it" 

    But I've to admit, it sounds sophisticated.


    Success
    Cor



    Friday, February 24, 2017 12:01 AM