[Code Snippet] Using With statement for cleaner code

Tuesday, May 22, 2007 by SirSmiley | Discussion: DesktopX

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


thomassen
Reply #1 Tuesday, May 22, 2007 6:44 PM
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. }
thomassen
Reply #2 Tuesday, May 22, 2007 6:46 PM
hmm.... The code feature didn't keep my linebreaks. had to go back and insert BR tags manually... ...and no colouring?
SirSmiley
Reply #3 Tuesday, May 22, 2007 9:39 PM
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.
SirSmiley
Reply #4 Tuesday, May 22, 2007 10:01 PM
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
sViz
Reply #5 Wednesday, May 23, 2007 8:27 AM
Good tip. I use it all the time in forms and never thought to try it with everything else.
SirSmiley
Reply #6 Wednesday, May 23, 2007 1:19 PM
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?
ZubaZ
Reply #7 Wednesday, May 23, 2007 1:34 PM
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)
SirSmiley
Reply #8 Wednesday, May 23, 2007 1:52 PM
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"
SirSmiley
Reply #9 Wednesday, May 23, 2007 1:55 PM
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
ZubaZ
Reply #10 Wednesday, May 23, 2007 2:07 PM
Thanks.  Makes sense now.
 
SirSmiley
Reply #11 Wednesday, May 23, 2007 2:30 PM
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
RomanDA
Reply #12 Wednesday, May 23, 2007 3:03 PM
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
SirSmiley
Reply #13 Wednesday, May 23, 2007 3:22 PM
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

Please login to comment and/or vote for this skin.

Welcome Guest! Please take the time to register with us.
There are many great features available to you once you register, including:

  • Richer content, access to many features that are disabled for guests like commenting on the forums and downloading skins.
  • Access to a great community, with a massive database of many, many areas of interest.
  • Access to contests & subscription offers like exclusive emails.
  • It's simple, and FREE!



web-wc01