Třída

A Třída 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 Třída

' GAMBAS class file
PUBLIC f AS Float
PUBLIC s AS String

PUBLIC SUB p()

Print "p is executed"
= 4.5
= "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.= "yc from main"
xc.= "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

Viz také