With - End With

If I remember correctly, in Visual Basic there was a “With … End With” construction. It preserved you from writing long expressions over and over.

e.g. instead of

Window1.TextArea1.StyledText.Bold=True
Window1.TextArea1.StyledText.Italic=True
Window1.TextArea1.StyledText.Underline=True

you could write

With Window1.TextArea1.StyledText
.Bold=True
.Italic=True
.Underline=True
End With

Is there anything similar in Xojo?

Hank

dim st as StyledText st = Window1.TextArea1.StyledText st.Bold = True st.Italic = True st.Underline = True

This is a fairly common request. But J Andrew Lipscomb’s example is the best way to do the same thing in Xojo. While the With - End With seems convenient, I find the the Xojo way to be a bit more explicit especially if you happen to have a long method that requires scrolling (in which case it seems to be a bit easier to read). Opinions vary on this, naturally, but I don’t miss With - End With.

The problem with JAL’s version :

is that you now have an extra local variable “st” hanging around. This can be annoying / dangerous in a long complex method.

Sometimes, I’ll wrap the whole thing in a n “if” block so that the “st” variable is local and goes out of scope

if true then
  dim st as StyledText = Window1.TextArea1.StyledText
  st.Bold = True
  st.Italic = True
  st.Underline = True
end if

Thanks for all tips!

Hank

It’s not just a syntactic sugar, a shortcut, it adds more instructions to the code, uses more memory and steals CPU cycles. :slight_smile:

I’m not sure you can prove this since there’s no With-End With in Xojo to compare it against.

It’s easy to understand:

Dim num as Long

You must agree that we allocated space for num, fill it with zeros and when out of scope it will need to be deallocated.

Allocation consumes memory space, initialization and deallocation consumes CPU.

Dim st as Object // allocates a reference . :wink:

Instead of “With”, I would love a new keyword for Xojo for declaring smart macro substitutions like “alias”:

[Local/Global] Alias (as | = )

The default scope is local.

Use:

Alias st as Window1.TextArea1.StyledText // Just a macro substitution instruction to the compiler
st.Bold = True
st.Italic = True
st.Underline = True
//
Alias st as Window1.TextArea2.StyledText // reusable, from now on st gets another value
st.Bold = True
st.Italic = True
st.Underline = True
//
Alias x§ as 3.1415926 * p / 2
Dim n1 As Single = x(98.7) // Same as writing 3.1415926 * 98.7 / 2
Dim n2 As Single = x(32.1)

[quote=40846:@Rick Araujo]It’s easy to understand:

Dim num as Long

You must agree that we allocated space for num, fill it with zeros and when out of scope it will need to be deallocated.

Allocation consumes memory space, initialization and deallocation consumes CPU.

Dim st as Object // allocates a reference . ;)[/quote]

If it were strings, I would agree with you but here you’re talking about 8 or 4 bytes. I don’t remember the size of the stack but I’m sure that even a few dozens of references or Integer, Double or Int64 doesn’t make [edited] a big difference.
I always cache references to object in my code as much as I can, because I find it clearer, and I’m almost certain that accessing a local variable is much more CPU efficient than trying to retrieve again and again the same object in a namespace or in a Window/ContainerControl, etc…
But, I’m no expert in compiler stuff. The last assembler code I wrote was for Z80, just a few ( Coughing ) years ago.

On OS X, the main thread’s stack is 8MB and Xojo stacks default to 512KB.

It certainly can be more efficient, but my recommendation has always been to focus on code clarity and correctness. Then if things show up on performance traces, optimize.

[quote=40747:@Michael Diehr]The problem with JAL’s version :
is that you now have an extra local variable “st” hanging around. This can be annoying / dangerous in a long complex method.
[/quote]

another way to mimic With / End With while keeping the shortcut variable local could be using a For Each:

for each st as StyledText in array(Window1.TextArea1.StyledText)
  st.Bold = True
  st.Italic = True
  st.Underline = True
next

but ok, the “if true” way seems to be easier to read :wink:

Actually I believe you are all overthinking this…
It is not a compiler issue as much as it is a pre-processor issue.
If done (what I would consider) properly. The compiler sees exactly what it sees today.

YOU type

with xyz 
    .property="fred"
    .count=9
   .abc=47
end with

The pre-processor changes it… to

   xyz .property="fred"
    xyz.count=9
   xyz.abc=47

which is exactly what you would have to type today.
No CPU cycles… no extra memory allocations.

The “WITH” keyword simply “remembers” the object named… and any property starting with a period gets that object pre-pended, until then END WITH

Sure there are changes to the IDE, etc… but the resultant compiled application would be the same in both cases.

At least the compilers I have written produce the same code :smiley: