[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

Listbox code optimaization

Saucer Man

7/17/2011 3:22:00 PM

I have a checkbox style listbox. I add items like so...

lstBox1.AddItem "unique description 1"
lstBox1.AddItem "unique description 2"
.
.
.

I then check a boolean associated with each item in the listbox to see if it
should be selected...

If blnWhatever = False Then
lstBox1.Selected(i) = False
Else
lstBox1.Selected(i) = True
End If
i = i + 1

If blnWhatever = False Then
lstBox2.Selected(i) = False
Else
lstBox2.Selected(i) = True
End If
i = i + 1
.
.
.

I also check each item later to assign the boolean...

If lstBox1.Selected(i) = True Then
blnWhatever= True
Else
blnWhatever = False
End If
i = i + 1

If lstBox2.Selected(i) = True Then
blnWhatever= True
Else
blnWhatever = False
End If
i = i + 1
.
.
.

Currently, I repeat this code 42 times. Frequently, the items need to
change so I add one or remove one. Is there a better way of checking the
boolean and setting the boolean in a loop so I don't have to adjust this
code every time there is a change?

--
Thanks.



--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
17 Answers

mm

7/17/2011 4:33:00 PM

0

El 17/07/2011 12:21 p.m., Saucer Man escribió:

> Currently, I repeat this code 42 times. Frequently, the items need to
> change so I add one or remove one. Is there a better way of checking the
> boolean and setting the boolean in a loop so I don't have to adjust this
> code every time there is a change?

Use a vector (or a collection) for the boolean variables.


Saucer Man

7/17/2011 6:22:00 PM

0

"Eduardo" <mm@mm.com> wrote in message
news:ivv2s9$nmp$1@speranza.aioe.org...
> El 17/07/2011 12:21 p.m., Saucer Man escribió:

> Use a vector (or a collection) for the boolean variables.
>

Can you provide an example?



--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---

MikeD

7/17/2011 8:49:00 PM

0



"Saucer Man" <saucerman@nospam.net> wrote in message
news:ivv97v$2h01$1@adenine.netfront.net...
> "Eduardo" <mm@mm.com> wrote in message
> news:ivv2s9$nmp$1@speranza.aioe.org...
>> El 17/07/2011 12:21 p.m., Saucer Man escribió:
>
>> Use a vector (or a collection) for the boolean variables.
>>
>
> Can you provide an example?

I'm not really sure what Eduardo is getting at with that suggestion. But,
from my own personal perspective of the issue, I don't think you've provided
enough information. Perhaps if you provide a more "complete" code example
rather than just a couple of snippets, I (and perhaps others) can get a
better understanding of your issue.

But, the bottom line is that to determine if an individual item in a listbox
is selected (regardless of whether you're using the checkbox style or just
multi-select), you need to loop through all the items and evaluate the
Selected property for each one. Now if you need to do this for several
different listbox controls (as it appears you are), you might want to write
a sub to which you pass a reference to a listbox. Maybe that's what you were
really getting at.

More info in another reply.

--
Mike


Thorsten Albers

7/17/2011 9:00:00 PM

0

Saucer Man <saucerman@nospam.net> schrieb im Beitrag
<ivuuml$28dk$1@adenine.netfront.net>...
> Currently, I repeat this code 42 times. Frequently, the items need to
> change so I add one or remove one. Is there a better way of checking the

> boolean and setting the boolean in a loop so I don't have to adjust this
> code every time there is a change?

Use a 'control array' (here an array of list box controls), if possible.

--
Thorsten Albers

gudea at gmx.de

MikeD

7/17/2011 9:02:00 PM

0

Comments inline.


"Saucer Man" <saucerman@nospam.net> wrote in message
news:ivuuml$28dk$1@adenine.netfront.net...
> I have a checkbox style listbox. I add items like so...
>
> lstBox1.AddItem "unique description 1"
> lstBox1.AddItem "unique description 2"
> .
> .
> .
>
> I then check a boolean associated with each item in the listbox to see if
> it should be selected...
>
> If blnWhatever = False Then
> lstBox1.Selected(i) = False
> Else
> lstBox1.Selected(i) = True
> End If

The above is the FIRST thing I'd re-write. Completely unnecessary to have an
If statement block. Just do this:

lstBox1.Selected(i) = blnWhatever


> i = i + 1
>
> If blnWhatever = False Then
> lstBox2.Selected(i) = False
> Else
> lstBox2.Selected(i) = True
> End If
> i = i + 1
> .
> .
> .
>
> I also check each item later to assign the boolean...
>
> If lstBox1.Selected(i) = True Then
> blnWhatever= True
> Else
> blnWhatever = False
> End If

Again, same optimization as above. You don't need an If statement block.
Just do

blnWhatever = lstBox1.Selected(i)


> i = i + 1
>
> If lstBox2.Selected(i) = True Then
> blnWhatever= True
> Else
> blnWhatever = False
> End If
> i = i + 1
> .
> .
> .
>
> Currently, I repeat this code 42 times. Frequently, the items need to
> change so I add one or remove one. Is there a better way of checking the
> boolean and setting the boolean in a loop so I don't have to adjust this
> code every time there is a change?

Now here's what I don't understand. Are you saying that you have 42 listbox
controls for which you're duplicating this code? This is the part I think
you need to explain better.

--
Mike



Stan Weiss

7/17/2011 11:53:00 PM

0

I know you did not ask about this but

With lstBox1
.AddItem "unique description 1"
.AddItem "unique description 2"
End With

Stan


Saucer Man wrote:
>
> I have a checkbox style listbox. I add items like so...
>
> lstBox1.AddItem "unique description 1"
> lstBox1.AddItem "unique description 2"
> .
> .
> .
>
> I then check a boolean associated with each item in the listbox to see if it
> should be selected...
>
> If blnWhatever = False Then
> lstBox1.Selected(i) = False
> Else
> lstBox1.Selected(i) = True
> End If
> i = i + 1
>
> If blnWhatever = False Then
> lstBox2.Selected(i) = False
> Else
> lstBox2.Selected(i) = True
> End If
> i = i + 1
> .
> .
> .
>
> I also check each item later to assign the boolean...
>
> If lstBox1.Selected(i) = True Then
> blnWhatever= True
> Else
> blnWhatever = False
> End If
> i = i + 1
>
> If lstBox2.Selected(i) = True Then
> blnWhatever= True
> Else
> blnWhatever = False
> End If
> i = i + 1
> .
> .
> .
>
> Currently, I repeat this code 42 times. Frequently, the items need to
> change so I add one or remove one. Is there a better way of checking the
> boolean and setting the boolean in a loop so I don't have to adjust this
> code every time there is a change?
>
> --
> Thanks.
>
> --- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---

Saucer Man

7/18/2011 1:05:00 AM

0

"MikeD" <nobody@nowhere.edu> wrote in message
news:ivviji$r9c$1@dont-email.me...

> Now here's what I don't understand. Are you saying that you have 42
> listbox controls for which you're duplicating this code? This is the part
> I think you need to explain better.
>
> --
> Mike
>

Sorry I wasn't clear. I have 1 listbox which currently has 42 items in it.
I have 42 boolean variables. I know how to loop through and check the
selected property. I just don't know how to create a loop that will set
each variable. For the following example I am only showing 3 items...

Public blnHidePlanes As Boolean
Public blnHideTrains As Boolean
Public blnHideAutos As Boolean

With lstBox1
.AddItem "Hide Airplanes"
.AddItem "Hide Locomotives"
.AddItem "Hide Cars"
End With

Now if I take your suggestion, I will have 42 lines of code to set the
variables but I will only show 3 in this example...

For i = 0 to lstBox1.listcount -1
lstBox1.Selected(i) = blnHidePlanes
lstBox1.Selected(i) = blnHideTrains
lstBox1.Selected(i) = blnHideAutos
Next i

My question is...can I set the variables without 42 lines of code?



--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---

JensB

7/18/2011 1:15:00 AM

0

Saucer Man presented the following explanation :

> Sorry I wasn't clear. I have 1 listbox which currently has 42 items in it. I
> have 42 boolean variables. I know how to loop through and check the selected
> property. I just don't know how to create a loop that will set each
> variable. For the following example I am only showing 3 items...
>
> Public blnHidePlanes As Boolean
> Public blnHideTrains As Boolean
> Public blnHideAutos As Boolean
>
> With lstBox1
> .AddItem "Hide Airplanes"
> .AddItem "Hide Locomotives"
> .AddItem "Hide Cars"
> End With
>
> Now if I take your suggestion, I will have 42 lines of code to set the
> variables but I will only show 3 in this example...
>
> For i = 0 to lstBox1.listcount -1
> lstBox1.Selected(i) = blnHidePlanes
> lstBox1.Selected(i) = blnHideTrains
> lstBox1.Selected(i) = blnHideAutos
> Next i
>
> My question is...can I set the variables without 42 lines of code?

Only if you chyange your approach. Try something along the lines of...

Private Enum enmHiddenItems
[_ehiFirst] = 0
ehiAirplane = 0
ehiLocomotive
ehiCar
[_ehiFirst]
End Enum

Dim blnHide([_ehiFirst] to [_ehilast] - 1) AS Boolean

Dim i as Integer

For i = [_ehiFirst] to [_ehiLast] - 1
lstBox1.Selected(i) = blnHide(i)
Next i

--
Michael Cole


MikeD

7/18/2011 2:01:00 AM

0



"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


MikeD

7/18/2011 2:57:00 AM

0



"Saucer Man" <saucerman@nospam.net> wrote in message
news:j000sf$kl$1@adenine.netfront.net...
> "MikeD" <nobody@nowhere.edu> wrote in message
> news:ivviji$r9c$1@dont-email.me...
>
>> Now here's what I don't understand. Are you saying that you have 42
>> listbox controls for which you're duplicating this code? This is the part
>> I think you need to explain better.
>>
>> --
>> Mike
>>
>
> Sorry I wasn't clear. I have 1 listbox which currently has 42 items in
> it. I have 42 boolean variables. I know how to loop through and check the
> selected property. I just don't know how to create a loop that will set
> each variable. For the following example I am only showing 3 items...
>
> Public blnHidePlanes As Boolean
> Public blnHideTrains As Boolean
> Public blnHideAutos As Boolean
>
> With lstBox1
> .AddItem "Hide Airplanes"
> .AddItem "Hide Locomotives"
> .AddItem "Hide Cars"
> End With
>
> Now if I take your suggestion, I will have 42 lines of code to set the
> variables but I will only show 3 in this example...
>
> For i = 0 to lstBox1.listcount -1
> lstBox1.Selected(i) = blnHidePlanes
> lstBox1.Selected(i) = blnHideTrains
> lstBox1.Selected(i) = blnHideAutos
> Next i
>
> My question is...can I set the variables without 42 lines of code?


Now I'm questioning if you should even be using a ListBox for this. <g>

So let me see if I have a correct understanding. You're using 42 items in a
listbox to determine if 42 (presumably) *types* of something should appear
in some other list (or lists)? Is that right? I don't even think the code
you posted above will do that properly. The last boolean variable you assign
to the Selected property is the only one that will matter. The previous 41
assignments to the Selected property are completely irrelevant. If I'm
understanding correctly, you'd need to do something like this:

For i = 0 to lstBox1.ListCount - 1
Select Case i
Case 0 'for planes
lstBox1.Selected(i) = blnHidePlanes
Case 1 'for trains
lstBox1.Selected(i) = blnHideTrains
Case 2 ' for autos
lstBox1.Selected(i) = blnHideAutos
End Select
Next

So what you'd have is 42 Case statements, one for each item in your listbox.
Of course, that's really no better (might even be worse) than just doing
this:

lstBox1.Selected(0) = blnHidePlanes
lstBox1.Selected(1) = blnHideTrains
lstBox1.Selected(2) = blnHideAutos

Now, assuming I've got this understood correctly, MY suggestion would be to
use an array for your 42 boolean values. Each element in the array
corresponds to an item in the listbox (they'd have the same index). THEN,
you could use a simple loop like this:

For i = 0 to lstBox1.ListCount - 1
lstbox1.Selected(i) = blnHide(i)
Next

What I'm really confused about now is WHY do you need the boolean variables?
It just seems like you're trying to keep 2 things in sync (the listbox items
with either 42 boolean variables or an array with 42 elements) that you
really don't need to do. Why can't you just directly check if the item in
the listbox is selected or not?


--
Mike