The syntax of the With..End_With control structure is simple:
WITH Object Name ... END WITH
With btnCancel .Text = "Cancel" End With ' Equivalent to the statement: btnCancel.Text = "Cancel"
The strength of the With… End_With structure is only visible if, for example, you have to set many property values for an object, as you can see in the next section of the source code from the file .. /src/gb. form/ListContainer. class.
Example 2
Public Sub _new() $hScrollView = New ScrollView(Me) As "ScrollView" $hScrollView.Resize(80, 60) $hScrollView.Name = Me.Name & ".ScrollView" $hTimer = New Timer As "Child" $hTimer.Delay = 80 With $hScrollView .Border = True .ScrollBar = Scroll.Vertical .Background = Color.TextBackground .Arrangement = Arrange.Vertical $iSaveBackground = .Background $iSaveForeground = Color.TextForeground End With Me._Container = $hScrollView Me.Arrangement = Arrange.Vertical End ' _new()
Since Gambas 3.5 you can also use the With..End_With syntax for elements of an array or a collection in two different spellings:
Example 3
Public Sub Main() Dim hArray As New String[] Dim hCollection As New Collection Dim i As Integer hArray.Add("Maria") hArray.Add("Peter") With hArray Print .[0], !"1" ' !"1" ist äquivalent für .[1] End With hCollection["1"] = "Maria" hCollection.Add("Peter", "2") hCollection["3"] = "Uta" hCollection["4"] = "Eva" With hCollection !"5" = "Susi" ' Equivalent for.["5"] = "Susi." For i = 1 To .Count Print .[Str(i)], Next End With End ' Main()
The procedure Main ()/ creates the following output in the console:
Maria Peter Maria Peter Uta Eva Susi
If you take a close look at the three examples, you will surely recognize them: You can save a lot of paperwork with the With..End_With structure and give the source code structure - that's all.
Die Syntax der With…End_With-Kontrollstruktur ist einfach:
WITH Object Name ... END WITH
Beispiel 1
With btnCancel .Text = "Cancel" End With ' Äquivalent zur Anweisung: btnCancel.Text = "Cancel"
Die Stärke der With…End_With-Struktur zeigt sich erst dann, wenn Sie zum Beispiel viele Eigenschaftswerte für ein Objekt setzen müssen, so wie Sie das im nächsten Quelltext-Ausschnitt aus der Datei ../src/gb.form/ListContainer.class sehen.
Beispiel 2
Public Sub _new() $hScrollView = New ScrollView(Me) As "ScrollView" $hScrollView.Resize(80, 60) $hScrollView.Name = Me.Name & ".ScrollView" $hTimer = New Timer As "Child" $hTimer.Delay = 80 With $hScrollView .Border = True .ScrollBar = Scroll.Vertical .Background = Color.TextBackground .Arrangement = Arrange.Vertical $iSaveBackground = .Background $iSaveForeground = Color.TextForeground End With Me._Container = $hScrollView Me.Arrangement = Arrange.Vertical End ' _new()
Seit Gambas 3.5 können Sie die With…End_With-Syntax auch für Elemente eines Arrays oder einer Collection in zwei unterschiedlichen Schreibweisen nutzen:
Beispiel 3
Public Sub Main() Dim hArray As New String[] Dim hCollection As New Collection Dim i As Integer hArray.Add("Maria") hArray.Add("Peter") With hArray Print .[0], !"1" ' !"1" ist äquivalent für .[1] End With hCollection["1"] = "Maria" hCollection.Add("Peter", "2") hCollection["3"] = "Uta" hCollection["4"] = "Eva" With hCollection !"5" = "Susi" ' Äquivalent für .["5"] = "Susi" For i = 1 To .Count Print .[Str(i)], Next End With End ' Main()
Die Prozedur Main() erzeugt folgende Ausgaben in der Konsole:
Maria Peter Maria Peter Uta Eva Susi
Wenn Sie sich die drei Beispiele genau ansehen, werden Sie sicher erkennen: Sie können einiges an Schreibarbeit mit der With…End_With-Struktur sparen und dem Quelltext Struktur geben – mehr nicht.