[Code Snippet] Using With statement for cleaner code
Tuesday, May 22, 2007 by SirSmiley | Discussion: DesktopX
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
Reply #2 Tuesday, May 22, 2007 6:46 PM
Reply #3 Tuesday, May 22, 2007 9:39 PM
Noticed this the other day.
Hope it gets fixed soon as it's a nice feature.
Reply #4 Tuesday, May 22, 2007 10:01 PM
- Set frmfeed = DesktopX.CreateForm
- With frmfeed
- .AddPreference "title"
- .Preference("title").Type = "Text"
- .Preference("title").DefaultValue = "Title"
- .Preference("title").Caption = "Title"
- End With
Reply #5 Wednesday, May 23, 2007 8:27 AM
Reply #6 Wednesday, May 23, 2007 1:19 PM
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 Wednesday, May 23, 2007 1:34 PM
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 Wednesday, May 23, 2007 1:52 PM
Since I'm lazy I'll just use the forms example difference using five settings.
Normal Example:
- Set myform = DesktopX.CreateForm
- myform.AddPreference "title"
- myform.Preference("title").Type = "Text"
- myform.Preference("title").DefaultValue = "Title"
- myform.Preference("title").Caption = "Title"
- myform.AddPreference "title"
- myform.Preference("title").Type = "Text"
- myform.Preference("title").DefaultValue = "Title"
- myform.Preference("title").Caption = "Title"
- myform.AddPreference "title"
- myform.Preference("title").Type = "Text"
- myform.Preference("title").DefaultValue = "Title"
- myform.Preference("title").Caption = "Title"
- myform.AddPreference "title"
- myform.Preference("title").Type = "Text"
- myform.Preference("title").DefaultValue = "Title"
- myform.Preference("title").Caption = "Title"
- myform.AddPreference "title"
- myform.Preference("title").Type = "Text"
- myform.Preference("title").DefaultValue = "Title"
- myform.Preference("title").Caption = "Title"
Reply #9 Wednesday, May 23, 2007 1:55 PM
- Set myform = DesktopX.CreateForm
- With myform
- .AddPreference "title"
- .Preference("title").Type = "Text"
- .Preference("title").DefaultValue = "Title"
- .Preference("title").Caption = "Title"
- .AddPreference "title"
- .Preference("title").Type = "Text"
- .Preference("title").DefaultValue = "Title"
- .Preference("title").Caption = "Title"
- .AddPreference "title"
- .Preference("title").Type = "Text"
- .Preference("title").DefaultValue = "Title"
- .Preference("title").Caption = "Title"
- .AddPreference "title"
- .Preference("title").Type = "Text"
- .Preference("title").DefaultValue = "Title"
- .Preference("title").Caption = "Title"
- .AddPreference "title"
- .Preference("title").Type = "Text"
- .Preference("title").DefaultValue = "Title"
- .Preference("title").Caption = "Title"
- End With
Reply #11 Wednesday, May 23, 2007 2:30 PM
For Example:
This code uses the with statement for the external object.
- '--position/aligned based on the main object
- Function posObject
- With DesktopX.Object("ExternalObject")
- .Left= Object.Right + 12 'Positions External Object to the left of main object
- .Top = Object.Top 'Aligns External Object Top to main object
- .ToolTipText = "This is My Object"
- End With
- End Function
Reply #12 Wednesday, May 23, 2007 3:03 PM
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 Wednesday, May 23, 2007 3:22 PM
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
- Const lLeft="objLeft",lMid="objMid",lRight="objRight",Label="txtLabel"
- Function sizeLabel
- With DesktopX
- .Object(lMid).Left = .Object(lLeft).Right
- .Object(lRight).Left = .Object(lMid).Right
- .Object(Label).Width = .Object(lLeft).Width+.Object(lMid).Width+.Object(lRight).Width
- '--mask adjustments
- Object.Width=.Object(lRight).Right
- Object.Height=.Object(lMid).Height
- End With
- End Function
- 'Called when the script is executed
- Sub Object_OnScriptEnter
- Call sizeLabel
- 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!







Reply #1 Tuesday, May 22, 2007 6:44 PM