Announcement

Collapse
No announcement yet.

ReDim VB string array

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • ReDim VB string array

    I'm trying to pass a VB string array to PB, enlarge it, write to it, and pass it back to VB. Something's not working. Here's the code --




    In VB ------


    Private Declare Sub enlarge_string_array _
    Lib "m:\Power Basic\Lib\Sorting and Searching\main.dll" _
    (s() As String)


    ===================================================


    Private Sub Command1_Click()

    Dim s() As String
    Dim msg As String
    Dim i As Integer



    ReDim s(0 To 5)



    'goto PB --
    Call enlarge_string_array(s())



    'show results --
    For i = LBound(s) To UBound(s)
    msg = msg & i & " -- " & s(i) & Chr(10)
    Next

    MsgBox msg

    End Sub



    '=======================================================


    In PB -------



    SUB enlarge_string_array ALIAS "enlarge_string_array" (pSA AS DWORD) EXPORT

    DIM LBound_ AS LONG
    DIM UBound_ AS LONG
    DIM array_address AS DWORD
    DIM ret AS LONG



    LBound_ = vbArrayLBound(pSA, 1)
    UBound_ = vbArrayUBound(pSA, 1)


    array_address = vbArrayFirstElem(pSA)
    MSGBOX STR$(array_address)
    DIM s(LBound_ TO UBound_) AS STRING AT array_address


    'try to redim from (0 ... 5) to (0 ... 10) --
    ret = vbArrayRedim(pSA, 11)
    IF ret = 0 THEN
    MSGBOX "problem with vbArrayRedim"
    END IF



    MSGBOX "after enlargement" 'OK THRU HERE



    s(3) = "new value at #3" 'DOESN'T CAUSE A CRASH 'BUT ASSIGNMENT DOES NOT GET BACK TO VB

    MSGBOX "after first assignment"



    s(7) = "new value at #7" 'CAUSES VB TO CRASH
    MSGBOX "after second assignment"

    END SUB

  • #2
    Hi Bill,

    Try this PB code:
    Code:
    SUB enlarge_string_array ALIAS "enlarge_string_array" (pSA AS DWORD) EXPORT
    
       DIM LBound_ AS LONG
       DIM UBound_ AS LONG
       DIM array_address AS DWORD
       DIM ret AS LONG
    
       'try to redim from (0 ... 5) to (0 ... 10) --
       ret = vbArrayRedim(pSA, 11)
       IF ret = 0 THEN
       MSGBOX "problem with vbArrayRedim"
       END IF
    
       LBound_ = vbArrayLBound(pSA, 1)
       UBound_ = vbArrayUBound(pSA, 1)
       array_address = vbArrayFirstElem(pSA)
       
       MSGBOX STR$(LBound_) & " to " & STR$(UBound_) & " at " & STR$(array_address)
       DIM s(LBound_ TO UBound_) AS STRING AT array_address
    
       MSGBOX "after enlargement" 'OK THRU HERE
    
       s(3) = "new value at #3" 'DOESN'T CAUSE A CRASH 'BUT ASSIGNMENT DOES NOT GET BACK TO VB
    
       MSGBOX "after first assignment"
    
       s(7) = "new value at #7" 'CAUSES VB TO CRASH
       MSGBOX "after second assignment"
    
    END SUB
    Your problem is that you DIM s at the size and address of the array, and then redimension it. You need to do those two the other way around! The array will definitely change size and there's no guarantee it will be in the same place in memory either.

    Regards,

    Pete.

    Comment


    • #3
      Perfect! Thanks

      Comment

      Working...
      X