Hello!
I've seen some ppl here using With, for instance
With CommonDialog1
.Flags = ...
and so on.
What's the point with this?
Is there a special benefit with using With, and when should you use it?
Thanks in advance,
Pentax
Printable View
Hello!
I've seen some ppl here using With, for instance
With CommonDialog1
.Flags = ...
and so on.
What's the point with this?
Is there a special benefit with using With, and when should you use it?
Thanks in advance,
Pentax
You use with so that you don't have to type
text1.bla1=..
text1.bla2=..
text1.bla3=..
all the time, instead you type
with text1
.bla1=...
.bla2=...
.bla3=...
end with
You can do this for objects with properties, and UDT's
Saves repeditive typing:
ie
Data1.Recordset.movelast
data1.recordset.addnew
data1.recordset!myRec = text1
data1.recordset.update
or
With data1.recordset
.movelast
.addnew
etc.
Late for dinner..I have to improve my typing speed.
And using With ... is a bit quicker then accessing the same object over and over again.
You can also nest WITH statements when you are changing properties on a single object.
"WITH" can help make your code look a little cleaner if you are making a lot of changes on one object.
Instead of :
Code:Private Sub Command1_Click()
Text1.Height = 100
Text1.Text = "Sample"
Text1.Font.Bold = True
End Sub
And, as mentioned earlier, there is a slight performance gain in using WITH since the name of the object does not need to be requalified for each statement.Code:Private Sub Command1_Click()
With Text1
.Height = 100
.Text = "Sample"
With .Font
.Bold = True
End With
End With
End Sub