Announcement

Collapse
No announcement yet.

This may be a DIM question but...

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

  • This may be a DIM question but...

    Hi Folks,

    While learning DDT (I haven't used PB since 3.5 was a NEW release ) and so I'm finding that I fall back on the console compiler a lot (whatever works) I started wondering...

    Is it possible to DIM more than one variable on a line?

    The code would be: DIM x,y,z As LONG

    Obviously you're not going to do this for major variable, but for little test programs and loop counters I've always found it useful.

    John, Hypnotherapist Extraordinaire, NLP Trainer,
    and Full Time Grad Student in Clinical Psychology
    ---------------------------------------------------
    John Strasser
    Phone: 480 - 344 - 7713

    John,
    --------------------------------
    John Strasser
    Phone: 480 - 273 - 8798

  • #2
    John,
    Almost immediately I was going to post the answer is "YES", but then a moment hit me and remembered that although you can Dim a variable, (and I am in the habit of it myself, bad habit mind you), that I am trying to relearn to use Local rather than Dim because if you stop and think about it...."Dim" would technically be "Dimension" (meaning Arrays and such). or from the help files
    DIM statement
    Purpose: Declare and dimension arrays, scalar variables, and pointers.
    That said a simple example will show you that Dim will not work the way you want, but Local will
    Code:
    #COMPILE EXE
    #DIM ALL
    
    FUNCTION PBMAIN () AS LONG
    '    dim x, y, z AS LONG       '<--- Does not work
        LOCAL x, y, z AS LONG     '<--- Works
        x=1
        y=2
        z=4
        MSGBOX "x+y+z=" + STR$(x+y+z)
    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


    • #3
      I had to convert it to PB/win 7.04 but the following does work.
      Code:
      #COMPILE EXE
      #DIM ALL
      
      FUNCTION PBMAIN () AS LONG
          DIM x AS LOCAL LONG      '
          DIM y AS LOCAL LONG      '
          DIM z AS LOCAL LONG      '
      
      '    LOCAL x as long
      '    local y as long
      '    local z AS LONG     '<--- Works
          x=1
          y=2
          z=4
          MSGBOX "x+y+z=" + STR$(x+y+z)
      END FUNCTION
      So, changing your example from "DIM x,y,z AS LONG" to "DIM x,y,z AS LOCAL LONG" should work but I don't have version 8.x to test it.
      There are no atheists in a fox hole or the morning of a math test.
      If my flag offends you, I'll help you pack.

      Comment


      • #4
        Without getting into the whole Help File definitions, I did a quick test and got the following:
        Code:
        #COMPILE EXE
        #DIM ALL
        
        FUNCTION PBMAIN () AS LONG
        '*** Cliff's Original
        ''    dim x, y, z AS LONG       '<--- Does not work
        '    LOCAL x, y, z AS LONG     '<--- Works
        '    x=1
        '    y=2
        '    z=4
        '    MSGBOX "x+y+z=" + STR$(x+y+z)
        '
        '
        ''*** Mel Bishop version 7                   <--- Works in 8.03.0040
        '    DIM x AS LOCAL LONG      '
        '    DIM y AS LOCAL LONG      '
        '    DIM z AS LOCAL LONG      '
        '
        ''    LOCAL x as long
        ''    local y as long
        ''    local z AS LONG     '<--- Works
        '    x=1
        '    y=2
        '    z=4
        '    MSGBOX "x+y+z=" + STR$(x+y+z)
        '
        '
        '*** Cliff Modified from multi-Line to single line
            DIM x, y, z AS LOCAL LONG      '<--- Not work
            x=1
            y=2
            z=4
            MSGBOX "x+y+z=" + STR$(x+y+z)
        '******************************************************************************
        '*** So I would say the long and short of it, if declaring a variable
        '*** Break the habit of Dimensioning ("Dim") a variable and use "Local" instead
        '*** if you want to have multiple variables declared on the same line
        '*** (Someone else may have a better explanation though)
        '******************************************************************************
        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


        • #5
          >Break the habit of Dimensioning ("Dim") a variable and use "Local" instead

          I will second that nomination.

          I never use DIM at all.

          All variables - scalars and arrays, are defined with..
          Code:
             LOCAL |STATIC |GLOBAL  varname      AS [i]vartype[/i]   ' scalars
             LOCAL |STATIC |GLOBAL  arrayname() AS [i]vartype[/i]   ' arrays
          Arrays are REDIM'd when they need elements defined.

          I also like that this way allows all array variables to be defined in the same physical area of the source code where the scalar variables used in a prodedure are defined.. i.e., everything is one place.

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

          Comment


          • #6
            Thanks Folks

            Never realized that you could declare variables straight off with local/global/static.

            Fwiw, I've just tested it in the console compiler 4.04.0042 and it works just fine.

            Here's the program code

            Code:
                STATIC x,y,z AS LONG
                x=1
                y=2
                z=3
                PRINT x,y,z
            Thanks again.
            John,
            --------------------------------
            John Strasser
            Phone: 480 - 273 - 8798

            Comment

            Working...
            X