I’ve always missed the with…end statements that I used in vb. So handy.
Is there a reason they can’t be done in Xojo or is this old school programming?
Dim theCustomer As New Customer
With theCustomer
.Name = "Coho Vineyard"
.URL = "http://www.cohovineyard.com/"
.City = "Redmond"
End With
...
This was first requested on May 1, 2008 in the current iteration of Feedback (case 2077), but I remember there being a much older request when I came to Realbasic sometime in 2003. At one point, on the old forums that Aaron setup, there was a lengthy discussion about including this and I think one of the devs said that they don’t want to implement because it would be a pain to accommodate the syntax for something that just the VB converts wanted (or something to that effect).
It’s something I wanted at one point, also, but certainly have lived without for an extremely long time now. It’s just as easy for me to do this:
Understood, just handy if you have long, descriptive variable names that are similar.
Autocomplete is good but I often have to type a lot of letters to filter out the name I want.
Doing it over and over on each line is a pain.
I missed for a while until I realized my code was so much more readable without it. Sometimes I have to pull out some old VB6 code that I haven’t looked at in years and it is painful to figure what goes with what. In short examples like yours very easy, but with nest With statements that are scatter over multiple lines of codes, some of it was a real mess. I like the explicit way of handling this, much easier to read later on.
When I remember correctly, it was never implemented because “with” would make code much less readable. I used it in my VB6 time, but never missed it in Xojo.
Whenever possible, I use classes because I have the feeling of having much more control over what I am doing and it makes my code clearer.
Also, remember that Xojo is Object-Oriented from the very start where VB in all its incarnations, was not.
My example is using a class.
Containing it within a with statement makes it clearer for me as all the property allocations are contained inside one block which (if implemented) can be collapsed.
It also has the advantage of automatic indentation which makes it cleaner to look at.
I think it is mainly preference, there is no right or wrong in the case of “with”. So maybe Xojo will add this in a future release, who knows. Let’s hope so because many people asked for it in the past.
I wish we had a variant, where With can activate several scoped ALIASES like
WITH p As ObjectX.dictionaryPropertyY , objNum As veryLargeOrConfuseObjectName
p.Value("aaa") = "a" // just to be less polluted and more lazy
p.Value("bbb") = "b"
p.Value("ccc") = "c"
p.Value("ddd") = "d"
objNum.v1 = 1 // Also renamed this object to clarify a bit the intention
objNum.v2 = 2
END WITH
Don’t confuse aliasing with creating a reference to some object, the above lines means just the same as:
Because I share a memory of how useful this was in VB6, I looked for it in the Xojo® literature. I found “With” as a reserved word. When I attempted to use it in code, the words “With” and “End With” turned blue, but the included code was ignored.
When I right-clicked on “With” the popup menu made a reference to “Serial Devices”. Clicking on the reference took me to a page where “With…End With” made no appearance whatsoever.
Incidentally, Many of the reserved words are blue and link to an explanation. “With” is not. Does this mean that it has been reserved for future use?
One can only hope so.