Is there an Equivalent to .NET's With End With

Is there an Equivalent to .NET’s With End With

With this function You save time writing code and add multiple properties.
Example:

For adding several attributes to this TextField:

[quote] txtCIEC.Enabled
txtCIEC.Italic
txtCIEC.Bold
txtCIEC.Text = “Hola”
txtCIEC.TextColor = RGB (247,48,32)[/quote]

Using “With” in .NET will be:

1 Like

No, Xojo does not have With…End With.

But you can do things to make it easier on yourself, like:

dim o as MyObject = MyVeryLongObjectNameObject
o.Prop1 = "this"
o.Prop2 = "that"

You can limit the scope of such a reference this way:

if true then
  dim o as MyObject = MyVeryLongObjectNameObject
  o.Prop1 = "this"
  o.Prop2 = "that"
end if

//
// o is gone now
//

This comes up every other year. Kem’s solution is just as good, IMO.

The nearest that I can think of in Xojo is to make a ‘Fluent Interface’. You would need to wrap or subclass every class/control that you want to do this with but you could come up with some generic wrappers that use introspection. Your classes would have setter methods that return a reference to the instance so you could write something like:

txtCIEC.Enabled().Italic().Bold().Text("Hola").TextColor(247,48,32)

Here is a wikipedia link with some Fluent Interface examples in various languages

If you want to be more ‘fluent’ then you can choose method names that read better like:

txtControl.MakeItalic().MakeBold().SetTextTo("Hola").Enable()