With...End statements vb style

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
...
1 Like

I’ve seen this topic come up every now and then
Best to offer is make a Feedback request and hope others sign onto (give points) to the initiative

1 Like

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:

var c as New Customer
c.Name = "Joe"
c.Age = 24

Or, better, using the Constructor:

var Joe as new Customer( "Joe", 24 )
2 Likes

Not very language neutral, my opinion

Yes it is convenient especially coming from Office VBA but not a deal breaker

1 Like

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.

1 Like

Yeah, I’d probably build a constructor to handle the most common ones. Save your hands a bit of typing, at least.

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.

2 Likes

In Excel the with…end was really helpful. The code was like

Chart.PartOfChart.OtherPartOfChart.SubPartOfChart.PropertyParent.Property = Value

When I had to change a lot of properties the with…end simplified the code. In Xojo I don’t see with…end as useful.

agree. i used it often for recordsets.

or is this old school programming

GameMaker2 use it today but different it would replace the “Me.” context.

With (theCompany) {
        Name = "Xojo, Inc"
        URL = "https://www.xojo.com"
        City = "Austin"
    }

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.

Chris

1 Like

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.

1 Like

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.

Chris

Only way to know is submit or check for previous FR and add points to it

<https://xojo.com/issue/2077>

Yeah, it looks like the original request when this case was migrated was from 2002.

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:

  ObjectX.dictionaryPropertyY.Value("aaa") = "a"
  ObjectX.dictionaryPropertyY.Value("bbb") = "b"
  ObjectX.dictionaryPropertyY.Value("ccc") = "c"
  ObjectX.dictionaryPropertyY.Value("ddd") = "d"

  veryLargeOrConfuseObjectName.v1 = 1
  veryLargeOrConfuseObjectName.v2 = 2

My WITH proposal is a metasyntactic feature, so when an error arises, the final form is shown, like:

WITH p As ObjectX.dictionaryPropertyY
  p.Valu("aaa") = "a"   // causes syntax error in line 2 for ObjectX.dictionaryPropertyY.Valu()
END
1 Like

Sounds like a legitimate FR

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.

Is there an active issue for it in the issue tracker? If not, I will create one because I find it very useful too.