Announcement

Collapse
No announcement yet.

PB9 Problems

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

  • #21
    When you declare an array with no bounds it's basically telling the compiler to declare that array as the specified type. The array is empty though.

    These are the same:

    Code:
     
    GLOBAL MyArray() As Long
     
    -or-
     
    DIM MyArray() As Long
     
    ...later...
     
    REDIM MyArray(0) ' No type required, but array can now be used.
    Just like DECLARE FUNCTION doesn't actually create a function, but allows the compiler to call a function with that definition using CODEPTR. Declaring an array with no elements, simply creates a definition to be used later.
    Last edited by Kev Peel; 16 Dec 2008, 11:34 AM.
    kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

    Comment


    • #22
      Or the most simple demo of the array
      Code:
      #COMPILE EXE
      #DIM ALL
      
      FUNCTION PBMAIN () AS LONG
      
          DIM MyArray() AS LONG
          MSGBOX "MyArray Declared" + $CR + "LBOUND(MyArray) = " + STR$(LBOUND(MyArray)) + $CR + "UBOUND(MyArray) = " + STR$(UBOUND(MyArray))
      
      END FUNCTION
      Engineer's Motto: If it aint broke take it apart and fix it

      "If at 1st you don't succeed... call it version 1.0"

      "Half of Programming is coding"....."The other 90% is DEBUGGING"

      "Document my code????" .... "WHYYY??? do you think they call it CODE? "

      Comment


      • #23
        I know what and array declaration does. However:

        Code:
        #COMPILE EXE
        DEFLNG A - z
        
        FUNCTION PBMAIN()
        
        STATIC A() AS LONG
        LOCAL B   AS STRING
        
        CALL A_Sub(A(), Cnt)
        
        FOR I = 1 TO Cnt                      
             B$ = B$ + STR$(A(I) + ", "     '<--- "Array not dimensioned" compiler error here
        NEXT I                                    ' yet the array is dimensioned 
        
        MSGBOX B$
        
        END FUNCTION
        
        
        SUB A_Sub(B(), Ubnd)
        
        FOR I = 1 TO 10
             REDIM PRESERVE B(1 TO I)
             B(I) = I
        NEXT I
        
        Cnt = 10
        END SUB
        Walt Decker

        Comment


        • #24
          I'm pretty sure you need at least one DIM or REDIM with an element count before a REDIM PRESERVE

          Use DIM A(1 to 1) AS STATIC LONG
          kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

          Comment


          • #25
            Walt,

            Array A(x) is not dimensioned B() is. Edit Had it bassack...

            See: REDIM PRESERVE B(1 TO I) in your sample.
            Last edited by Scott Hauser; 16 Dec 2008, 04:34 PM.
            The most exasperating part of the "rat race" is how often the rats are in the lead!

            Comment


            • #26
              Kev:

              No, you don't need a DIM prior to REDIM PRESERVE.

              Scott:

              B() in the sub is the same as A() in the main.
              Walt Decker

              Comment


              • #27
                Walt,

                IMO, the compiler is issuing a valid error where it should. It wants to 'DIM'ed as Kev mentioned before A-Sub is called. I made couple of tweaks to your code and it works. You are correct on the B(), I forgot that it is legal to do that because I never do it. My bad.

                Code:
                #DIM ALL
                
                #COMPILE EXE
                DEFLNG A - z
                
                FUNCTION PBMAIN()
                
                DIM A(1 TO 1) AS STATIC LONG '<---------------dimmed it explicitly here
                
                LOCAL Cnt, I AS LONG
                LOCAL B   AS STRING
                
                CALL A_Sub(A(), Cnt)
                
                FOR I = 1 TO Cnt
                     B$ = B$ + STR$(A(I)) + ", "     '<--- "Added ")"
                NEXT I                                    '
                
                MSGBOX B$
                
                END FUNCTION
                
                
                SUB A_Sub(B(), Ubnd)
                LOCAL I AS LONG
                FOR I = 1 TO 10
                     REDIM PRESERVE B(1 TO I)
                     B(I) = I
                NEXT I
                
                Ubnd = 10
                END SUB
                Last edited by Scott Hauser; 16 Dec 2008, 06:07 PM.
                The most exasperating part of the "rat race" is how often the rats are in the lead!

                Comment


                • #28
                  Scott:

                  The array "A()" is being dimensioned in A_Sub. Either the array descriptor or the array address is being passed to A_Sub.

                  Try this:

                  Code:
                  #COMPILE EXE
                  DEFLNG A - z
                  
                  FUNCTION PBMAIN()
                  
                  STATIC A() AS LONG
                  LOCAL B   AS STRING
                  
                  CALL A_Sub(A(), Cnt)
                  
                  CALL Msg_Sub(A(), Cnt)
                  
                  'FOR I = 1 TO Cnt                      
                  '     B$ = B$ + STR$(A(I) + ", "     '<--- "Array not dimensioned" compiler error here
                  'NEXT I                                    ' yet the array is dimensioned 
                  
                  'MSGBOX B$
                  
                  END FUNCTION
                  
                  
                  SUB A_Sub(B(), Ubnd)
                  
                  FOR I = 1 TO 10
                       REDIM PRESERVE B(1 TO I)
                       B(I) = I
                  NEXT I
                  
                  Cnt = 10
                  END SUB
                  
                  SUB Msg_Sub(B(), Ubnd)
                  
                  LOCAL A AS STRING
                  
                  FOR I = 1 TO Cnt                      
                       A$ = A$ + STR$(B(I) + ", "     '<--- No compiler error
                  NEXT I                                    
                  
                  MSGBOX A$
                  
                  END SUB
                  This shows that "A()" is actually dimensioned.
                  Walt Decker

                  Comment


                  • #29
                    Walt,
                    I think we crossed posts. Try the mod in my last post (I was editing my gibberish when you posted). It works if explicitly dimmed as Kev mentioned earlier.
                    The most exasperating part of the "rat race" is how often the rats are in the lead!

                    Comment


                    • #30
                      Scott:

                      That's right. It does have to be explicitly dimensioned prior to using, but only if it is in the same procedure it's declared, unless it's a global (I think).

                      This is what I've been trying to get at all along.
                      Walt Decker

                      Comment


                      • #31
                        Putting a couple Msgboxes in makes Walt's point clear. I even moved the A_Sub ahead of PBMain in case it was a forward referencing thing but same results.

                        Code:
                        'PBWin 9.00 - WinApi 05/2008 - XP Pro SP3
                         
                        #Compile Exe
                        DefLng A - z
                        Sub A_Sub(B(), Ubnd)
                        For I = 1 To 10
                             ReDim Preserve B(1 To I)
                             B(I) = I
                        Next I
                         ? "Ubound B() =" & Str$(UBound(b())),,FuncName$
                        Cnt = 10
                        End Sub
                        '
                        Function PBMain()
                        Static A() As Long
                        Local B   As String
                        Call A_Sub(A(), Cnt)
                        ' ? "Cnt = " & Str$(cnt),,FuncName$
                         ? "Ubound A() =" & Str$(UBound(A())),,FuncName$
                        For I = 1 To 10 'Cnt                      
                        '     B$ = B$ + Str$(A(I) + ", "     '<--- "Array not dimensioned" compiler error here
                        Next I                                    ' yet the array is dimensioned 
                        ? "i = " & Str$(i),,FuncName$
                        MsgBox B$
                        End Function
                        ============================================================
                        "The difference between fiction and reality?
                        Fiction has to make sense."
                        Tom Clancy
                        ============================================================
                        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


                        • #32
                          Gosta,

                          Wow! I guess I was completely missing Walt's point, (it is a gift I have sometimes). I stepped through your sample, the array filled properly and never raised an error.

                          Frankly, I had the exact same recollection as Kev's as to: "I'm pretty sure you need at least one DIM or REDIM with an element count before a REDIM PRESERVE."

                          Scott
                          The most exasperating part of the "rat race" is how often the rats are in the lead!

                          Comment


                          • #33
                            Code:
                            FUNCTION FOO AS LONG
                             LOCAL A() AS LONG, B() AS SINGLE 
                            
                              REDIM A(0), B(0)
                            
                              REAL CODE BEGINS HERE
                             ...
                            END FUNCTION
                            Michael Mattias
                            Tal Systems (retired)
                            Port Washington WI USA
                            [email protected]
                            http://www.talsystems.com

                            Comment


                            • #34
                              You guys are still missing the point.

                              1. A local array should not produce the compile error "array not dimensioned" regardless of where it is dimensioned and/or initialized.

                              2. A global array should not produce the compile error "array not dimensioned" regardless of where it is dimensioned and/or initialized.

                              But they do.

                              EXAMPLE 1: local array

                              Code:
                              #COMPILE EXE
                              DEFLNG A - Z
                              
                              FUNCTION PBMAIN()
                              
                              LOCAL A() AS STRING
                              
                              CALL A_Sub(A$(), Cnt)
                              
                              PRINT UBOUND(A$), Cnt
                              
                              FOR I = 1 TO Cnt
                                 PRINT A$(I)           '<--- compiler error "array not dimensioned"
                              NEXT I
                              
                              WAITKEY$
                              END FUNCTION
                              
                              SUB A_SUB(B$(), Ubnd)
                              
                              DATA "APPLES", "ORANGES", "GRAPES", "PINEAPPLES", "PEARS", "PAPAYA", "MANGO", "LEMONS"
                              
                              Ubnd = DATACOUNT
                              DIM B$(1 TO Ubnd)
                              
                              FOR I = 1 TO Ubnd
                                 B$(I) = READ$(I)
                              NEXT I
                              
                              END SUB
                              EXAMPLE 1: global array

                              Code:
                              #COMPILE EXE
                              DEFLNG A - Z
                              
                              GLOBAL Fruit() AS STRING
                              
                              FUNCTION PBMAIN()
                              
                              CALL A_Sub(Fruit$(), Cnt)
                              
                              PRINT UBOUND(Fruit$), Cnt
                              
                              FOR I = 1 TO Cnt
                                 PRINT Fruit$(I)           '<--- compiler error "array not dimensioned"
                              NEXT I
                              
                              WAITKEY$
                              END FUNCTION
                              
                              SUB A_SUB(B$(), Ubnd)
                              
                              DATA "APPLES", "ORANGES", "GRAPES", "PINEAPPLES", "PEARS", "PAPAYA", "MANGO", "LEMONS"
                              
                              Ubnd = DATACOUNT
                              DIM B$(1 TO Ubnd)
                              
                              FOR I = 1 TO Ubnd
                                 B$(I) = READ$(I)
                              NEXT I
                              
                              END SUB
                              In the above examples you will get the error where indicated even though I used DIM instead of REDIM PRESERVE. However, if you REM out the PRINT in the FOR/NEXT loop you will see that the UBOUND() is the same as "Cnt". Therefore the arrays are dimensioned.

                              This can be especially troublesome with globals that are supposed to be accessable anywhere regardless of where they are dimensioned.
                              Walt Decker

                              Comment


                              • #35
                                I don't get it, how is adding one line of code (REDIM x(0)) troublesome? Using DIM/REDIM before the array is first used is what the compiler is expecting, nothing more and nothing less. I can't see the problem unless this behaviour has changed since older versions (PB/DOS, PB/DLL, etc) and broken a lot of code.

                                Perhaps sending an email to PB requesting how you would like it to work would help? They don't read every post on this forum so may not hear you on here.
                                kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

                                Comment


                                • #36
                                  The Array was not Dimensioned

                                  After making a couple of changes to allow your example to run under PB 9, I indeed got the "array not dimensioned" error.

                                  I believe the problem is due to the fact that the Fruit array was never dimensioned.

                                  Code:
                                  #COMPILE EXE
                                  DEFLNG A - Z
                                  
                                  GLOBAL Fruit() AS STRING
                                  
                                  FUNCTION PBMAIN()
                                  
                                  CALL A_Sub(Fruit$(), Cnt)
                                  
                                  PRINT UBOUND(Fruit$), Cnt
                                  
                                  'Adding:
                                  DIM Fruit$(1 to Cnt)
                                  'Solves the problem.
                                  
                                  FOR I = 1 TO Cnt
                                     PRINT Fruit$(I)           '<--- compiler error "array not dimensioned"
                                  NEXT I
                                  
                                  WAITKEY$
                                  END FUNCTION
                                  
                                  SUB A_SUB(B$(), Ubnd)
                                  
                                  DATA "APPLES", "ORANGES", "GRAPES", "PINEAPPLES", "PEARS", "PAPAYA", "MANGO", "LEMONS"
                                  
                                  Ubnd = DATACOUNT
                                  DIM B$(1 TO Ubnd)
                                  
                                  FOR I = 1 TO Ubnd
                                     B$(I) = READ$(I)
                                  NEXT I
                                  
                                  END SUB

                                  Comment


                                  • #37
                                    Walter:
                                    I believe the problem is due to the fact that the Fruit array was never dimensioned.
                                    Fruit$() is dimensioned in SUB A_Sub. It is passed to A_Sub in PBMAIN. If Fruit$() was not dimensioned then PRINT UBOUND(Fruit$) would not be the same as "Cnt". Since it is a global it should be usable in PBMAIN without a compiler error, but it is not.
                                    Walt Decker

                                    Comment


                                    • #38
                                      Kev:


                                      I don't get it, how is adding one line of code (REDIM x(0)) troublesome? Using DIM/REDIM before the array is first used is what the compiler is expecting, nothing more and nothing less. I can't see the problem unless this behaviour has changed since older versions (PB/DOS, PB/DLL, etc) and broken a lot of code.
                                      The problem is: the array is DIMed, REDIMed, REDIM PRESERVEd before it is used. The compiler should recognize that. To do it again, unless one actually wants to change the size, adds a spurious statement that is not needed and reduces execution time. It can also produce confusion, especially in a large app, and, to get an "array not dimensioned" compile-time error when one knows that the array has been dimensioned, is frustrating.

                                      Another thing, assume that you "DIM A(1 TO 1)" in one procedure then pass A() to a procedure that opens a file and reads the contents into A(). Assume further that as the file is read it REDIM PRESERVEs "A()", but there is no data in the file. When the app returns to the procedure that DIMed it, "A()" will still have a size of 1, but with nothing in it. Figuring out what happened can be a real bugger.
                                      Walt Decker

                                      Comment


                                      • #39
                                        Walt,

                                        I understand. In that situation, I tend to use a counter and return it as a separate parameter or as the actual function return value. I don't rely on the UBOUND of an array as it could be ambiguous if the array already contains data or such. I've just accepted that the additional DIM/REDIM is what is needed by the compiler, and the overhead is really negligible since the initial DIM is only required once for each array

                                        It is as it is, as they say
                                        kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

                                        Comment


                                        • #40
                                          1. A local array should not produce the compile error "array not dimensioned" regardless of where it is dimensioned and/or initialized.

                                          2. A global array should not produce the compile error "array not dimensioned" regardless of where it is dimensioned and/or initialized.
                                          I agree. Any "not dimensioned" errors should occur only at runtime.

                                          But they do.
                                          I was not in the room when the design decision was made.

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

                                          Comment

                                          Working...
                                          X