Date: 02-16-2022
Return to Index
created by gbSnippets
'Dimensioning an array is NOT handled the same in all languages. There are
'seven PowerBASIC statements used to dimension or re-dimension arrays.
'In general, the Global, Local, Static and Threaded statements create an array,
'but do not dimension the array. These must be followed by the use of either
'Dim or ReDim. Dim can be used once to dimension the array, whereas ReDim
'can be repeatedly used to (re)dimension the array.
'Dim/ReDim can create and dimension an array at the same time, requiring no
'other statement. The PowerBASIC Erase statement can also be used with
'Dim to give the same results as ReDim.
'Primary Code:
'Create Global Scope Arrays
Global 'must be folowed by use of Dim/ReDim
Global x() As Long
Dim x(5)
'Create Local Scope Arrays
Dim, ReDim 'do not require use of other statements, dimensions the array
Dim x(5) As Long 'x() cannot have been pre-dimensioned in same scope
ReDim y(5) As Long 'y() does not have to pre-exist, cannot change Type
Local, Static, Threaded 'must be followed by use of Dim/ReDim, does not dimension the array
Local x() 'must follow Local with empty parentheses
Dim x(5) 'Dim/ReDim must follow Static before x() can be used
Static x() 'must follow Static with empty parentheses
Dim x(5) 'Dim/ReDim must follow Static before x() can be used
Threaded x(0) 'must follow Threaded with empty parentheses
Dim x(5) 'Dim/ReDim must follow Static before x() can be used
'Dimension Existing Arrays
Dim '1st time dimensioning of array created by Global, Local, Static, Threaded
'see above examples
ReDim 'Re-dimensioning of array created by Global, Local, Static, Threaded
'see above examples
Erase/Dim 'When used together, same effect as ReDim
Dim x(5) As Long
Erase x()
Dim x(10) As Long
'Assigning Dimensions
'When Dim/ReDim are used to assign dimensions, the following syntax variations
'are allowed.
Dim x(5)
Dim x(1:5)
Dim x(1 to 5)
'Compilable Example: (Jose Includes)
'This example simply demonstrates the various ways to create and dimension arrays.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global X() As Long 'empty parentheses required
Global hDlg as DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 50,10,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
Dim A(5) As Long 'stand alone way to create/dimension array
ReDim A(10) 're-dimensions existing array
ReDim A(20) 're-dimensions existing array, again
ReDim B(5) As Long 'stand alone way to create/dimension array
Local C() As Long 'empty parentheses required
Dim C(5) As Long 'Dim/ReDim must follow Static or C() can be used
Static D() As Long 'empty parentheses required
Dim D(5) 'Dim/ReDim must follow Static or D() can be used
Dim E(5) As Long
Erase E()
Dim E(10) As Long 'can change dimension, but not data Type
MsgBox "Safe!"
End If
End Function
'gbs_00327
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm