10.5.4 With... End With

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.

Download