Creating NET code with HBasic

Compile programs with NET compiler

Starting with version 0.9 a new kind of compiler will be implemented in HBasic. Older versions could create the pcode for the interpreter and the assemblercode for standalone executables. The new compiler mode can create code for the .NET environment. This programs can be executed with the ilrun program of the DotGNU environment or used to link against other programs like C# sourcecode in the .NET environment.

Currently the NET compiler cannot compile programs with GUI interface. You therefore normally have to drop the Form description for your project. You can drop the GUI with a mouseclick of the right mousebutton on the Form (Gui) entry of your current form in the project tree.Select Drop GUI and the form description will be dropped for the matching sourcecode.

Instead of opening the first form HBasic will try to find a method called Main and start executing the program by calling this method. If you do not provide a main method in your program you may use the code as a NET library. You should therefore set the compiler options in the dialog project options to -shared so that the compiler knows that he should not search for a entrypoint method.


Hello world with .NET code


Class modmain
Sub Main
Print "Hello NET world"
End Sub
End Class


Example hello_world.bas:  Example of a normal HBasic method.


Assign variables and use simple expressions

This example should show how you can use some simple mathematical operations on variables.


Class modmain
Sub Main
Dim i, j, k As integer

i = 11
j = 12
k = i + j
Print k

k = i - 2
Print k

k = j * 2
Print k

k = j / 2
Print k
End Sub
End Class


Example netcomp/net_assign.bas:  Example of simple operations on variables.


Compile with "Run / Net Compiler". A file /usr/local/hbasic/hbasic.s with IL code and a net executable will be generated which could also be started with "ilrun <progname>".


Creating NET code with HBasic


The first version of HBasic was capable of creating and running it's own form of runtime code. Since it is helpfull for the developer that he may include as many working external libraries as possible HBasic can access class definitions within a NET library. Currently this NET libraries will normally be created with a C# compiler. In this form you can only import C# programs into your HBasic project.

With the NET compiler it will also be possible to create NET code from HBasic code. This will be included into the existing compiler which has to create another form of destination code. The code generated by the NET compiler can then be integrated into other C# or HBasic projects which use the NET execution environment.

This compiler is in a very early alpha state but will be extended in the following versions of HBasic step by step.

The next program shows an example how a program for the NET compiler might look like. Since there is currently no integration for forms and grafical elements in the NET library we show an example program that outputs a text line to the console.


Class
Sub Main
Print "Hello NET world"
End Sub
End Class


Example hello_world.bas:  Example of a HBasic program for the NET compiler.

Compile this program with "Run/ Net Compiler". A file /usr/local/hbasic/hbasic.s with IL code and a net executable will be generated which could also be started with "ilrun <progname>".