[Code Snippet] Using With statement for cleaner code

So, I'm browsing through the MS Scripting Help file and stumble upon a statement I forgot about completely... from back in my Access days. I opened up Builder and found the With Statement works really well in DesktopX. I'm not using the vbscript tags because the seemed be striping the formatting.

With object
    statements
End With

Using it from within an object:

Sub MySub
'--Changes object size
With Object
   .Height = 128
   .Width = 128
   .ToolTipText = "This is My Object"
End With
End Sub

Referencing another object:

Sub MySub
'--Changes object size
With DesktopX.Object("AnotherObject")
   .Height = 128
   .Width = 128
   .ToolTipText = "This is My Object"
End With
End Sub

Within the timer function I found this to work also:

Sub Object_OnTimer100
'--Grows the object until timer is killed
With Object
   .Height = .Height + 20
   .Width = .Width + 20
End With
End Sub


3,209 views 13 replies
Reply #1 Top
Ditto for Javascript. (Haven't tried this in DesktopX, but the JScript language supports it.
Code: javascript
  1. With (Object)
  2. {
  3. Height += 20;
  4. Width += 20;
  5. }
Reply #2 Top
hmm.... The code feature didn't keep my linebreaks. had to go back and insert BR tags manually... ...and no colouring?
Reply #3 Top
Thanks. Don't know why I didn't check that out as I always use the big WSH Help file which contains everything.

hmm.... The code feature didn't keep my linebreaks. had to go back and insert BR tags manually... ...and no colouring?


Noticed this the other day. Hope it gets fixed soon as it's a nice feature.
Reply #4 Top
It seems to work with forms as well. Now that's where I can see it being really useful.

Code: vbscript
  1. Set frmfeed = DesktopX.CreateForm
  2. With frmfeed
  3. .AddPreference "title"
  4. .Preference("title").Type = "Text"
  5. .Preference("title").DefaultValue = "Title"
  6. .Preference("title").Caption = "Title"
  7. End With
Reply #5 Top
Good tip. I use it all the time in forms and never thought to try it with everything else.
Reply #6 Top
That's why I like throwing things out there. Because you never the uses someone will come up with.

On a simple object it's probably not that useful but, maybe would save a few bytes in the script. On a large script I'm wondering if it might slow things down slightly?
Reply #7 Top
OK . . I rode a short bus to work this morning . . so help me out learning something new.

What's it do better than some other way of doing something?  Why use it?  How to use it in the real world?

(sorry for being so clueless)
Reply #8 Top
IMO it's mostly an aesthetics thing but, it will save some bytes or more to your script.

Since I'm lazy I'll just use the forms example difference using five settings.

Normal Example:

Code: vbscript
  1. Set myform = DesktopX.CreateForm
  2. myform.AddPreference "title"
  3. myform.Preference("title").Type = "Text"
  4. myform.Preference("title").DefaultValue = "Title"
  5. myform.Preference("title").Caption = "Title"
  6. myform.AddPreference "title"
  7. myform.Preference("title").Type = "Text"
  8. myform.Preference("title").DefaultValue = "Title"
  9. myform.Preference("title").Caption = "Title"
  10. myform.AddPreference "title"
  11. myform.Preference("title").Type = "Text"
  12. myform.Preference("title").DefaultValue = "Title"
  13. myform.Preference("title").Caption = "Title"
  14. myform.AddPreference "title"
  15. myform.Preference("title").Type = "Text"
  16. myform.Preference("title").DefaultValue = "Title"
  17. myform.Preference("title").Caption = "Title"
  18. myform.AddPreference "title"
  19. myform.Preference("title").Type = "Text"
  20. myform.Preference("title").DefaultValue = "Title"
  21. myform.Preference("title").Caption = "Title"
Reply #9 Top
Using With Statement Example:

Code: vbscript
  1. Set myform = DesktopX.CreateForm
  2. With myform
  3. .AddPreference "title"
  4. .Preference("title").Type = "Text"
  5. .Preference("title").DefaultValue = "Title"
  6. .Preference("title").Caption = "Title"
  7. .AddPreference "title"
  8. .Preference("title").Type = "Text"
  9. .Preference("title").DefaultValue = "Title"
  10. .Preference("title").Caption = "Title"
  11. .AddPreference "title"
  12. .Preference("title").Type = "Text"
  13. .Preference("title").DefaultValue = "Title"
  14. .Preference("title").Caption = "Title"
  15. .AddPreference "title"
  16. .Preference("title").Type = "Text"
  17. .Preference("title").DefaultValue = "Title"
  18. .Preference("title").Caption = "Title"
  19. .AddPreference "title"
  20. .Preference("title").Type = "Text"
  21. .Preference("title").DefaultValue = "Title"
  22. .Preference("title").Caption = "Title"
  23. End With
Reply #10 Top
Thanks.  Makes sense now.
 
Reply #11 Top
The other thing to keep in mind when dealing with objects is that you can only reference one object using the With Statement but, they can be nested like if statements. If possible I'd reference the external object using this method.

For Example:
This code uses the with statement for the external object.

Code: vbscript
  1. '--position/aligned based on the main object
  2. Function posObject
  3. With DesktopX.Object("ExternalObject")
  4. .Left= Object.Right + 12 'Positions External Object to the left of main object
  5. .Top = Object.Top 'Aligns External Object Top to main object
  6. .ToolTipText = "This is My Object"
  7. End With
  8. End Function
Reply #12 Top
looks great. I use this a lot in the forms i have setup.
Look at some of the right-click menus.

haven't thought to use it in all the "object" type settings, but it looks good.

and no, im not dead.. yet.... I have a new tutorial coming .. soon
Reply #13 Top
Guess, I would've figured this out sooner if I actually looked at every widget/object's code.

RomanDA, damn you and your tutorials making me too lazy!

Edit: Haven't gotten Nested With Statements to work like in my old modules but, it works fine with the "DesktopX" namespace

Code: vbscript
  1. Const lLeft="objLeft",lMid="objMid",lRight="objRight",Label="txtLabel"
  2. Function sizeLabel
  3. With DesktopX
  4. .Object(lMid).Left = .Object(lLeft).Right
  5. .Object(lRight).Left = .Object(lMid).Right
  6. .Object(Label).Width = .Object(lLeft).Width+.Object(lMid).Width+.Object(lRight).Width
  7. '--mask adjustments
  8. Object.Width=.Object(lRight).Right
  9. Object.Height=.Object(lMid).Height
  10. End With
  11. End Function
  12. 'Called when the script is executed
  13. Sub Object_OnScriptEnter
  14. Call sizeLabel
  15. End Sub