SHARED Array values with SUBs and FUNCTIONs
#11
KP "No!"  no my code runs fine!

Why are you changing the ReDim's to 2 for lower bound?
   

My code ran fine here is indy test of it to show the final values as correct:
Code: (Select All)
Dim Shared mySharedArray(1 To 20) As Long

For i = 1 To 20
    mySharedArray(i) = i * i
Next

ReDim out10(1 To 10) As Integer
first10Plus5 = first10squaresPlus&(5, out10())
For i = 1 To 10
    Print i ^ 2 + 5, out10(i)
    tot2 = tot2 + i ^ 2 + 5
Next
Print "Total was: "; first10Plus5
Print "Tot2 check my sub total"; tot2

Function first10squaresPlus& (X, outputFirst10() As Integer)
    For i = 1 To 10
        outputFirst10(i) = mySharedArray(i) + X
        tot& = tot& + outputFirst10(i)
    Next
    first10squaresPlus& = tot&
End Function
   
b = b + ...
Reply
#12
Things should be done like Jack's example if the subprogram has to handle arrays like that. Cannot assume 0 or 1 or any other value as the lowest subscript, just like trying to decide the upper subscript for a given program using the same subprogram.

There are too many programs I've seen that assume only zeroth subscript, to then use REDIM _PRESERVE on it, but even so, a subprogram should always consult with LBOUND() and UBOUND() for the limits.
Reply




Users browsing this thread: 7 Guest(s)