NET Compiler examples
Contents
Overview
The goal for the development of the HBasic compilers should be that every
compiler is capable of compiling every syntactical correct program. Currently
the NET compiler has two main differences to the other compilers. First difference
is that it cannot handle GUI's so that program functions cannot be started
by a GUI event like a mouseclick (typical function button1_clicked). If has
to be started by a function called Main which will be called by the environment
when starting the program. Second the NET compiler handles all definitions
in and object oriented way which means you have to put every method into the
body of a class definition.
In this document we will show some simple sourcecode examples that are similar
to code examples we used for the normal HBasic interpreter or compiler but
have been adapted for the NET compiler.
Hello world for the NET compiler
As a first example we show a simple program that displays the string "Hello
world" on the console when it will be compiled and started. You can see how
the declaration of the method main shound be surrounded by a class definition.
The main method has to be declared as static for the NET compiler because
otherwise the environment cannot find and start this method.
Class example
Static Sub Main()
Print "Hello NET world"
End Sub
End Class
Example hello_netcomp.bas: Example of simple
program for NET compiler.
Variables, values and operator
The second example should show that you can create variables, assign values
and combine them in expressions with operators in the same way as you would
do it in other HBasic programs.
Class example
Static Sub Main()
Dim i, j, k As integer
i = 11
j = 22
k = i + j
Print k
k = j / 2
Print k
End Sub
End Class
Example netcomp/net_assign.bas: Example of
simple expressions for NET compiler.
For..Next example
You can find some examples in the code_examples/netcomp directory of your
HBasic distribution that show you how different types of loops may be used
in the NET compiler programs. In this example we will show a For..Next loop
and in the next example the Do..Loop.
Class example
Static Sub Main()
Dim i As Integer
For i=1 To 20 Step 3
Print i
Next i
End Sub
End Class
Example netcomp/for.bas: Example of For-loop
for NET compiler.
Do..Loop and If example
This example shows a Do..Loop statement which will be terminated with an
Exit
statement. An
If statement examines if the end condition for the loop
has been reached.
Class example
Static Sub Main()
Dim i As Integer
i = 1
Do
If i > 10 Then
Exit Do
End If
Print i
i = i + 1
Loop
End Sub
End Class
Example netcomp/net_doloop.bas: Example of
do..loop for NET compiler.
Call method with parameter
In this example we will call a method which has been defined in the same class,
pass some parameter value to the method and display the return value, which
is the sum of the parameter values.
Class example
Sub add( p1 As Integer, p2 As Integer ) As Integer
add = p1 + p2
End Sub
Static Sub Main()
Dim c As example
c = new example()
Print c.add( 11, 22)
End Sub
End Class
Example netcomp/net_sub_par.bas: Example of
method call for NET compiler.
Create instance of NET class
In this example we want to create an instance of a class "workclass" that
has been defined at another position in the code. We store the reference to
this object in the variable c and call a method for this class.
Class workclass
Sub show()
Print "Hello from NET class"
End Sub
End Class
Class example
Static Sub Main()
Dim c As workclass
c = New workclass()
c.show()
End Sub
End Class
Example netcomp/net_class_method.bas: Example
of simple class access for NET compiler.
Access field in NET class
In this example we want to read and write the value of a field from the class
"workclass".
Class workclass
Dim i As Integer
End Class
Class example
Static Sub Main()
Dim c As workclass
c = New workclass()
c.i = 1111
Print c.i
End Sub
End Class
Example netcomp/net_class_prop.bas: Read
and write field for NET compiler.