You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
>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.
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
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.
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
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
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Leave a comment: