This edition applies to release 1.6 of KBasic and to all subsequent released and modfications until otherwise indicated in new editions. Make sure you are using the correct edtion for the level of the product. The term „KBasic“ as used in this publication, refers to the kBasic product set (January 2007).
normal text appears in writing Arial. Here is an example here: This is normal text
Syntax and source code code appear in writing Courier New and grey background. Here the example:
Dim i As Integer
Important references and keywords are italically deposited: Arguments
The syntax of sub, function or statement in the KBasic help entry shows all elements, which are needed to correctly use the sub, function or statement. How you can understand those information shows the following lines.
Example: Syntax of the MsgBox-Function
MsgBox(prompt[, buttons] [, title] [, helpfile, context])
Arguments, which are inside of [ ], are optional. (Do not write these [ ] in your KBasic code). The only argument, what you have give the MsgBox-Function is the one for the showing the text: 'prompt'.
Arguments for functions or subs can be used with the help of their position or their name. In order to use the arguments defined with their position, you do not have to ignore the position written in the syntax. You must write them exactly in the same order they occur in the syntax. All arguments must be separated by a comma. Example:
MsgBox("The answer is right!", 0, "window with answer")
If you would like to use a argument with its name, use the name of the argument and colon and equals sign (:= and the value of the argument. You can write these named arguments in any order you wish. Example:
MsgBox(title:="window with answer", prompt:="The answer is right!")
Some arguments are written inside of {} in the syntax of functions or subs.
Option Compare {Binary | Text}
In the syntax of the 'Option Compare'-statement: { } together with | means that one of the elements must be written.(Do not write these { } in your KBasic code). The following statement defines that text will be compared and sorted without case sensitive.
Option Compare Text
Syntax of the 'Dim'-Statement
Dim VarName[([Indexes])] [As Type] [, VarName[([Indexes])] [As Type]] …
'Dim' is a keyword in the syntax of the 'Dim'-Statement. The only needed element is VarName (the name of the variable). The following statement creates three variables: myVar, nextVar and thirdVar. These variables are declared as 'Variant'-variables automatically (or 'Double' in 'VeryOldBasic Mode').
Dim myVar, nextVar, thirdVar
The following example declares a variable of type 'String'. If you declared the datatype of the variable explicitly, it will help KBasic to optimize the RAM-usage and will help you to find errors in your code.
Dim myAns As String
If you want to declare many variables in one line, you should declare every datatype of each variable explicitly. Variables without declared datatype get the default datatype, which is 'Variant'.
Dim x As Integer, y As Integer, z As Integer
X and y get in the datatype 'Variant' in the following statement. Only z has the 'Integer' datatype.
Dim x, y, z As Integer
You have to put ( ) or [ ] (for new style), if you want to declare an array variable. The indexes of the array are optional. The following statement declares a dynamic array named myArray.
Dim myArray[]
Dim sName As String Public sName As String Private sName As String Protected sName As String Dim Name[([Index])] [As Type] [, Name[([Index])] [As Type]] ... Dim Name [= Expression] [As Type] Dim Name [As Type] [= Expression] [Public | Protected | Private | Dim | Static] Name [= Expression] [As Type]
Dim yourName As String yourName = InputBox("What is your name?") MsgBox "Your Name is " & yourName
Type Name Name [(Index)] As Type ... End Type
REM this is a comment ‘ this is a comment as well /* start comment and stop comment */ /** start documentation comment and stop documentation comment */
1, 2, -44, 4453, +78
&HAA43
&B11110001
&O1234
21.32, 0.344, -435.235421.21, +67.8
212.23
45.3@
#1993-12-31#
„hello“
<?hello?>
True, False
Const Border As Integer = 377 Const Name = Expression Const Name [As Type] = Expression [, Name [As Type] = Expression] ... [Public | Protected | Private] Const Name [As Type] = Expression
s = New Control() s = New Timer(start, ende) objectVariable = New ClassName[(Arguments)] objectVariable = New ClassName() objectVariable = New ClassName
Class oak Inherits tree Variables / Constants / Properties / Types / Enumerations Constructors Destructors Functions Subs End Class
classname.classVariable objectname.instanceVariable
objectname.instanceVariable = 99
Static Sub myClassMethod() ... End Sub Sub myInstanceMethod ... End Sub
objectname.typefield
objectname.enumfield
objectname.classproperty
objectname.myMethod()
Class movies Protected sMovieName As String Sub printName print sMovieName End Sub Constructor movies(s As String) sMovieName = s End Constructor End Class Class movies2 Inherits movies Constructor movies2(ByRef s As String) Parent.movies(s + "2") End Constructor End Class Dim k As Integer = 9 Dim m As New movies2("final fantasy") m.printName()
If TypeOf myObject Is myClass Then TypeOf objectVariable Is ClassName
Parent.myVariable ' access parent class myVariable ' access current class (me)
Parent.myMethod() ' access parent class myMethod ' access current class (me)
Class A Dim i As Integer Function f() Return i End Function End Class Class B Inherits A Dim i As Integer ' hides variable i in A Function f() ' overwrites method f() in A i = Parent.i + 1 ' access A.i Return Parent.f() + i ' acces A.F() End Function End Class
Class plane Private wings As Integer Protected wings2 As Integer Private Function countWings() ... End Function End Class
Dim variableName(Index) As Type ' old style Dim variableName[Index] As Type Dim variableName[Index, Index, ...] As Type Dim variableName[Index To Index] As Type Dim variableName[Index To Index, Index To Index, ...] As Type
i(3) = 10 ' old style i[3] = 10 o[3, 88] = 10
UBound (arrayVariable[,(Dimension]) LBound (arrayVariable[,(Dimension])
Dim i [50 To 100] As Integer
Dim i(100, 50, 400) Dim sngMulti(1 To 5, 1 To 10) As Single
Dim a() As Integer Redim Redim variableName(Index) ' old style Redim variableName[Index] Redim variableName[Index, Index, ...] Redim variableName[Index To Index] Redim variableName[Index To Index, Index To Index, ...]
Erase arrayVariable[, arrayVariable]
If Expression Then Statement If Expression Then Statement : Else Statement If Expression Then LineNo If Expression Then LabelName: If Expression Then [Statements] End If If Expression Then [Statements] Else [Statements] End If If Expression Then [Statements] ElseIf Expression [Statements] Else [Statements] End If If Expression Then [Statements] ElseIf Expression [Statements] ElseIf Expression [Statements] Else [Statements] End If If Expression Then [Statements] ElseIf Expression [Statements] End If
IIf(Expression, ThenReturnExpression, ElseReturnExpression)
Select Case Expression Case Expression [Statements] Case Expression [Statements] End Select Select Case Expression Case Expression [Statements] Case Expression To Expression [Statements] Case Is Expression [Statements] Case Else [Statements] End Select Select Expression ' modern style Case Expression [Statements] Case Expression [Statements] End Select
Switch(Expression, ReturnExpression[, Expression, ReturnExpression, ... ])
Choose(Expression, ReturnExpression[, ReturnExpression, ... ])
GoTo {lineno | label:} GoTo myExit: GoTo nextStep:
Sub FormatOrder () With myclass .Value = 30 End With End Sub Sub setValue () With j(3) .e.bkname = "Frankfurter Zoo" With .e .isbn ( 99 ) = 333 End With End With End Sub
For variable = beginExpr To endExpr [Step Expression] [Statements] Next [variable]
Do While Expression [Statements] Loop
Do [Statements] Loop Until Expression
Do [Statements] Loop While Expression
Do Until Expression [Statements] Loop
While Expression [Statements] WEnd
While Expression [Statements] End While
Exit For Exit Do
Iterate For Iterate Do
Sub Name([Argumente]) [Statements] End Sub Sub Name([Argumente]) [Throws Name, ...] [Statements] End Sub
Function Name([Argumente]) [As Type] [Statements] End Function Function Name([Argumente]) [As Type] [Throws Name, ...] [Statements] End Function
Name As Type [ByVal | ByRef] Name As Type [ByVal | ByRef] Name [As Type] [ByVal | ByRef] Name [()][As Type] [ByVal | ByRef] [Optional] Name [()][As Type] [= Expression]
Sub PassArg(strName As String, intAlter As Integer, gebDatum As Date) Print strName, intAlter, gebDatum End Sub PassArg(Frank", 26, #2-28-79#) PassArg(intAlter:=26, gebDatum:=#2/28/79#, strName:="Frank") MsgBox(Title:="Aufgabe-Dialogfeld", Prompt:="Aufgabe erledigt!") Sub OptionaleArg(strPLBereich As String, Optional strLand As String = "Deutschland") ... End Sub Sub OptionaleArg(strLand As String, Optional intZBezirk As Integer, _ Optional strLand As String = "Deutschland") If IsMissing(intZBezirk) And IsMissing(strLand) Then Print strPLBereich ElseIf IsMissing(strLand) Then Print strPLBereich, intZBezirk ElseIf IsMissing(intZBezirk) Then Print strPLBereich, strLand Else Print strPLBereich, intZBezirk, strLand End If End Sub
Sub OptionaleArg(strLand As String = "Deutschland") Print strLand ' even strLand is not passed it contains Deutschland End Sub
Sub nadja(ByRef z As Integer, ByVal h As Double, Optional j As Integer, ParamArray b() As Variant) Print "z = " + z Print "h = " + h If Not IsMissing(j) Then Print "j = " + j End If Dim i As Integer For i = LBound(b) To UBound(b) Print "b(" + i + ") = " + b(i) Next i End Sub nadja(j := 33, h := 12.2, z := m, b[12] := "12 hello", b[5] := 555, b[7] := "7 ho")
Sub Main() MultiBeep 56 Meldung End Sub Sub MultiBeep(Anzahl) For n As Integer = 1 To Anzahl Beep Next n End Sub Sub Meldung() MsgBox "Zeit für eine Pause!" End Sub
Exit Sub Exit Function
Function Name([Argumente])[As Type] [Statements] End Function Function Name([Argumente])[As Type] [Throws Name, ...] [Statements] End Function
Return Expression
FunctionName = Expression
varname.classproperty = 99 Print varname.classproperty
Property Set Name(Argument) [Statements] End Property Property Get Name(Argument) As Type [Statements] End Property Property Name As Type Get [Statements] End Get Set(Argument) [Statements] End Set End Property
Type varname.typefield = 99 End Type Type Name Name [(Index)] As Type ... End Type
varname.enumfield = 99 Enum End Enum Enum Name Name [= Expression] ... End Enum
[Abstract] Class Name Inherits ParentClassName [Static] Dim Name As Type [Static] Public Name As Type [Static] Protected Name As Type [Static] Private Name As Type Const Name As Type Public Const Name As Type Protected Const Name As Type Private Const Name As Type ... [Public | Protected | Private] Enum Name Name As Type ... End Enum ... [Public | Protected | Private] Type Name Name As Type ... End Type ... [Public | Protected | Private] Property Name As Type Get [Statements] End Get Set(Argument) [Statements] End Set End Property ... [Public | Protected | Private] Constructor Name([Arguments]) [Statements] End Constructor ... [Public | Protected | Private] Destructor Name( ) [Statements] End Destructor [Static] [Public | Protected | Private] Function Name([Arguments]) [As Type] [Throws Name, ...] [Statements] End Function ... [Static] [Public | Protected | Private] Sub Name([Arguments]) [Throws Name, ...] [Statements] End Sub ... [Public | Protected | Private] Slot Name([Arguments]) [Statements] End Slot ... [Public | Protected | Private] Signal Name([Arguments]) [Statements] End Signal ... End Class
Module oak Variables / Constants / Types / Enumerations Functions Subs End Module
modulename.moduleVariable moduleVariable
modulename.moduleSub(99)
Sub myModuleSub ... End Sub Function myModuleFunction ... End Function
modulename.myModuleSub()
modulename.typefield
modulename.enumfield
Module Name Dim Name As Type Public Name As Type Private Name As Type Const Name As Type Public Const Name As Type Private Const Name As Type ... [Public | Private] Enum Name Name As Type ... End Enum ... [Public | Private] Type Name Name As Type ... End Type ... [Public | Private] Function Name([Arguments]) [As Type] [Throws Name, ...] [Statements] End Function ... [Public | Private] Sub Name([Arguments]) [Throws Name, ...] [Statements] End Sub ... End Module
Throw ExceptionObject
Try [Statements] Catch (Name As Exception) [Statements] End Catch Try [Statements] Catch (Name As Exception) [Statements] Catch (Name As Exception) [Statements] End Catch Try [Statements] Catch (Name As Exception) [Statements] Finally [Statements] End Catch
Catch (Name As Exception) [Statements] Finally [Statements] End Catch
By Bernd Noetscher www.kbasic.com