Announcement

Collapse
No announcement yet.

UBOUND/LBOUND on VIRTUAL arrays?

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

  • UBOUND/LBOUND on VIRTUAL arrays?

    I'm having a problem with the UBOUND / LBOUND functions in PB3.5 with a VIRTUAL array shared between a main program and a PBU... Reduced to the essentials, the code is as follows:

    main program code
    Code:
    $LINK "inif.pbu"
    DECLARE FUNCTION PutINIParameter?(string, string)
    DIM VIRTUAL IniParms(100) AS STRING * 64
    SHARED IniParms()
    
    CLS
    PRINT UBOUND(IniParms())
    PRINT LBOUND(IniParms())
    
    FOR Q = 0 to 15
    X = PutINIParameter?("ALEPH", "TEXTVAL")
    PRINT X
    NEXT Q
    INIF.PBU code
    Code:
    $COMPILE UNIT
    
    DIM VIRTUAL IniParms(100) AS STRING * 64
    SHARED IniParms()
    
    FUNCTION PutINIParameter?(ParameterName$, ParameterVal$) PUBLIC
    SHARED IniParms()
    LOCAL EntrySlot?
    
    EntrySlot? = LBOUND(IniParms()) 'get lowermost entry slot in array
    
    DO
      IF IniParms(EntrySlot?) = STRING$(64,0) THEN EXIT DO
      INCR EntrySlot?
    LOOP UNTIL EntrySlot? > UBOUND(IniParms())
    
    IF EntrySlot? <=  UBOUND(IniParms) THEN
      IniParms(EntrySlot?) = ParameterVal$
      PutINIParameter? = -1
    ELSE
      PutINIParameter? = 0
    END IF
    
    END FUNCTION

    Now, the INIF.PBU code compiles just fine; no complaints from PB. Running the code, however, I get "Illegal Function Call" errors on the UBOUND/LBOUND statements in the PutINIParameter function - but not in the main program!

    I've been able to verify that the IniParms() virtual array is being properly shared between the two, with a little test code that forces values into the array elements in the PBU and then prints them out in the Main, so... why doesn't the above code work properly?

    ------------------

  • #2
    The problem is that the PBU code is treating the array as "local" to the PBU... in order to make the array available from the main code you need to make the array PUBLIC in both the main and the PBU code (or you could use the External/Public technique too).
    Code:
    'Main code
    ...
    DIM VIRTUAL IniParms(100) AS STRING * 64
    PUBLIC IniParms()
    SHARED IniParms()
    ...
    
    'PBU code
    DIM VIRTUAL IniParms(MAX,1) AS STRING * 64
    PUBLIC IniParms()
    SHARED IniParms()
    Note the way that I declared the array in the PBU code... this makes it easier to control the size of the array in the main code, rather than the UNIT code.

    See the help file for more information on PUBLIC variables, and using the DIM ArrayName(MIN|MAX,n) method.



    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Ahh, I see... hm. So, PowerBASIC assumed it as a "local" array when it compiled the PBU, then it somehow turned "pseudo-global" when it linked the PBU to the main code? (Like I said, I could put values into IniParms() in the PBU code and read them in the main, so obviously the array wasn't purely local to the PBU...)

      Actually, it would be fine by me if the array was purely local to the PBU; the main doesn't actually need access to it... But when I tried compiling and running it that way (no DIM VIRTUAL/SHARED statements in the main code), the program GPF'ed on me. (Crashed a DOS box in Win98, and kicked off an EMM386 protection error screen under DR-DOS 7.02.) I assumed (perhaps incorrectly) that was because the EMS code doesn't get linked into a PBU, and didn't get linked into the main code for some reason... or did I miss something?

      ------------------

      Comment

      Working...
      X