Announcement

Collapse
No announcement yet.

newbie question about global string arrays

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

  • newbie question about global string arrays

    Hi all - 2 quick questions:

    I am using Firefox 2.9 and PB 9 - in the FF_WINMAIN function (that starts first when my program starts), I am trying to define a global string array that I can use throughout my program but I can't find the correct syntax (and yes, I have looked in the help files)...I'm sure it's something idiotic that I am not seeing... I am trying to use:

    Global thearray (1 To 20,1 To 20,1 To 4) As String

    Why doesn't this line work?

    Once this begins working, I'll simply be reading and putting values in the variable using lines like:

    thearray(2,5,2)="123"

    I was able to get these lines to compile:
    Dim thearray(1 To 20,1 To 20,1 To 4) As Global String
    thearray(1,1,1)="bingo"

    but further in the program when I try to "msgbox thearray(1,1,1)", it wants me to dimension the variable (like it's not global) - arg!

    Any ideas how I can dimension this string array variable to be used globally in my little program?

    I also need to dimension two string variables (non-arrayed) globally as well if you can comment on that.

    Thanks

    Patrick
    Last edited by Patrick Harvey; 8 Nov 2008, 08:02 PM.

  • #2
    Looks like you are using FireFly, not FireFox

    Outside of the FF_WinMain() function you put code like:

    Global thearray () As String

    Inside the FF_WinMain() function you would put the following:

    Dim thearray (1 To 20,1 To 20,1 To 4) As Global String

    Check out the "GLOBAL Statement" Help file topic in the PB help file.
    Paul Squires
    FireFly Visual Designer (for PowerBASIC Windows 10+)
    Version 3 now available.
    http://www.planetsquires.com

    Comment


    • #3
      Paul, one question:

      Doesn't that second declaration of thearray CREATES another global?

      When I first began programming in PBWin I made the mistake of declaring global variables outside the PBMAIN sub AND declared them again inside the PBMAIN.

      What I really did was to declare two global instances of the same variable. This lead to many hard-to-solve errors until I declared the variables as globals outside the PBAMIN only.

      Now I do the same for arrays.

      What I do now is the following, without specifying the type inside the PBMAIN:

      Code:
      GLOBAL thearray() AS STRING
      
      PBMAIN
      
           DIM thearray(1 to 20)
      
           ... HERE THE CODE ...
      
      END SUB
      Last edited by Pedro Ramirez; 8 Nov 2008, 08:39 PM.

      Comment


      • #4
        Another way is to groupl all Globals together in one Function/Sub.
        Code:
        'Putting the Globals in a function here allows faster access during programming
        ' can access them via F4 quicker than otherwise (scrolling, etc.)
        Function Global_Variables As Long
        '*Nearly* all variables are Global. Not good programming practice maybe but this is a standalone program
        '  so there's no concern of "cross contamination" and what it does do is allow for more liberal use
        '  of Subs and Functions to ease program readability. GHL
        'My stuff
        'note all "t" variables are Local (except timer stuff, Twinkle_On_Flag), don't make any Global
          'strings here
        Global Hdr_Start, Starting_Time, Caption, Hour_in_Day, k, Last_k, Rules(), s, Stasis_Results() As String           
           ' Row Col arrays
          Global nRC() As Byte          
          Global nRCPtr0, nRCPtr1, nRCPtr2, nRCPtr3, nRCPtr4, nRCPtr5, nRCPtr6, nRCPtr7 As Byte Ptr
          Global tRC() As Byte
          Global tRCPtr0, tRCPtr1, tRCPtr2, tRCPtr3, tRCPtr4, tRCPtr5, tRCPtr6, tRCPtr7 As Byte Ptr
         
        Global Array_Num, Array_Num_Max, _ ' As Long
         _
               bg_Color_Now, bg_Color, bg_Ctr_Flag,  bg_Color_Chgs_per_Day, bg_Color_Chgs_Today, _
               bg_start_color, bg_Finish_Color, Bg_Color_Dir, _ 
               bg_Change_ctr, bg_Color_R, bg_Color_G, bg_Color_B, _
               bg_Color_Last, Bg_Change_Flag, bg_R_Dir, bg_G_dir, bg_B_dir, _
               Both_Flag, _ 
         _
               Col_Max, Colonies_Added, C, Colony_Centers(), Crown_Flag, Caption_Scroller_Flag, _
               Current_Dot_Color, Clors(), clor, ctr, ctr1, ctr2, ctr3, Col As Long, _
               Char_Height, Char_Width As Single, _
               Color_Ptr, cline_ptr As Long Ptr
         
        Global Days_Ctr As Long 
        Global flen, fnum, Forts_Added, Finish_Color, First_Round_Flag, flag As Long, _ 
               fle$, fn$, _
               Font_Handle As Dword, _
               Font_Size As Single        
         
        Global Gary_Flag, Goldfish_Flag, Generations, gen_check, gen_total_Last, gen_total As Long
        Global Hm_Ctr1, Horizontal_Scroll_Flag, hrs As Long
        Global LB_tg, Last_Secs, Last_bg_Color, Last_gen_total, last_Scroll_secs, Life_Ctr, Living_Color_Flag As Long, _
               Left_Col() As Byte
        Global mins, Max_Colors  As Long
        Global num As Long, _
               nline_ptr As Long Ptr
        Global Original_Starting_Population As Long
        Global Pause_Flag, Pixel_Color_Direction, Pixel_Color_Flag, Pixel_Color As Long, _
               pline_ptr, Pixel_Color_Ptr As Long Ptr
        Global Row_Top_Left, Row_Top_Ctr, Row_Top_Right, _
               Row_Mid_Left, Row_Mid_Right, _
               Row_Btm_Left, Row_Btm_Ctr, Row_Btm_Right, _
               Row_Max, R, rnd_dir, Row As Long
        Global Spce, Stp, Start_Flag, Scroll_ctr, secs, Secs_Last, Scroll_secs_Last, Seed, Stasis_Range, _
               Slow_Motion_Flag, Start_Color, Sum_ctr(), Stasis_Points_Flag, Stasis_ctr  As Long, _
               sum_Ptr As Long Ptr, _
               Saying_Delay As Long, Saying_Lines(), Sayings(), Saying, Saying_Last As String
        Global tmr1, timer_last, tmr, Twinkle_On_Flag As Long, _
               TotPixel_Ptr As Long Ptr
        Global Update_Flag, UB_tg As Long, _
               UpDate() As String
         
        Global Wrap_Top_or_Bottom, Working_Flag As Long, _
               w_Ptr As Long Ptr
         
        Global x_ptr As Long Ptr       
        'Gary's stuff so don't use anywhere
        Global w,h,ncw,nch,gwin,z,sum,rsx,rsy,x,y,w1,h1,gdc, totpixel As Long
        Global tg(), ng() As Byte
        Global tgptr, ngptr As Byte Ptr
        Global c1,x,y,offset As Long
        Global pline, nline, cline As Long
        End Function
        '
        Then
        Code:
        Function PBMain() As Long
          Global_Variables  'assign all Globals
        '  Do other stuff here ....
        ============================================
        "All the world's a stage,
        and most of us are desperately unrehearsed."
        Sean O'Casey
        ============================================
        It's a pretty day. I hope you enjoy it.

        Gösta

        JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
        LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

        Comment


        • #5
          Thanks Paul/others - yep, I meant FireFly

          I think I have it working now, but it seems in Firefly that I have to add

          Dim thearray (1 To 20,1 To 20,1 To 4) As Global String

          to each function (ie, for each item (button) clicked where i want to access the global string array)

          Sound correct...?

          Comment


          • #6
            Do you have to add:
            Dim thearray (1 To 20,1 To 20,1 To 4) As Global String
            or is:
            Dim thearray (1 To 20,1 To 20,1 To 4)
            enough?

            The PB compiler already knows it is a global string array from the:
            GLOBAL thearray() AS STRING
            statement.

            Inside the function you should only be setting the dimensions and bounds with the DIM statement.

            -------------------------
            "to each function (ie, for each item (button) clicked where i want to access the global string array)
            Sound correct...?"

            No, you're defeating the purpose of making it global in the first place. In other functions you just use the array, you don't have to redefine it because it it GLOBAL.

            Someone may have mentioned above, but a memory aid (your's, not the computer's) is to prefix all global names with a "g". In this case like:
            GLOBAL gTheArray() AS STRING

            Cheers,
            Last edited by Dale Yarker; 8 Nov 2008, 11:59 PM.
            Dale

            Comment


            • #7
              Most of the answers on globals and arrays are already out there, but due to the lack of an efficient indexing system, we keep revisiting them!

              Here's another thread on a similar topic. Post #12 answers one of your questions: http://www.powerbasic.com/support/pb...ighlight=array

              Comment


              • #8
                There are some very serious misconceptions mentioned here, so let me clarify it.

                The GLOBAL statement (without dimensions) is used to declare that a particular global array exists. It simply tells the compiler that a particular name should be associated with this particular global array.

                Global thearray () As String

                GLOBAL does not generate any executable code -- it's an informational entry. It's best to place it at the beginning of the program and can appear outside of a Sub/Function. You can repeat it anywhere in the program as a reminder to yourself that it exists, but these additional copies do not affect the compiler. If you create a LOCAL or STATIC array by the same name, it takes precedence over the GLOBAL in that particular Sub/Function.

                The DIM statement (with dimensions) is used to allocate memory for the array. It determines the number of dimensions, and the number of elements in each of them.

                DIM thearray (1 To 20,1 To 20,1 To 4) As Global String

                You must include the scope (global) and type (string) so the compiler will know exactly which array you are allocating. For example, if you omit the word global, the compiler would have to assume that you were actually creating a second local array, because LOCAL is the default scope. The DIM statement must be located inside a Sub/Function, and must be executed before any element of the array is referenced. If a DIM statement is executed for an array which already has active dimensions, it is ignored.

                Best regards,

                Bob Zale
                PowerBASIC Inc.

                Comment


                • #9
                  Thanks Bob/others:

                  Thanks Bob /everyone:

                  I really appreciate the help! It's working great now! yea!

                  Patrick

                  Comment


                  • #10
                    If a DIM statement is executed for an array which already has active dimensions, it is ignored.
                    Hmmm... Since the compiler already detects "array not dimensioned" and flags that as an error, I wonder if maybe there shouldn't maybe also be be...
                    Code:
                    PowerBASIC Console Compiler
                    PB/CC   Version Something 
                    Copyright (c) 1998-2008 PowerBasic Inc.
                    Venice, Florida USA
                    All Rights Reserved
                    
                    Error [b]99999[/b]in D:\SOFTWA~1\pbcc50\Work\BUgs\StdErr1.bas(20:014): 
                     Array already dimensioned: MyArray
                    .. compile time error, when a second DIM for the same array appears in a prodedure?

                    Goodness knows we've seen people confused by "MY DIM () did not work" (because this condition existed and the DIM was ignored).

                    ???

                    MCM
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                    • #11
                      Ok. After clarifying some concepts (thanks Bob for that) I found that my previos explanation on DIMming arrays is wrong. I went on and check both the PB help file and my commercial application and found that what I really do is to REDIM the array when I first want to set dimensions on it:

                      First: I declared the array as GLOBAL.

                      Second: I dimension it inside a procedure but using the REDIM statement so as to keep the array GLOBAL (as declared previously). If I where to use the DIM statement instead, I would end up dimensioning a LOCAL array.


                      Code:
                      ' Declare as global
                      GLOBAL thearray() AS STRING
                      
                      PBMAIN
                      
                          ' Redim to maintain global scope
                          ' incorrect: REDIM thearray(1 to 20)
                      
                          ' Correct:
                          REDIM thearray(1 to 20) AS GLOBAL STRING
                      
                           ... HERE THE CODE ...
                      
                      END SUB
                      Last edited by Pedro Ramirez; 9 Nov 2008, 11:19 AM.

                      Comment


                      • #12
                        Your REDIM must also have an AS GLOBAL STRING clause. A MUST. That's a necessity in this case for both DIM and REDIM.

                        Bob Zale
                        PowerBASIC Inc.

                        Comment


                        • #13
                          After my previuos post I decided to test my example, so I build the following code:

                          Code:
                          #COMPILE EXE
                          #DIM ALL
                          
                          GLOBAL MyArray() AS STRING
                          
                          FUNCTION PBMAIN () AS LONG
                          
                                ' incorrect: DIM MyArray(1 TO 5) AS STRING
                              
                              ' Correct: 
                               DIM MyArray(1 TO 5) AS GLOBAL STRING
                          
                                MyArray(3) ="third element"
                                ShowValue
                          
                          END FUNCTION
                          
                          
                          SUB ShowValue
                               
                               MSGBOX MyArray(3)
                               
                          END SUB
                          The line "DIM MyArray(1 TO 5) AS STRING" makes all the difference. As shown, MyArray() remains GLOBAL, as it does if I use the REDIM statement or if I ommit the type ("AS STRING"). The compiler "knows" that MyArray, unless otherwise noted, is GLOBAL.

                          If I change that line to "DIM MyArray(1 TO 5) AS LOCAL STRING" then a new LOCAL array is created and has the same name as the GLOBAL one.
                          In this case the compiler gives error 461: array no dimensioned (inside sub ShowValue) when I try to compile the program.

                          So, as Bob pointed out, the only way to declare it is by specifying the scope: "DIM MyArray(1 TO 5) AS GLOBAL STRING".

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

                          Bob, I read your comment after I posted this one, so I corrected it.
                          Thanks for the warning.
                          Last edited by Pedro Ramirez; 9 Nov 2008, 11:18 AM.

                          Comment

                          Working...
                          X