DroidBasic General Manual
DroidBasic General Manual
Table of Contents
About This Document..........................................................................................................................4
Why You Should Read It......................................................................................................................4
Note Of Thanks....................................................................................................................................5
About The Author.................................................................................................................................5
Supporter..............................................................................................................................................5
Tell A Friend!........................................................................................................................................5
DroidBasic Needs Your Help!..............................................................................................................5
Give Us Feedback.................................................................................................................................6
New Set Of Manuals............................................................................................................................6
Installation............................................................................................................................................6
Introduction..........................................................................................................................................6
Java for Android...................................................................................................................................7
What is a Computer Program?.............................................................................................................8
Programming Languages......................................................................................................................8
History of the Name.............................................................................................................................8
All Kinds of BASIC.............................................................................................................................9
Compilers and Interpreters...................................................................................................................9
The Programming Process..................................................................................................................10
Attack of the Bugs..............................................................................................................................10
How much do I need to know to use it?.............................................................................................11
Introduction to the DroidBasic programming language.....................................................................11
What is DroidBasic?......................................................................................................................11
Briefly and well in a sentence........................................................................................................12
DroidBasic's Past, Present and Future...........................................................................................12
What is still needed to know about DroidBasic?...........................................................................12
DroidBasic is object-oriented........................................................................................................12
Stable.............................................................................................................................................12
DroidBasic is Fast..........................................................................................................................13
Rapid application development.....................................................................................................13
How Can I Get DroidBasic?..........................................................................................................14
On which platform is DroidBasic available?.................................................................................14
Software development with DroidBasic.............................................................................................14
Event controlled programming vs. traditional programming........................................................14
How does an event controlled application run?.............................................................................15
Three Programming Steps.............................................................................................................15
Objects and classes.............................................................................................................................15
Inheritance of classes.....................................................................................................................17
Statements and expressions................................................................................................................18
Multi-Line statements.........................................................................................................................18
Variables and Data Types...................................................................................................................18
Declaration of Variables.................................................................................................................19
Declarations of variables in different scopes.................................................................................20
Use of the 'Public'-Statement.........................................................................................................21
Use of the 'Private'-Statement........................................................................................................21
Page 1
Local variables...............................................................................................................................21
Assignment-statement....................................................................................................................22
Lifetime of variables......................................................................................................................22
Place of declaration........................................................................................................................22
Data Types..........................................................................................................................................23
Simple Data Types.........................................................................................................................23
Data Type Size...............................................................................................................................23
Class types/Objects........................................................................................................................24
Type generic object........................................................................................................................25
Comments...........................................................................................................................................25
Way of naming....................................................................................................................................25
Literals................................................................................................................................................26
Expressions.........................................................................................................................................26
Constants............................................................................................................................................27
Operators and Operands.....................................................................................................................28
Operators for calculating...............................................................................................................28
Increment and Decrement..............................................................................................................29
Comparison....................................................................................................................................29
Logical operators (Boolean Operators)..........................................................................................30
Other Operators..............................................................................................................................30
Operator order................................................................................................................................31
Avoiding name collision.....................................................................................................................31
Editing source code............................................................................................................................32
Working with objects..........................................................................................................................32
Create new objects.........................................................................................................................32
Use of Init functions......................................................................................................................32
Create a class.................................................................................................................................33
Classes are not executed, but methods are.....................................................................................33
Accessing objects...........................................................................................................................33
Accessing instance-variables.........................................................................................................34
Instance-methods...........................................................................................................................34
Calling methods.............................................................................................................................34
References to objects.....................................................................................................................35
Copying objects.............................................................................................................................35
Comparison of objects...................................................................................................................35
Creating object variables...............................................................................................................35
Declaration of an object variable...................................................................................................35
Assignment of objects to an object-variable..................................................................................36
Use current instance or object / Me...............................................................................................37
Subclassing and inheritance...........................................................................................................37
Hidden Methods.............................................................................................................................38
Overriding methods.......................................................................................................................38
Calling an overridden method........................................................................................................39
Hiding data.....................................................................................................................................39
Scope modifiers.............................................................................................................................40
Arrays.................................................................................................................................................40
Control of the program flow...............................................................................................................41
Decisions........................................................................................................................................41
Single decision - If.........................................................................................................................41
Select..............................................................................................................................................43
Page 2
Loop-statements.............................................................................................................................44
For Next.........................................................................................................................................44
For Each.........................................................................................................................................45
Other kind of loops........................................................................................................................46
Do While...Loop............................................................................................................................46
Explicitly leave a loop...................................................................................................................48
Explicitly test a condition..............................................................................................................48
Nested control structures...............................................................................................................48
Procedures / Methods.........................................................................................................................48
Sub-Procedure................................................................................................................................49
Function-Procedure........................................................................................................................50
Arguments......................................................................................................................................50
Named arguments..........................................................................................................................50
Writing function-procedure...........................................................................................................51
Call of sub-procedures and function-procedures...........................................................................51
Add comments to a procedure.......................................................................................................51
Hints for calling procedures...........................................................................................................52
Leave procedure.............................................................................................................................52
Use of return value after calling a function-procedure..................................................................52
Writing recursive procedures.........................................................................................................52
Functions........................................................................................................................................53
Function Return.............................................................................................................................54
Modifiers / Scopes.........................................................................................................................54
Local scope....................................................................................................................................55
Classes................................................................................................................................................55
Classes unlike procedures are not executed...................................................................................56
Edit Class.......................................................................................................................................56
The DroidBasic development environment........................................................................................56
Windows........................................................................................................................................57
Toolbar...........................................................................................................................................57
Editor.............................................................................................................................................57
Classes and objects of DroidBasic.....................................................................................................57
Projects...............................................................................................................................................58
Interfaces/Layouts..............................................................................................................................58
Appendix............................................................................................................................................58
Copyright............................................................................................................................................65
Page 3
About This Document
Welcome to this document, your guide for the development of your
DroidBasic applications. In this manual you find some necessary
information to successfully build your DroidBasic programs. If you
complete this document, you will be able to write some simple
DroidBasic programs.
When you begin with DroidBasic read the first chapters to learn about
the programming language. Then try out the examples shipping with
DroidBasic and of this manual. The principal purpose of this manual is
to give you an overview about the programming language at all. The
less experienced DroidBasic programmer receives an introduction to
the programming language DroidBasic. The object-oriented and other
features of it are explained by some examples of the most important
elements of DroidBasic.
You will find a complete reference of the Android frameworks and the
DroidBasic framework inside DroidBasic’s integrated development
environment and therefore this manual comes without a detailed
reference.
Page 4
Note Of Thanks
We are very grateful to all the people who helped me to complete this
manual.
Supporter
Thank you for proofreading this manual and for beta testing.
Tell A Friend!
Like it? Love it?
When you share DroidBasic Software, you will make your friends happy
and give us a hand in getting the word out around the world.
Page 5
With your support it will be possible to show the world that BASIC
development software do not have to be limited and difficult.
It is up to you!
Give Us Feedback
Please help us to improve this book. As the reader, you are the most
important commentator and critic of this document. We respect your
opinion and would like to know what improvements we could make. If
you find an error in any example program or in the text, sent an e-
mail. Thank you!
Please note that we cannot help you with technical problems related
to the topic of this book, and that due to the high volume of mail we
receive, we might not be able to reply to every message.
Installation
Simply unzip the download file and copy the DroidBasic application
where you like.
Before you can use DroidBasic, you must have installed the developer
tools of like Java and Android SDK. See the donwload page on
droidbasic.com for more details.
Introduction
You heard probably already much about DroidBasic and have probably
downloaded DroidBasic and played a little with the sample programs.
Page 6
And now you want to have your own DroidBasic programs... Have you
ever wanted to know how a computer program works? If so, there
might be a computer programmer somewhere inside you, waiting to
get out.
You might have found computer programming not only difficult but
downright frightening. Are you going nearly crazy, if you try to write a
simple script file? If you feel this way, this manual is here to prove that
programming your computer is fun, rewarding, and not difficult. But
before you get started with developing computer programs, you ought
to have a basic understanding of what programming is all about. You
probably have some ideas about what a program is and how it works.
After reading this introduction, you may find that your knowledge
about programming is good or you know... In either case, it is worth
it...
Page 7
What is a Computer Program?
A computer program is a list of instructions telling a computer what to
do. The computer follows these instructions or commands, one by one,
until it reaches the end of the program.
Programming Languages
Computers do not understand German or English or any other human
language. They cannot even understand BASIC, the computer language
upon which DroidBasic is based. Computers understand only one thing,
machine language, which is entirely composed of the numbers 1 or 0.
Programming languages like BASIC allow people to write programs in
an English-like language. The BASIC interpreter changes the program
into machine language so the computer can understand it. DroidBasic
programs are a dialect of the BASIC computer language that was
developed not to help computers, but to help people make sense out
of the numerical code machines use to function. DroidBasic replaces
many of the numbers used by machine language with words and
symbols we can more easily understand and remember. Moreover, it
enables a programmer to visually assemble a program’s window from
parts in a toolbox. It is much easier for you to communicate with your
computer as an software developer; instead of thinking as a machine
you can think as an human being.
Page 8
also see the name spelled in all capital letters. The BASIC acronym
stands for Beginner’s All-purpose Symbolic Instruction Code. A BASIC
program uses symbols and words (and a few numbers) that people can
understand. How it is possible that the computer can understand and
run BASIC?
When you load DroidBasic you are also loading a compiler. A compiler is
a special program that takes the words and symbols from a DroidBasic
program and translates them into machine language the computer can
understand. Your computer would not have any idea what to do with
your program without the compiler’s interpretation of your programs.
Many computer languages exist, including Java, C++ and BASIC.
However, all computer languages have one thing in common: they can
be read by humans and therefore must be converted to machine
language before the computer can understand them.
Page 9
The Programming Process
Now that you know something about computer programs, how do you
go about writing one? Creating a computer program is easy, though it
can be a long process. Writing a DroidBasic program requires
development steps similar to those you use when writing a paper or
report. The following list outlines these steps:
Most of the steps in the programming process are repeated over and
over as errors are discovered and corrected. Even experienced
programmers cannot write error-free programs unless the program is
extremely short. Programmers often spend more time fine-tuning their
programs than they do writing them initially. It is important to do the
fine-tuning, because we are not as logical as we like to think.
Moreover, human minds are incapable of remembering every detail
required to make a program run perfectly. When a program crashes or
does something unexpected we have to find errors hiding within code.
Computer experts say there is no such thing as a program without
bugs. After you start writing full-length programs, you will see how
true this statement is.
Is Programming difficult?
Page 10
minimal practice, you can write many useful and fun programs.
Still, there is a lot you can do with DroidBasic whether you are
interested in writing utilities, small applications, or even computer
games. And, step by step, you will discover programming in DroidBasic
is not as difficult as you might have thought.
What is DroidBasic?
DroidBasic is a powerful programming language designed to be
intuitive and easy to learn. DroidBasic is a new programming language,
a further BASIC dialect related to Visual Basic and Java. More
precisely, DroidBasic is an object-oriented and event-controlled
programming language and is designed particularly for the needs of
GUI developers.
Page 11
Java developers feel that BASIC is a beginner language, but with
DroidBasic you can write many applications that may otherwise have
been written with the more difficult Java.
DroidBasic is object-oriented
That means for you as a programmer that you concentrate on the data
and on the methods in your application - how to manipulate the data,
instead of thinking only in procedures. If you get to know with it, how
powerfully this new object-oriented paradigma is, you will get used to
it, because you will see how easy it is to develop re-useable complex
application programs clearly and in a modular way. Contrary to other
programming languages DroidBasic is designed from the beginning as
an object-oriented programming language. So all things in DroidBasic
are objects; except the simple data types, like numeric and boolean
values.
Stable
DroidBasic is designed to write very reliable and durable software. Of
course, DroidBasic does not eliminate the need for quality control. It is
still possible to write unreliable software. However DroidBasic
Page 12
eliminates certain kinds of programming errors so that it is easier to
write reliable software.
DroidBasic is Fast
DroidBasic is a compiled programming language. Therefore, it is fast as
Java. DroidBasic programs are compiled into Java code, which is itself
compiled using the default compiler of the Java and Android SDK.
Page 13
How Can I Get DroidBasic?
You can buy DroidBasic Professional. You can get it directly from its
maker www.DroidBasic.com, where you can purchase a commercial
license.
First read this book. You can also get news and new information on
http://www.DroidBasic.org/. Many programming examples are posted
on the Internet.
Page 14
How does an event controlled application run?
An event is an action recognized by your windows or interface
controls. Objects in DroidBasic know some predefined events and react
to them automatically. To get a control to react to an event, write an
event procedure for this event. A typical application runs as follows:
Page 15
• Classes and objects in DroidBasic
• Creating objects
• Garbage collection to release unused objects
There are no class variables or class functions yet, all variables and
functions are useable together with objects only due to simplify the
object-orientation. Though class variables and functions might be
added in future releases. Though all functions and variables can be
used with the singleton pattern, which in the end acts like a class
variables and class functions.
For example, in the real world, cars consist of many objects like
windows, steering wheels, and so on. There is only one class (building
plan) for a window or steering wheel. Therefore there exists a class
window and a class steering wheel, but many objects window or
steering wheel, which are the instances of a class. An instance of a
class is also called an object of a class. Classes exist when you write
your code. Objects are created when DroidBasic runs your code using
your classes!
1. Super-class
2. Attributes (variables, constants...)
3. Methods/Procedures
Page 16
the new class. Attributes can be constants, or variables containing to a
class.
Inheritance of classes
Inheritance is an important part of object-oriented programming.
DroidBasic, supports single inheritance, meaning every class inherits
one parent class. It is not possible to inherit from many classes. In the
far future, it might be possible to use an interface to do many things
you could do with multi-inheritance. Unlike in Java, all objects in
DroidBasic are created when you declare them with a "Dim NAME As
TYPE" statement. You do not need to use "new" like in Java. For details
and exceptions read the language reference.
Page 17
Statements and expressions
Statements are a complete action in DroidBasic. Statements can
contain keywords, operators, variables, constants, and expressions.
Every statement can be categorized by one of the following:
Multi-Line statements
Currently, only single-line statements are supported. But you may use
_ to connect several lines.
Page 18
Declaration of Variables
Before using variables, you must declare them. You must define the
name and the data type of a variable.
To use a data type you have to write it for every variable, otherwise
the default data type (generic object) would be used.
You may assign a value to the variable directly in the same line of
declaration.
Names of variables must not be longer than 128 characters and must
not use all characters possible. Do not use periods, commas and other
non-writable characters. Only the underscore (_) is allowed.
Important! DroidBasic does differentiate between a variable name
written in lowercase or uppercase. In fact you cannot write it
differently in every line. Always write your variable names
consistently. So Named, named, naMED are different variables.
Page 19
Use the following rules to name functions, subs, constants, variables,
or arguments in DroidBasic: Use a letter (A-Z, a- z) or underscore (_) as
your first character
The name may contain numbers but must not start with a number.
You cannot use variables with the same name in the same scope. e.g.
you cannot declare variable ‘age’ two times in a function, but you can
declare ‘age’ in another function (at different places).
Page 20
‘Private’ implicitly). To be able to use it everywhere, you can use the
keyword ‘Public’.
Example:
Local variables
The value of a local variable is available inside the procedure only. You
cannot access the value from outside the procedure. This makes it
possible to have the same variable name for different variables in
different procedures without name collision. Example:
Sub test1()
Dim i As Integer
i = 1
End Sub
Sub test2()
Dim i As Integer
i = 19
Page 21
End Sub
Assignment-statement
Assignment-statements give or assign a variable a value or expression
with variables, constants, numbers, etc. An assignment always includes
a sign. The following example shows an assignment.
After the declaration of a variable, you can use it, e.g. make an
assignment.
Lifetime of variables
The lifetime of variables is the time in which the variable exists or has
a value. The value of a variable can be changed during its lifetime. If a
variable is outside its scope, it has no value. If a procedure is
executed, a variable exists after its ‘Dim’-statement. Local variables
exists until the procedure completed; after reentering the procedure
all variables are created again after their respective ‘Dim’-statements.
If you use instance-variables you must consider that they only exist
together with the instance (object) to which they belong.
Place of declaration
Declare every variable and every procedure inside a class.
Page 22
Syntax:
Data Types
Data types describe the type of data stored inside a variable. If you
declare a variable you must also give it a data type. DroidBasic
supports most common data types . You can use one of the following
data types:
Integer: -2^31 till +2^31 –1 (also known as Byte, Short, Int8, Int16,
Int32):
Stored as 32-bit numbers. You can use integer variables to simulate
enumerations, such as 0 = black, 1 = white and so on. The range for Byte
Page 23
is 0..255.
Long: -2^63 till +2^63 –1 (also known as Byte, Short, Int8, Int16, Int32,
In64):
Stored as 64-bit numbers. Values can be -2^63 >= and ⇐ +2^64. You can
use integer variables to simulate enumerations, such as 0 = black, 1 =
white and so on. The range for Byte is 0..255.
String:
Actually, it is a class of the Java Framework (String).
DateTime
They are stored as a date object.
Generic object
Stored as references.
Class types/Objects
Variables can store objects (actually, references to objects).
Page 24
Type generic object
The data type generic object is automatically used if you do not
specifiy a data type for an argument, constant, procedure or variable.
Dim myVar
Comments
The comment symbol (’) is used in many code lines in this book.
Comments can explain a procedure or a statement. DroidBasic ignores
all comments while compiling and running your program. To write a
comment, use the symbol (’) followed by the text of the comment.
Comments are printed in green on screen. DroidBasic recognizes
comments, as shown below.
Way of naming
When coding in DroidBasic, declare and name elements, like
procedures (functions and subs), variables, and constants and so on.
All names
Page 25
which means you must it write with the same lowercase or uppercase
letters everywhere.
Literals
Besides keywords, symbols, and names, a DroidBasic program contains
literals. A literal is a number or string representing a value. There are
different numerical literals.
Expressions
Expressions represent values. They can contain keywords, operator,
variables, constants or even other expressions. Examples for
expressions are:
myVar = 1 + 9
MyProcedure(1 + 9)
Page 26
Expressions are calculated at runtime and represents a value. The
result can be assigned to a variable or to other expressions listed
above.
Constants
Constants are similar to variables but they cannot change values.
When you declare a constant you assign a value to it that cannot be
altered during lifetime of your program. Example:
Use the same rules for declaring constants as for variables. A ‘Const’-
statement has the same scope as the variable statement. To declare a
constant inside a procedure, place the ‘Const’-statement inside this
procedure (normally one of the first statements). To declare a
constant as accessible for all procedures, place it within the class
where the procedures are.
You may change the visibility of a class const to other class by using
'Public' or 'Private'.
Public Const a = 34
Private Const b = "Hey"
Page 27
[Public | Private] Const Name [As Type] = Expression
+ Addition – Addition adds two numbers. If you add a number and a string
• / Division - Use to divide two numbers. The result is floating point. e.g. 2 / 5
Example: x is 10, y is 4
Page 28
MsgBox(x + y) ' is 14
MsgBox(x – y) ' is 6
MsgBox(x * y) ' is 40
MsgBox(x / y) ' is 2.5
MsgBox(x \ y) ' is 2
MsgBox(x Mod y) ' is 5
Normally, the result has the data type of the operand (= expression)
with more precision.
Comparison
DroidBasic has different operators to compare expressions: These
operators compare between two operands (expressions) and the result
is ‘True’ or ‘False’:
= Equal
e.g. x = 3
<> Unequal
e.g. x <> y
< Smaller
e.g. x < 3
> Bigger
e.g. x > 4
e.g. x <= 4
Page 29
e.g. x >= 3
AndAlso
A result is 0 when all bits are not set (binary 0000 0000). You can use
shift operators Shr or Shl.
Other Operators
• ^ Power IS NOT SUPPORTED THIS WAY – See the math function in the
language reference to get a workaround. Useful if you want to use
power. If they are many powers in one expressions they are
processed from left to right.
• . dot operator - Needed for calling methods
Page 30
Operator order
DroidBasic supports the operator order of Java. Normally, an
expression is executed from left to right following standard
mathematical rules.
1. . ( )
2. Not, !, (unary +), (unary -)
3. * / Mod \
4. & + -
5. < > < = >= = == ===
6. And
7. Or Xor
8. AndAlso
9. OrElse
Page 31
Editing source code
Editing source code is no different than editing text in a word
processor. You have a cursor showing the current position you can type
in. The find and replace commands within the editor work just as in
your word processor, as well as many other common commands.
Dim a As List
Page 32
a. The list object contains the values 1, 2, 3, 4, and 77 as integer
objects.
Create a class
A class can inherit from another class (be child of that class). The
declaration of a class consists of a class name and the super (parent)
class name. Furthermore, you have init functions, sometimes a
destructor (finalize method), some variables (instance-variables), and
procedures (methods, instance-methods) and, of course, some
constants.
Parts are:
• Variables
• Constants
• Functions
• Subs
• Events/Outlets/Signals/Slots
•
A class can only be declared inside a class file. It is not possible to
declare many classes in one class-file.
Accessing objects
Accessing objects is done by using the dot operator (.).
Example:
objectInstanceName.constantname
objectInstanceName.variablename
objectInstanceName.subname
Page 33
objectInstanceName.functionname
Accessing instance-variables
Accessing variables is done by using the dot operator (.).
Example:
myObject.variable
objectName.instanceVariable
Example:
Another example:
Dim o As myClass
o.instanceVariable = 99
Instance-methods
Instance-methods are only present when its object is present. You may
use ‘Me’ inside instance-methods.
Calling methods
Again, use the dot opertator (.) to access the methods.
myObject.myInstanceMethod()
Page 34
Sub myInstanceMethod
...
End Sub
References to objects
Using the ‘=’-Assignment you can assign a reference of an object
variable to another object variable. Both variables then reference the
same object.
Copying objects
In future version, if you would like to copy (or values), you must use
‘Copy’. Meanwhile copying have to be done manually.
Comparison of objects
The ‘=’-comparison operator tests if two object variables have the
same value.
Page 35
declaration statement (’Public’, ‘Private’). A variable, which
references an object, must be of type ‘Object’, or specific object type
(like ‘String’...).
' Object1
Dim Object1
' Object1
Dim Object1 As Object
Page 36
Object1 = Object2 ' object reference
myString = myString2
Example:
Sub otherMethod()
changeObjectColor(Me) ' statement inside the object
End Sub
The keyword 'Super' for accessing the super object of the current
object is not supported yet.
Page 37
Using the file extension tells DroidBasic which super class it uses for
the new class.
Because, every class has a super class, all classes forms together a
class hierarchy (or inheritance hierarchy). The inheritance hierarchy
like a tree, with a root and many twigs (many child classes). A famous
example is the paint event method of a View, which is considered to
be overridden by you to implement custom drawing for your control.
This is done when you would like to have a custom control.
Hidden Methods
An existing method, like a variable, in a parent class can be
overloaded by a method in the child class through inheritance, though
it is possible to access both methods with the same name. This is
called hiding, because it is not automatically visible anymore. Hidden
methods are also called overridden methods. Overriding takes place if
the child class has a method with the same methods’ signature (name,
return type, same arguments) as the parent class. The parent method
is now overridden.
In the future, not implemented yet: If you would like to access the
parent method, enter ‘Super’ dot (.) and the method name:
Overriding methods
Overriding occurs if the child class has a method with the same
method signature (name, return type, same arguments) as the parent
class. The parent method is now overridden. If this method is called
within the child class, the new defined method is used, even by the
parent methods (included by the child class). Overriding methods is a
very important technique in object-oriented programming. However,
do not confuse overriding with overloading. Overloading means having
many methods with the same name in a class but with different
method signatures (different return type or arguments).
Page 38
In the future, not implemented yet:
You can easily access hidden variables by casting its type to the parent
type or using the keyword ‘Super’. The difference is an internal
meaning because variables are different when used in child and parent
class (they actually use their own variable), while methods are used by
both (both parent and child class use the same method!).
If you override a method, you have two methods: the old method in
the parent class and the new method in the child class. Access the old
method explicitly using ‘Super.’ Access the new method using ‘Me’ or
the standard call of a method.
Hiding data
Hiding data (variables and constants...) is a very important technique
in object-oriented programming. Hiding data means not all data in a
class can be accessed by other classes, but through methods of the
same class in which the data is present so that it is only visible within
this class. This helps reduce mistakes when using internal data of a
class by other classes or modules. You have public methods and
variables that are accessible by all others and you have private
methods and variables that should only be used by the class itself and
those public methods. You automatically hiding data, when you use the
‘Private’ modifiers for variables and sometimes for methods. It
depends on the code you write. Further reasons for hiding data are:
Internal variables, which are externally visible, mixing up the API for
other coders etc. Hiding them leads to small, smart classes with a few
variables and methods visible to others.
Page 39
documentation time.
Scope modifiers
To hide variables or methods you must declare them as ‘Private’:
Private Dim wings As Integer ' only visible within this class
A private variable is visible for the class in which it has been declared.
This means the methods of this class can only use this private variable.
Private methods and variables are not visible to child classes of the
class in which they are declared. Non-private methods and variables
are always visible to child classes.
Some tips for using scope modifiers. Use ‘Public’ for methods and
variables that are used and accessible from everywhere.
Arrays
If you have worked with other programming languages, you understand
the concept of arrays. Arrays are variables that look like a chain. All
elements of that chain may have the same data type and can be
accessed by an index. These variables have one name but a different
index. You can use the array as one block or as one element of the
array.
Dim a As List
a = List("one", "two", three") ' modern style: declares a list with
three strings
Using arrays leads to cleaner, more flexible code because you can use
Page 40
loops to use or access thousands of variables (the arrays). Arrays have
an upper and a lower boundary and elements of that array are
between those boundaries. The important role of new arrays is rising,
because the more modern style of having classes imitating arrays with
more flexibility and functionality, especially when using Android, leads
to cleaner code. There are the DroidBasic Framework classes ‘List’ and
‘Dictionary’, which should fit in most situations.
Decisions
The term ‘decisions’ refers to the use of conditional statements to
decide what to execute in your program. Conditional statements test if
a given expression is ‘True’ or ‘False’. Then, statements are executed.
Normally a condition uses an expression in which a comparison
operator is used to compare two values or variables.
Single decision - If
A single decision is used to execute a set of statements if a condition is
set (’If’-statement). If the condition is ‘True’ then the statements
after the ‘Then’ are executed and the statements after the ‘Else’ are
skipped. If the condition is ‘False’, the statements after the ‘Else’ are
executed. If the item after ‘Then’ is a line number, a goto is executed.
If the condition is ‘True’ and there is no ‘Then’ statement on the same
line, statements are executed until a line with an ‘End If’ is found.
Syntax:
If Expression Then
[Statements]
End If
If Expression Then
[Statements]
Else
[Statements]
Page 41
End If
If Expression Then
[Statements]
Else If Expression
[Statements]
Else
[Statements]
End If
If Expression Then
[Statements]
Else If Expression
[Statements]
Else If Expression
[Statements]
Else
[Statements]
End If
If Expression Then
[Statements]
Else If Expression
[Statements]
End If
Example:
Dim i As Integer
Page 42
Dim n As Integer
If i 1 Then
n = 11111
Else If i = 2 * 10 Then
n = 22222
Else
n = 33333
End If
Select
The ‘Select’-statement is much more complicated than the ‘If’-
statement. In some situations, you may want to compare the same
variable or expression with many different values and execute a
different piece of code depending on which value it equals to. This is
exactly what the ‘Select’-statement is for. ‘Select’ introduces a multi-
line conditional selection statement.
Syntax:
Select Expression
Case Expression
[Statements]
Case Expression
[Statements]
Page 43
Case Else
[Statements]
End Select
Example:
Dim i As Float
Dim n As Integer
i = 4
Select i
Case 0
n = 0
Case Else
n = 999999
End Select
Loop-statements
The statements that control decisions and loops in DroidBasic are
called control structures. Normally, every command is executed only
one time but in many cases it may be useful to run a command several
times until a defined state has been reached. Loops repeat commands
depending upon a condition. Some loops repeat commands while a
condition is ‘True’, other loops repeat commands while a condition is
‘False.’ There are other loops repeating a fixed number of times and
some repeat for all elements of a collection.
For Next
The For-Next loop is useful when you know how often statements
should be repeated. For-Next defines a loop that runs a specified
number of times.
Syntax:
Page 44
determines the starting value of the loop counting. ‘Stop’ is a
numerical expression that determines the ending value at which the
loop will stop. To count in steps other than the default value of 1, use
‘stepExpression’ to determine a specific value for incrementing the
counter. If you use ‘Break’, this exits the ‘For Next’ loop immediately
and continues with the line following to the ‘Next’ statement. This is
usually used in connection with an ‘If’ clause (If something Then Break)
to exit the loop before its specified end.
The ‘Next’ statement is the lower end of the loop and increments
‘counter’ by the value given by ‘stepExpression’ or by 1 if no
‘stepExpression’ incrementation is defined. The loop repeats as long as
the value of ‘counter’ + 1 is smaller equal the value of ‘stop.’
Notes:
The speed of ‘For Next’ loops depends on the variable types used. ‘For
Next’ loops run fastest when ‘counter’ is an integer variable and
‘start,’ ‘stop,’ and ‘stepExpression’ are integer expressions.
For Each
In the future, not implemented yet:
Page 45
Other kind of loops
Use the following loops when you are not sure how often a command
should be repeated: ‘Do’, ‘While’, ‘Loop’. There is only one way to use
the keyword ‘While’ in order to test a condition within a ‘Do...Loop’-
statement.
You can test the condition before the commands inside the loop are
executed or you can test the condition after the commands of the loop
have been executed at least once. If the condition is ‘True’ ( in the
following procedure ‘SubBefore’) the commands inside the loop
execute. If you set ‘myNumber’ to 9 instead of 20, no command inside
the loop execute. Inside procedure ‘SubAfter,’ all commands execute
at least once because the condition is not true.
Sub SubBefore()
Dim Counter As Integer = 0
Dim myNumber As Integer = 20
Do While myNumber > 10
myNumber = myNumber - 1
Counter = Counter + 1
Loop
End Sub
Do While...Loop
A ‘Do While’ loop is a group of statements enabling you to define a
loop that will repeat until a certain condition remains ‘True’.
‘Do’: launches the loop and must be the first statement ‘Loop’: ends
the loop and must be the last statement ‘While’: lets the loop repeat
while the condition is ‘True’ Condition: a numeric or string expression
with a result of ‘True’ or ‘False’ ‘Break’: exits the loop at this very line
and lets the program continue behind the ‘Loop’-statement
‘Continue’: jumps directly to the ‘Loop’-condition
Syntax:
Do While Expression
[Statements]
Loop
Page 46
Examples: In the first example, the loop repeats as long as ‘xyz’
remains 5:
Do While xyz = 5
(lines of code)
Loop
Please note the lines of code will never be executed if ‘xyz’ is not 5
when entering the loop. You may use the condition with a number of
expressions like 'And' and 'Or':
Note: Please be careful when nesting several loops within each other.
Page 47
Explicitly leave a loop
Normally, a loop ends when its condition allows it. Sometimes it might
be useful to exit a loop before the condition is met. Manually exit a
loop using the ‘Break’-statement.
Syntax:
Break
Continue
Procedures / Methods
Your programs have been short, each designed to demonstrate a single
programming technique. When you start writing real programs,
however, you will discover they can grow to many pages of code. When
programs increase in length, they become harder to organize and read.
Professional programmers use modular programming to decrease the
Page 48
length of programs. Modular programming uses procedures. A
procedure is like a small program within your main program.
DroidBasic source code is inside a procedure, normally. A procedure is
a set of commands inside the written words ‘Sub’ and ‘End Sub’ or
‘Function’ and ‘End Function’. There are different types of
procedures. In fact, your program contains of methods only, declared
inside of classes. The main program, like in old BASIC languages, can
be considered to be represented by the method “Init” of the Global
class.
Sub-Procedure
A sub-procedure can have arguments, e.g. variables, expressions, or
constants that are given to the sub-procedure while calling it.
Syntax:
Sub Name([Arguments])
[Statements]
End Sub
Sub Name([Arguments])
[Statements]
End Sub
Page 49
Function-Procedure
A sub-procedure can have arguments, variables, expressions, or
constants that are given to the sub-procedure when calling it.
Function-procedures return values.
Syntax:
Arguments
For all practical purposes, a procedure can have as many arguments as
needed. You must be sure the arguments you specify in the
procedure’s call exactly match the type and order of the arguments in
the procedure’s sub line. To use more than one argument in a
procedure, separate the arguments with commas. You can pass one or
more arguments to a procedure. Keep in mind the arguments are
passed to the procedure in the order in which they appear in the
procedure call. The procedure’s sub line must list the arguments in the
same order they are listed in the procedure call.
If a procedure does not have arguments, you must not write any
expression or variable inside the braces when calling it. All statements
and commands are executed only after calling the procedure.
Arguments have names. If you have many arguments, you can separate
them by a comma (,). Every argument is like a variable declaration and
leads to automatically declared variables when the statements and
commands of a procedure are executed.
Syntax of arguments:
Name As Type
Name [As Type]
Named arguments
There are two ways to give arguments to a procedure. One is to name
the arguments, the other with giving values only. Arguments – the
Page 50
normal way:
Named arguments:
If you use named arguments, you must list the arguments in the right
order. It is probably more readable.
A named argument contains the argument name, colon (:) and equal
sign, and the expression the argument should have.
Writing function-procedure
A function-procedure contains commands and statements, which are
after ‘Function’ and before ‘End Function.’ A function-procedure is like
a sub-procedure but it can return a value. That is the only difference
between them. A function-procedure can also have arguments. You
must use the keyword ‘Return’ for return values.
Comments start with (’). This character tells DroidBasic to ignore all
text until reaching the end of line. Find more information about
comments and other comment styles in the previous chapter about
comments.
Page 51
Hints for calling procedures
When changing the name of classes, be sure to change all places the
class is used or DroidBasic runs into an error. You could use the file
replace in the DroidBasic menu.
Leave procedure
You can leave the procedure at any line inside of a procedure. For
that, use the keywords ‘Return’. For a function-procedure you need to
use “Return EXPRESSION”.
Syntax:
Return
Example:
If you do not need the return value, ignore it and use the function-
procedure like an ordinary sub-procedure.
Example:
myMsgBox("Task done!")
Example:
Function doError(Maximum)
Page 52
doError = doError(Maximum)
End Function
Example:
• Check the logic of the procedure. Use loops inside loops instead of
recursion calls.
Functions
Functions are very similar to subs in that both are procedures, but
functions return a value and can be used in expressions.
Syntax:
Page 53
‘Function.’ You should also define a return type for every function and
normally you does define the return type explicitly. For more
information about using procedures see the previous chapter.
Function Return
If you would like to set the return value of a function and exit the
function, use ‘Return’. With this statement you immediately leave the
function.
Syntax:
Return Expression
Modifiers / Scopes
Modifiers and scopes are a very important aspect of the DroidBasic
programming language and of modern programming languages in
general. Scopes tell DroidBasic when a variable, a constant, or a
procedure can be used, from which place in the source code files,
actually. There are different scopes (abstract places in code):
Scopes are useful when you would like to organize your program or
want to avoid name collisions of variables.
Page 54
do not use the same names for local variables as you do for instance
variables. Another way to get around this particular problem, however,
is to use ‘Me.variableName’ to refer to the instance variable and just
variable to refer to the local variable. By referring explicitly to the
instance variable by its object scope you avoid the conflict.
Local scope
A variable declared inside a procedure is not usable outside the
procedure, only the code of the procedure in which the variable has
been declared can use it. The following example demonstrates how a
local variable may be used. There are two procedures, one of them
has a variable declared:
Sub localVar()
Dim str As String
str = "This variable can only be used inside this procedure"
Log(str)
End Sub
Sub outOfScope()
Log(str) ' causes a compiler error
End Sub
Classes
A simple application can contain a simple interface, while all source
code is inside a class file. Create a class, which contains a methods,
which is useful for your interface.
You DroidBasic code is stored in classes. You can archive your code
within classes. Every class consists of the declaration part and the
methods you have inserted.
Page 55
You must not put several code of classes in one file.
Edit Class
It is not much different editing text in a word processor or in
DroidBasic’s IDE. You have a cursor showing the current position you
can type. You can also find and replace inside your source code as in a
word processor.
For example, the compiler might find an error in the source code. In
addition to displaying the error, DroidBasic opens the source file in the
text editor and jumps to the exact line in the source code where the
error occurred.
Page 56
Windows
DroidBasic windows are used to develop your programs, including
monitoring the status of your projects at any time. These
windows/programs include:
Toolbar
DroidBasic provides most common commands for quick access on a
toolbar.
Editor
DroidBasic has one editor for creating and managing DroidBasic
projects. You can use this editor to control the development of your
projects, such as editing source code and manipulating classes.
With the Layout Designer you add visual objects/controls to your forms
to assemble applications.
Page 57
Projects
Projects keep your work together. When developing an application in
DroidBasic, you work mainly with projects. A project is a collection of
files that make up your DroidBasic application. You create a project to
manage and organize these files. The project list shows each item in a
project. Starting a new application with DroidBasic begins with the
creation of a project. So before you can construct an application with
DroidBasic, you need to create a new project. Normally, you do this by
copying an existing project’s directory to a new location and changes
the files you need. You can add and remove files directly or in
DroidBasic's IDE itself.
Interfaces/Layouts
In your DroidBasic-application, interfaces are not only masks for
inputting and changing data but they are the graphical interface of
your application. In the eyes of the beholder, they are the application!
By creating your application using interfaces, you control the program
flow with events that are raised in the forms.
Appendix
Argument
Arithmetic Operations
Array
BASIC
Bit
Page 58
The smallest piece of information a computer can hold. A bit can be
only one of two values, 0 or 1.
Boolean
Boolean Expression
Branching
Byte
Compiler
Constant
Data Type
The various types of values that a program can store. These values
include Integer, Float, and Boolean and so on.
Decrement
Float
Page 59
The data type that represents the most accurate floating-point value,
also known as a double-precision floating-point value. Also see Floating
Point.
Empty String
Executable File
A file, usually an application, that the computer can load and run.
File
Floating Point
A numerical value that has a decimal portion. For example, 12.75 and
235.7584 are floating-point values.
Function
Increment
Infinite Loop
A loop that can’t end because its conditional expression can never
evaluate to true. An infinite loop ends only when the user terminates
the program. Also see Loop and
Page 60
Integer
Literal
A value in a program that is stated literally. That is, the value is not
stored in a variable.
Local Variable
Logic Error
Logical Operator
Loop
Machine Language
Mathematical Expressions
Page 61
mathematical expression. Also see Arithmetic Operations.
Method
Numerical Literal
A literal value that represents a number, such as 125 or 34.87. Also see
Literal and String Literal.
Numerical Value
Object
Order of Operations
Parameter
Procedure
Page 62
value. Also see Function.
Program
Programming Language
Program Flow
Relational Operator
Return Value
The value a function sends back to the statement that called the
function. Also see Function.
Runtime Error
Scope
Source Code
String
A data type that represents one or more text characters. For example,
in the assignment statement str1 = "I’m a string," the variable str1
Page 63
must be of the String data type.
String Literal
Subprogram
Unconditional Branch
User Interface
Variable
Variable Scope
Page 64
Copyright
Copyright © 2012 by www.droidbasic.com.
Page 65