def • class

class

A Class is a special Gambas that describes the common features of objects having the same type.

Example of declaration and usage of an own defined class

Create a New Console project Add a Class (its name will be Class1) Then from the SUB Main create two instances of that Class1

See: You have all the Variables the twice: the same structure in each instance.

See: You need to create an instance using the NEW statement

The code example for the Class

' GAMBAS class file
Public f As Float
Public s As String

Public Sub p()

Print "p is executed"
f = 4.5
s = "from call"
End

The code example for the MMain Module

' Gambas module file
Public xc As Class1
Public yc As Class1

Public Sub Main()
' Class1.s = "Class1 from main"     -> ERROR -> Class1 is not STATIC
xc = New Class1
yc = New Class1
yc.s = "yc from main"
xc.s = "xc from main"
xc.p()
Print xc, xc.f, xc.s
Print yc, yc.f, yc.s
' PRINT Class1.s                    -> ERROR -> Class1 is not STATIC
End

See also