Lesson 2
Lesson 2
GBasic Introduction
GBasic is a variant of the Basic Language included in GoDB platform as the scripting language.
GBasic programs are usually associated with a form and have the same name as
that of the form.
For example
Home.Frm will have an associated BAS file in the project called home.bas.
When Home.Frm is loaded, GBasic scripting engine loads home.bas and executes
all statements, till the END statement is not encountered.
Home.bas
a=10
b=20
print a,b
END
sub Button1_click
msgbox "Button clicked"
endsub
The program can also have subroutines that are event handlers for controls
in the form, like a mouse click event on a control.
Event Handlers have names that are of the format ControlName_EventName
Ex: Button0_Click
GBasic has two types of variables, numeric and string variables.
String variables and functions that return strings always terminate with a $.
Ex: A$,B$, Mid$(a$,1,2) etc
Variables without a $ at the end are considered to be numeric variables.
GBasic has two types of objects , Form Field Objects (Controls on a form) and Database Field objects.
Form field objects precede with a #
Ex: #UID , #UID$ etc.
Database field objects precede with a @
EX: @a.ID, @a.Name$
GBasic Operators
For a
Full List of operators Refer to Operator List
Operators For Numeric variables
+ ADD
- SUB
* MULT
/ Division
\ Integer Division
% MOD
^ Integer Exponent
GBasic supports the following Comparison Operators.
> Greater than
< Less Than
= Equal
<> Not Equal
GBasic supports the following Logical Operators.
AND OR
Operators For String Variables
+ Append
EX:
B$="Test1"
A$=" Hello " +B$
Note: When appending a large number of strings use Concat Function instead of the + operator.
You can also append numeric literals, variables and expressions to strings.
' Numeric Literal
A$=" Hello " + 100.25
'Numeric Variable
c=100
A$=" Hello " + b$ + c
'Numeric Expressions appended to strings
c=100
d=200
A$=" Hello " + b$ + (c+d*20)
A$=
(c*10+23) + " Hello There "
A$ = "Ubound of array is " + ubound(st$)
A$ = "Len of String is " + len("Hello")
A$ = "Len
of String is " + len("Hello") +20 +" Done "
A$ = "Len of String is "
+ ( len("Hello") +20 ) +" Done "
GBasic supports the following String Comparison Operators.
= Equal
<> Not Equal
EX:
if a$=b$ then
print "a is equal to b"
endif
String Escaping
Special chars like " etc are
escaped with a back slash '\' in a string Literal.
Ex
a$="Hello
there \"124\" "
a$="Hello there \n"
a$="C:\\Temp\\a.txt"
Special
Chars
\n - New Line
\r - Carriage Return
\t -
Tab
\" - Double Quote
\' - Single Quote
\\ -
Backslash
Other chars can be specified by specifying the HEX value
prefixed \x
EX:
a$="Hello there \x0D
\x0A "
Special Numeric Statements
You can
use C Style Increment ,Decrement and Cumulative Numeric
statements
Increment ++
Decrement --
Cumulative Numeric , +=
, -= , *=
etc
Ex:
a++
b--
a+=10
a-=20
Etc: Note these
commands cannot be used in expressions.
Ex: a = b+(c++)
is not allowed.
Numeric Literals in other bases Hexadecimal and
Binary.
GBasic Supports Hex Literals of the format
0xAABBCC
and binary Literals
0b1010
Ex:
' Hex Literal
a=0xEF10D
'
Binary Literal
b=0b10101
'String Append with Hex Literal
a$="Test "
+ 0xEF10 + "*" + 0b10101
' Convert Decimal to Hex
print
Format$("%x",1000)
The variables have three types of scopes
- GLOBAL
- LOCAL
- PAGE
Any variable that starts with ~ is a global variable and is
accessible from any function, subroutine and from any bas file in the project.
All variables declared outside the subroutines and functions becomes
a page-scope variable. This type of variable is accessible in any function and
subroutine within the same bas file.
Variables created in a function or a sub
have a LOCAL scope. They can be accessed only within the function.
Example
Home.Bas
Gnum1=10
'Page Variable - this variable can be accessed only in home.bas file.
~glob=20
'Global Variable - this variable can be accessed in all the .bas files.
sub test1
Lnum1=30
'local variable accessible in test1 sub only
print Gnum1
'gNum1 has PAGE scope, so it can be accessed in test1
print Lnum1
endsub
sub test2
print Gnum1
'OK
print ~glob
'OK
print Lnum1
'ERROR : LNum1 local variable accessible in test1 sub only
endsub
Home2.Bas
sub test3
print ~glob
'OK - if home.bas was loaded first and ~glob was initialized.
print Gnum1
'ERROR : Gnum1 is accessible only in home.bas
print Lnum1
'ERROR : LNum1 local variable accessible in test1 sub in home.bas only
endsub
GoDB Supports Block IF and Single line IFF statements.
ifConditionthen
Statements
Statements
else
Statements
Statements
endif
Ex:
if a > 120 then
print "a is greater than 120"
else
print "a is less than or equal to 120"
endif
print "Program Continuing"
iff condition then Statement1:Statements2
The : operator can be used for separating statements in a single line.
Ex:
iff a > 120 then print "a is greater than 120": goto
!ProceedNext
print "a is less than or equal to 120"
!ProceedNext print
"Program Continuing"
Note: GoDB Labels
start with a !.
GoDB supports two types of
Loops the For next and While Wend.
Loops can be aborted by a
Break Statement.
The control can be transfered to the loop
beginning using a Continue
statement.
For Variable = Start to End [Step Increment]
iff condition then
Break
iff condition then continue
Next
Ex:
for i =1 to 100
print i
next
for i =1 to 100 step 10
iff i > 50 then break
print i
next
While Condition
iff
condition then break
iff condition
then continue
Wend
Ex:
i=0
While i < 10
print i
i=i+1
wend
i=0
While i < 10
print i
iff i > 5 then break
i=i+1
wend
GoDB allows creating modular applications using subroutines and functions.
Subroutines are blocks of code that do not return values.
Functions are blocks of code that return values.
Subroutines are defined as.
SUB SubroutineName(Parameters)
iff condition then return
EndSub
To call a subroutine you have to use the CALL statement.
Ex:
Call test1
End
sub Test1
print "Inside Test "
endsub
Ex Sub with parameters:
Call test1(1,"test")
End
sub Test1(a,b$)
print "Inside Test with numeric and string parameter";a;b$
endsub
Note:
You should not jump out of a sub using a goto statement.
Use Return statement if you want to exit a subroutine.
In GoDB when strings are passed to a sub or function,
they are passed by reference.
String
literals , String expressions ,
Numeric variables and Numeric Expressions are passed by value.
All arrays variables
are passed by reference.
c$="test"
d=100
Call test1(d,c$)
print "after Test1 ";d;c$
End
sub Test1(a,b$)
print "Inside Test1 ";a;b$
b$="Welcome to GoDB "
a=120
endsub
' Passing Array
dimi
a(10)
a(0)=10
call test2(a)
print "After ";a(0)
End
sub Test2(Dimi b())
print "Before" ;
b(0)
b(0)=20
endsub
You can also pass Integers
and Double Variables to functions/subs by using the ByRef
Keyword..
dimi a
a=10
call test2(a)
print "After
";a
End
sub Test2(ByRefDimi b)
print "Before" ;
b
b=20
endsub
Functions
Functions are defined as
Function FunctionName(Parameters)
iff condition then return
return
100
Endfunction
Starting from
GODB 5.2 You can return values with return statement.
Function
Test
return 100
end function
FunctionI
Test1
return 100
end function
FunctionF
Test2
return 100.24
end function
FunctionS
Test4$
return "Welcome to Hello "
end function
The traditional way of returning value from a function is by
asigning the return value to the function Name.
Function
FunctionName(Parameters)
iff condition then return
FunctionName = ReturnValue
Endfunction
Functions are called by using them in an expression.
Ex:
t=TestFunc()
print t
End
function TestFunc
print "inside
TestFunc"
TestFunc=10
endfunction
Ex: With parameters
print TestFunc(1,"test")
End
function TestFunc(a,b$)
print "inside TestFunc ";a;b$
TestFunc=a+100
endfunction
Ex: String Functions
print TestFunc$(1,"test")
End
function TestFunc$(a,b$)
print "inside TestFunc ";a;b$
TestFunc$ = b$ +" added in func "
endfunction
Ex: String Functions Passed ByRef
c$="test"
print TestFunc$(1,c$)
print c$
End
function TestFunc$(a,b$)
print "inside TestFunc ";a;b$
TestFunc$ = b$ +" added in func "
b$="Modified in TestFunc"
endfunction
GoDB
also supports recursion .
Call recsub(1)
End
sub recsub(a)
iff a >10 then return
print a
call recsub(a+1)
endsub
Starting from GoDB 5.2 you can use Recursion
in functions too.
ret=RecFunc(1)
End
face=Courier>FunctionI RecFunc(a)
iff a >10 then return
print a
call RecFunc(a+1)
end Function
GoDB Supports creation of Numeric or string arrays using the DIM
statement.
DIMI - Integer Variables
DIMF - Floating
Point Variables
DIMS - String Variables
Numeric Array
Ex:
DimI a(100)
for i=0 to 99
a(i)=i
next
for i=0 to 99
print a(i)
next
Note: GoDB arrays are 0 based so when you declare an array of 100 elements the elements start from 0-99.
Multidimensional Numeric Array
Ex:
DimI a(100,10)
GoDB Supports up to Four Dimensions for Numeric Arrays.
Only single dimensional String arrays are supported.
DimS can be used to define Strings and
String Arrays.
Ex:
DimS a$ ' Single
String
DimS b$(10) ' Array of 10
Strings.
DimS b$(10,5) ' 2-D Array Strings.
One interesting thing about GoDB strings is that they can be accessed as an array of chars. You can also use the Mid$ function to get substrings.
a$="testing 123 "
for i=0 to len(a$)-1
print a$(i)
next
GoDB Strings are internally stored as Null terminated character strings. You need to consider this while setting characters at different positions.
This will work
a$="123"
a$(0)="A"
print a$
This will not work because there is a null char at a(3) so the print statement will print only till a(3)
a$="123"
a$(4)="A"
print a$
This will work.
a$="123"
a$(3)="A"
a$(4)="\x00"
print a$
Dim can be used to define String
with explicit sizes.
face=Verdana>Ex: String of size 10000
Dim a$(10000)
a$="testing 123 "
print a$
Ex: Array of 10 Strings with 100 chars
Dim a$(10,100)
for i=0 to 9
a$(i)="Hello there " + i
next
for i=0 to 9
print a$(i)
next
Array of strings can also be used like a two dimensional array of characters.
print a$(0,0)
Will print the first
character in the first string.
Multi dimensional strings (upto 3D) can be
declared using the Dim
Ex: 2D Array of 10x10 Strings with 100
chars
Dim a$(10,10,100)
You Can use
Redim to resize numeric arrays and StrExpand to resize Strings.
You can
initialize arrays by assiging the values enclosed in a bracket.
Dim
A(10)=(0,1,2,3,4,5,6,7,8,9)
You can also have new lines for
readability.
Dim
A(10)=(0,1,2,3,4,
5,6,7,8,9)
Strings Arrays.
Dim
st$(10)=("0","1","2","3","4","5","6","7","8","9")
For Multidimensional
Arrays you need to provide the data for all the elements.
Dim
A(9)=(1,2,3,
4,5,6,
7,8,9)
Advanced Subroutines and Labels
GoDB supports jumping using
the GoTo Statements.
iff a=1 then goto !Cond1
iff a=2 then goto
!Cond2
goto !ENDPROG
!Cond1
print "Cond1"
goto
!ENDPROG
!Cond2
print "Cond2"
!ENDPROG
To allow of
jumping based on a variable GoDB supports Label Dereferencing.
Using Label
Dereferencing the label can be decided based on a string variable and a
Dereferencing {
operator.
j$="!Cond"+a
goto {j$}
goto
!ENDPROG
!Cond1
print "Cond1"
goto !ENDPROG
!Cond2
print
"Cond2"
!ENDPROG
Similarly you can use Dereferencing
operator to call Subroutines too.
for i=1 to
3
a$="Test"+i
call
{a$}
next
END
Sub Test1
print
"Test1"
EndSub
Sub Test2
print
"Test2"
EndSub
Sub Test3
print
"Test3"
EndSub
Note: Dereferencing can be done to
Fields, and even Variables.
Field Dereferencing
face=Courier>b$="Grid1" 'Calling Object
Methods
#{b$}.Addrow("Data1,Data2,Data3,Data4,Data5")
face=Verdana>Variable Dereferencing
for i=1 to
10
dimS {"GI"+i+"$"}
Next
for i=1 to
10
GetImage({"GI"+i+"$"},10,10,20,20)
Next
Multiline
Commands
Multiline commands can be written using the _
operator.
a$="Hello There"
_
"Welcome here"
print a$
iff a>10 and b>10 and _
c>20 and d>20 then return
if a>10 and b>10 and
_
c>20 and d>20 then
print
"Hello"
endif
Note: The _ operator should have a preceding
space and should be followed by a newline and should not have any spaces (or
white spaces) following.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home