MikeD
7/18/2011 2:01:00 AM
"Stan Weiss" <srweiss@erols.com> wrote in message
news:4E2375E7.642EDC11@erols.com...
> I know you did not ask about this but
>
> With lstBox1
> .AddItem "unique description 1"
> .AddItem "unique description 2"
> End With
>
If you want to do that purely for your own personal coding style, that's
fine. But it doesn't actually optimize a thing. All that's doing is saving
you a little bit of typing and depending on your point of view, maybe making
your code look a little "neater". I know people that think a With block
makes code look uglier and less readable. Others think it's one of the
single best ways to make your code more readable. And other's don't really
care one way or the other. I personally think that AT TIMES, it can make
code more readable. But I sure wouldn't use it for adding 2 items to a
listbox. <g> But I do use it in cases like the following:
With List1
Do Until oRS.EOF
.AddItem oRS.Fields("Description").Value
.ItemData(.NewIndex) = oRS.Fields("ID").Value
Loop
End With
And of course that begs the question of would it be better to use the With
block on the listbox control or on the RecordSet object?
Where a With block CAN actually optimize things marginally is when you can
use it to reduce dots because that means fewer object references to resolve.
For example, just for the sake of argument, let's say this is a ListView
(VB6 version) and you're dealing with a ListSubItem object. Doing this
With ListView1.ListItems(1).ListSubItems(1)
.Text = "some text"
.Bold = True
.ForeColor = vbRed
End With
would be *slightly* more optimized over this
ListView1.ListItems(1).ListSubItems(1).Text = "some text"
ListView1.ListItems(1).ListSubItems(1).Bold = True
ListView1.ListItems(1).ListSubItems(1).ForeColor = vbRed
This is because with the With block, the reference to the ListSubItem object
only needs resolved once rather than 3 times.
I used to be a bigger proponent of using a With block than I am now. When it
was first introduced with VB4, I think using With made a bigger difference
in code optimization than it does with VB6 (or even VB5). Now, I really
think it's just more a matter of your own personal coding style; IOW, simply
whether you like using it for whatever reasons.
Now for that matter, THIS is actually even more optimized than using With:
Dim subitmX As ListSubItem
Set subitmX = ListView1.ListItems(1).ListSubItems(1)
subitmX .Text = "some text"
subitmX .Bold = True
subitmX .ForeColor = vbRed
This is because there is ever-so-slightly less overhead involved since the
VB runtime does not have to create its own internal variable to store the
With block reference (and really, that's all VB is doing when you use a With
block). Again, very, very marginal and it's unlikely you'd see any
difference in benchmark tests.
--
Mike