User Tools

Site Tools


k10:k10.5:k10.5.4:start

10.5.4 With... End With

The syntax of the With..End_With control structure is simple:

WITH Object Name
  ... 
END WITH
  • Between the With and the end of the With instruction, an expression begins with a dot and establishes the reference (? object selector) to the object written after With.
  • You can also nest With..End_With structures. Example 1
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.

Download

10.5.4 With .. End With

Die Syntax der With…End_With-Kontrollstruktur ist einfach:

WITH Object Name
  ... 
END WITH
  • Zwischen dem With und dem Ende der With-Instruktion beginnt ein Ausdruck mit einem Punkt und stellt so den Bezug (→ Objekt-Selektor) zu dem nach With notierten Objekt her.
  • Sie können With…End_With-Strukturen auch verschachteln.

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.

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k10/k10.5/k10.5.4/start.txt · Last modified: 02.07.2018 (external edit)

Page Tools