In VB(A) one frequently simplifies one's code by using the "With" statement to eliminate redundant object references and/or make code more maintainable.
Instead of --
myObject.Method1 parameters
myObject.Method2 parameters
You might do this --
With myObject
.Method1 parameters
.Method2 parameters
End With
Thank you Michael, been doing that for years, move on son.
Hang on a sec.
What you might not realize is that you can use the "With" statement with any expression that returns an object or type.
Eh?
Ok, a quick, but real example. Say you're adding items to a listview object and each entry has sub items. Here's a typical approach --
Dim Item As ListItem
Set Item = listview1.ListItems.Add(, , "Item")
Item.SubItems(1) = "Detail 1"
Item.SubItems(2) = "Detail 2"
Which can be simplified to (and frequently is) --
Dim Item As ListItem
Set Item = listview1.ListItems.Add(, , "Item")
With Item
.SubItems(1) = "Detail 1"
.SubItems(2) = "Detail 2"
End With
But what about --
With listview1.ListItems.Add(, , "Item")
.SubItems(1) = "Detail 1"
.SubItems(2) = "Detail 2"
End With