Wie man einen constructor nutzt zurueck

Ein constructor ist eine Methode um ein Objekt zu initialisieren.
Ein constructor ermöglicht es , die Werte einer Vriable bereits beim Start festzulegen. Schauen sie sich das Beispiel DrawingArea - How to draw some figures. an. Dort finden Sie ein weiteres Beispiel , welches den Gebrauch eines constructors zeigt.

Das Programm

Die globale Variable X wird beim Start durch den constructor festgelegt. Wenn der Button angeclickt wird , wird X in die Texteigenschaft des Label Feldes eingetragen.




Der Code:

' Gambas class file

STATIC PUBLIC SUB Main()

DIM hForm AS Fmain

hForm = NEW Fmain

hForm.Show

END

X AS String

'A variable has to be global in order for it to be visible in different methods

'(_new() and Button1_Click()), see Locale und globale Variablen

PUBLIC SUB _new()

X = "cool"

END

'The constructor is a method, called: PUBLIC SUB _new()
'Here, the global variable X is set to "cool".

PUBLIC SUB Button1_Click()

TextLabel1.Text = "Constructors are " & X & "!"

END