Comparison performance - String.compare vs Select Case

G

Guest

Can anybody tell me what is the quickest way )most efficient) method of
performing comparisons in vb.net?

I'm making modifications to an asp.Net / vb.net application developed by
someone else. The code base is loaded with nested if statements all doing
string comparisons. The research that I've done so far indicates that
string.compareordinal is more efficient than string.compare but I haven't
been able to locate anything regarding the efficiency of select case vs
either of these compare methods. Essentially I'm looking for a best practice
for string comparisons.

You thoughts are appreciated. Thank you!!
 
M

Marina

In this case I would be more worried about the readability of the program
then how it is comparing strings. A case statement is much clearer then
nested if statements - so it wins right there.
 
J

Jay B. Harlow [MVP - Outlook]

RJB,
In addition to Marina's comments.

I too prefer the Select Case over nested Ifs. However, if I need to use an
If, and I am checking a series of related conditions I would use If/ElseIf
instead of nesting the Ifs if at all possible

Dim firstShift As TimeRange
Dim secondShift As TimeRange
Dim thirdShift As TimeRange

Dim clockIn as DateTime

If firstShift.Contains(clockIn) then
' Do something exciting for the first shift
ElseIf secondShift.Contains(clockIn) Then
' Do something exciting for the second shift
ElseIf thirdShift.Contains(clockIn) Then
' Do something exciting for the third shift
Else
' should "never" happen :)
Throw New ArgumentException("clockIn", clockIn, "Clock is not in any
shifts")
End If

Search this newsgroup for the implementation of TimeRange & why one might
use it.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top