473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

vb.net 1.1 -- Better Coding Question

bh
If I want to loop through the values in a listbox, and get either all items
in the box, or only selected items, based on a boolean variable passed into
a subroutine, which method would be more efficient?

First Method (test allvalues, first and either loop through all items or
selecteditems, accordingly):

Dim dview As DataRowView
Dim AllValues As Boolean = False
If AllValues = True Then
For Each dview In lstMyListBox.Items
Apr 9 '07 #1
3 1666
BH,

Don't botter to much about loops, probably they take not even 1 promille
from your program.

Cor

"bh" <No****@ReplyToGroup.comschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
If I want to loop through the values in a listbox, and get either all
items in the box, or only selected items, based on a boolean variable
passed into a subroutine, which method would be more efficient?

First Method (test allvalues, first and either loop through all items or
selecteditems, accordingly):

Dim dview As DataRowView
Dim AllValues As Boolean = False
If AllValues = True Then
For Each dview In lstMyListBox.Items
.
.
.
Next
Else
For Each dview In lstMyListBox.SelectedItems (does this
property do a "behind the scenes" if...then, anyway?)
.
.
.
Next
End If

-OR- Second Method (loop through all items, regardless, then check
selected property, if needed):

Dim dview As DataRowView
Dim AllValues As Boolean = False
Dim iIndex As Integer
For iIndex = 0 To lstMyListBox.Items.Count - 1
If AllValues = True OrElse lstMyListBox.GetSelected(iIndex) =
True Then
.
.
.
End If
Next


Apr 9 '07 #2
Hi BH: Nice to see somebody concerned about "better coding" :-)

A couple of points... first you can avoid constructs like "If AllValues =
True" and simply test the value of the variable. Remember it is a boolean
so it's value is basically the result of the test. So code it as "If
AllValues" and you get the same answer.

Now to the point of better coding. It may be more "efficient" in terms of
time to use the SelectedItems property but that isn't the only measure of
better coding. I'd probably opt for traversing the collection and the
reason is (as you've indicated) you can address them all or the selected
subset. Importantly the same loop could address any other property (not
just selected) so if you had to test for any other condition your code would
be very similar. And similarity is good.

From an efficiency standpoint consider using the For Each syntax in your
second example. So basically your routine will visit every item once, test
it for a condition and do something if that condition is met. That's easy
to understand code, that's hard to break code and so that's "better" code.

Tom
"bh" <No****@ReplyToGroup.comwrote...
If I want to loop through the values in a listbox, and get either all
items in the box, or only selected items, based on a boolean variable
passed into a subroutine, which method would be more efficient?

First Method (test allvalues, first and either loop through all items or
selecteditems, accordingly):

Dim dview As DataRowView
Dim AllValues As Boolean = False
If AllValues = True Then
For Each dview In lstMyListBox.Items
.
.
.
Next
Else
For Each dview In lstMyListBox.SelectedItems (does this
property do a "behind the scenes" if...then, anyway?)
.
.
.
Next
End If

-OR- Second Method (loop through all items, regardless, then check
selected property, if needed):

Dim dview As DataRowView
Dim AllValues As Boolean = False
Dim iIndex As Integer
For iIndex = 0 To lstMyListBox.Items.Count - 1
If AllValues = True OrElse lstMyListBox.GetSelected(iIndex) =
True Then
.
.
.
End If
Next


Apr 9 '07 #3
bh
Thank you! That clears the cobwebs :)

"Tom Leylan" <tl*****@nospam.netwrote in message
news:Ot**************@TK2MSFTNGP05.phx.gbl...
Hi BH: Nice to see somebody concerned about "better coding" :-)

A couple of points... first you can avoid constructs like "If AllValues =
True" and simply test the value of the variable. Remember it is a boolean
so it's value is basically the result of the test. So code it as "If
AllValues" and you get the same answer.

Now to the point of better coding. It may be more "efficient" in terms of
time to use the SelectedItems property but that isn't the only measure of
better coding. I'd probably opt for traversing the collection and the
reason is (as you've indicated) you can address them all or the selected
subset. Importantly the same loop could address any other property (not
just selected) so if you had to test for any other condition your code
would be very similar. And similarity is good.

From an efficiency standpoint consider using the For Each syntax in your
second example. So basically your routine will visit every item once,
test it for a condition and do something if that condition is met. That's
easy to understand code, that's hard to break code and so that's "better"
code.

Tom
"bh" <No****@ReplyToGroup.comwrote...
>If I want to loop through the values in a listbox, and get either all
items in the box, or only selected items, based on a boolean variable
passed into a subroutine, which method would be more efficient?

First Method (test allvalues, first and either loop through all items or
selecteditems, accordingly):

Dim dview As DataRowView
Dim AllValues As Boolean = False
If AllValues = True Then
For Each dview In lstMyListBox.Items
.
.
.
Next
Else
For Each dview In lstMyListBox.SelectedItems (does this
property do a "behind the scenes" if...then, anyway?)
.
.
.
Next
End If

-OR- Second Method (loop through all items, regardless, then check
selected property, if needed):

Dim dview As DataRowView
Dim AllValues As Boolean = False
Dim iIndex As Integer
For iIndex = 0 To lstMyListBox.Items.Count - 1
If AllValues = True OrElse lstMyListBox.GetSelected(iIndex) =
True Then
.
.
.
End If
Next



Apr 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
12
by: Jonathan Bartlett | last post by:
Just finished a new IBM DeveloperWorks article on linked lists, and thought you all might be interested. It's not an introduction -- it instead covers some of the more interesting aspects of...
43
by: Rob R. Ainscough | last post by:
I realize I'm learning web development and there is a STEEP learning curve, but so far I've had to learn: HTML XML JavaScript ASP.NET using VB.NET ..NET Framework ADO.NET SSL
39
by: windandwaves | last post by:
Hi Folk I have to store up to eight boolean bits of information about an item in my database. e.g. with restaurant drive-through facility yellow windows
21
by: Newish | last post by:
Hi Can someone please enlighten me. I hear different things. Some suggest that the difference between the languages in terms of capability and versitility has really narrowed down to a point...
19
by: Mary Pegg | last post by:
There's got to be a better way: if (isset($c)) echo $c."<br>"; if (isset($c)) echo $c."<br>"; if (isset($c)) echo $c."<br>"; if (isset($c)) echo $c."<br>"; if (isset($c)) echo $c."<br>"; but...
1
by: Jennifer Jazz | last post by:
My question is regarding the mapping of Class diagram to the C++ coding. There are 3 realtions in Class diagram 1) Assosication 2) Composition 3) Aggregation (Weak Composition). ...
3
by: MikeY | last post by:
From my understanding when exporting your app.exe with a mediaplay hooked up, you must also place a copy of AxInterop.WMPLib.dll and Interop.WMPLib.dll" wrapper class. to the location of my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.