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.
@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.
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.
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.
[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.