Announcement

Collapse
No announcement yet.

Shared Arrays

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

  • Shared Arrays

    $ERROR ALL ON

    CLS
    CLEAR
    DEFINT A-Z

    TYPE TSStatsDEF
    HalfCap AS LONG
    KeyLen AS LONG
    ItmLen AS LONG
    DupFlag AS LONG
    RootNode AS LONG
    NextNode AS LONG
    LastDel AS LONG
    NodeAct AS LONG
    KeysAct AS LONG
    Filler AS STRING *476
    END TYPE

    SUB Init
    SHARED dbMaxFiles

    dbMaxFiles=4

    DIM TreeStats(dbMaxFiles) AS SHARED TSStatsDEF

    TreeStats(1).KeyLen =10
    TreeStats(2).ItmLen =18
    END SUB


    CALL Init

    ?TreeStats.ItmLen(1)

    Why do I get a syntax error when trying to access one of the array elements?
    If I move the DIM outside of the Procedure it works fine.

    Help please.



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

  • #2
    TreeStats is the array. ItmLen is an element of
    the array. Try this:
    Code:
    ?TreeStats(1).ItmLen

    ------------------
    Tom Hanlin
    PowerBASIC Staff

    Comment


    • #3
      Sorry my test code has a mistake, I was using ?TreeStats(1).ItmLen but the compiler reports error 477 syntax error.


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

      Comment


      • #4
        I think that the problem is scope resolution. The SHARED declaration
        should be outside of the SUB (which you said works for you) if it is
        addressed outside of the SUB.
        regards,

        ------------------
        [email protected]
        :) IRC :)

        Comment


        • #5
          'k, I've looked into this a bit deeper. The main problem, I think, is in
          the usage of SHARED. It appears that you're aiming to create a global
          array here. In that case, you need to define the array outside of any SUB
          or FUNCTION. On a more trivial note, TreeStats(2) was perhaps meant to be
          TreeStats(1) here. Anyway, try this:
          Code:
          $ERROR ALL ON
          
          CLS
          CLEAR
          DEFINT A-Z
          
          TYPE TSStatsDEF
            HalfCap  AS LONG
            KeyLen   AS LONG
            ItmLen   AS LONG
            DupFlag  AS LONG
            RootNode AS LONG
            NextNode AS LONG
            LastDel  AS LONG
            NodeAct  AS LONG
            KeysAct  AS LONG
            Filler   AS STRING *476
          END TYPE
          
            DIM TreeStats(1:1) AS SHARED TSStatsDEF
          
          SUB Init
            SHARED dbMaxFiles
          
            dbMaxFiles=4
          
            REDIM TreeStats(1:dbMaxFiles) AS SHARED TSStatsDEF
          
            TreeStats(1).KeyLen =10
            TreeStats(1).ItmLen =18
          END SUB
          
          CALL Init
          
          ?TreeStats(1).ItmLen
          The reference manual's notes on the SHARED statement are more detailed
          than the notes on DIM..SHARED, so you may want to look there as well.

          ------------------
          Tom Hanlin
          PowerBASIC Staff

          Comment


          • #6
            Correct I'm trying to create a global array, and your suggestion works.

            Many Thanks



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

            Comment

            Working...
            X