With ...

In VB6 and VBA we have the With-statement:

[b]With[/b] MyObject .name = "John" .age = 20 [b]End With[/b]

This saves a lot of typing and creates more readable code.
Do we have and equivalent solution for the With-statement in Xojo ?

No.

Hmm… this is a missed opportunity to make coding smarter in a relative simple way.

I disagree with the readability claim here - especially for large classes where the “With” line may not be visible, but no such method exists with Xojo.

MyObject.Name = "John" MyObject.Age = "20"

Is much more readable and doesn’t care if the With line has scrolled off the screen.

I too am a advocate for the inclusion of “WITH”, but on other topics it seems this is not a popular feature for addtion…

And it is like most other features… if you Like it… you could use it… if you don’t like it, then don’t use it

@Dave S - My point isn’t that it’s not useful, but rather that the “better readibility” claim is “iffy” at best. The only usefulness I see is it requires less typing or makes it easier to port projects from VB to Xojo with it was used.

But… as Dave pointed out

“And it is like most other features… if you Like it… you could use it… if you don’t like it, then don’t use it”.

Here we go again :confused:

“With” may become a bigger with the new framework with much more use of namespaces.

Run from “with” like it was the plague. My medium size project I converted was 30k LOC in size and I had With statements all over the place. Translation was much more difficult than it would have been without all those With statements. And now when I have to go back into VB6 to review code, I cannot believe how much less clear the code is with all those statements starting with a dot. I was a big fan of With before Xojo, now I have no idea why.

With was one of those “neat ideas” that folks abused the heck out of and then it was too late to take it back

With MyObject
.name = “John”
.age = 20
End With

is simple enough

But when you start to nest them then it becomes easy to get identifiers confused

I’d just use a short hand alias if its really important

dim mo as = MyObject
mo.name = “John”
mo.age = 20

Which is why I thought at one point VB only alllowed one level of WITH (which would be fine by me)

I’m designing an iOS compiler right now… it will allow WITH… but only ONE level to avoid exactly this issue.

The other issue I’ve seen noted about “with” is “WTF is this line starting with a dot referring to ?”

Since you can do something like

With MyObject
// hundreds of lines of code
………………………
.name = “John”
.age = 20
End With

ie. there’s no way to restrict “other code” being inside the with you end up having to scroll way back just to see what the lines refer to, what its type is etc.

The MS Solutions Provider I used to work for strictly forbid the use of WITH as it reduced readability - not increased it.
It got abused in so many “creative” & “thoughtful” ways that it just finally got banned from any code we provided in client solutions.

I know folks have asked for it before in Xojo and its been rejected every time its come up for consideration.

I wondered how close you can get with current Xojo, like how an extension method can fill in for +=. Got as far as this syntax…

[code]Sub Action()

if WithObj(PushButton2) then

dot.Enabled = false
dot.Caption = "foobzy"

end

End Sub[/code]

using this module and inner class…

[code]Module WithModule

Private Property targetObj As Object
Private Property propDict As Dictionary
Public Property dot As DotClass

Public Function WithObj(obj As Object) As boolean
targetObj = obj

  propDict = new Dictionary
  dim props() As Introspection.PropertyInfo = Introspection.GetType(targetObj).GetProperties
  for i As integer = 0 to props.Ubound
     propDict.Value(props(i).Name) = props(i)
  next
  
  if dot = nil then dot = new DotClass
  
  return true

End Function

Private Sub processDot(name As String, value As Variant)
if propDict.HasKey(name) then
Introspection.PropertyInfo(propDict.Value(name)).Value(targetObj) = value
end
End Sub

Private Class DotClass
Public Sub Operator_Lookup(name As String, assigns value As Variant)
WithModule.processDot(name, value)
End Sub
End Class

End Module[/code]

Easy to break, uncheckable typing and no autocomplete. Looking back over the achievable syntax it’s pretty pointless, like Norman said just use a short hand alias.

This was exactly my point above.

[quote=70148:@Tim Jones]I disagree with the readability claim here - especially for large classes where the “With” line may not be visible, but no such method exists with Xojo.

MyObject.Name = "John" MyObject.Age = "20"

Is much more readable and doesn’t care if the With line has scrolled off the screen.[/quote]

I think I had read eons ago that VB6 was slightly faster using With, if it didn’t have to dereference the object on every line of code, but I have no idea if it is was true or not. I just saw the words “speed up” in the late 90’s and went with it. Flash-forward 12 years and I was converting a medium size program with hundreds and hundreds of With statements in some Subs that had too much code and a ton of objects and class names to put back in. I wish I had never went that route, because I now think that style of coding is harder to read and maintain.

Thinking logically, I doubt this is true. This is because if it was faster the compiler, I believe the compiler would choose a main reference for you without the with statement. I just cannot think how a ‘With’ statement would improve the performance of referencing.

Unless, there the speed varies on what you choose to use with a With statement. I have never written a compiler so I am just guessing.