Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Visual Basic 6 -  "Select Case" VS "Elseif"

Posted on 2006-05-18
12
Medium Priority
?
6,457 Views
Last Modified: 2013-11-25
Which is better in terms of performance & memory usage, which is
higher performance and less memory usage?

"Select Case" VS "Elseif"

The language I am asking about is VB6.
0
Comment
Question by:rhinez0rz
  • 5
  • 3
9 Comments
 
LVL 8

Accepted Solution

by:
List244 earned 1500 total points
ID: 16714081
Well, memory is not really an issue here.  If you use an if or a select, you have all the memory already
spoken for.  As for performance, it seems they are the same.  It seems a select works just as an if, only
it looks little better when you start getting larger.

http://msdn2.microsoft.com/en-us/library/2h66e7a8.aspx

Seems to state that EACH case will be checked, like an if statement.  That is opposed to a language like C++
where you get greater performance using a switch (Select case) because it creates a jump table and is able
to skip much of the checking an if/else would not be able to.

So, I would suggest using whichever works best for you.
0
 
LVL 8

Expert Comment

by:List244
ID: 16714128
One thing to note however, is the following:

A switch statement only needs to evaluate once, while if you have multiple if statments
asking the same thing, they have to evaluate more than once. For example:

Select Case 1 + 3
    Case 1:
        MsgBox "1"
    Case 2:
        MsgBox "2"
    Case 3:
        MsgBox "3"
    Case 4:
        MsgBox "4"
    Case Default:
        MsgBox "Default"
End Select
If 1 + 3 = 1 Then
    MsgBox "1"
ElseIf 1 + 3 = 2 Then
    MsgBox "2"
ElseIf 1 + 3 = 3 Then
    MsgBox "3"
ElseIf 1 + 3 = 4 Then
    MsgBox "4"
End If

The select case only had to evaluate 1+3 once while the if had to do it 4 times.
0
 
LVL 19

Expert Comment

by:Shauli
ID: 16714146
Select Case will be preferred when ther are more than one action possible per condition, whereas ElseIf is per one action at a time. Example:

Select Case SomeCondition
    Case 2, 4, 6 To 9, 12
        'do your stuff
    Case 1, 3, 5
        'do the other stuff
    Case Else
        'take vacation
End Select

This you cannot do with ElseIf, so the answer is more driven by the number of expecetd results and less by memory usage and performace.

S
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Expert Comment

by:List244
ID: 16714185
Shauli, that is not correct, like I stated earlier, it is really about which is cleanest.  

If temp = 1 Or temp = 4 Or (temp >= 6 & temp <= 9) Or temp = 12 Then
    MsgBox temp
ElseIf temp = 1 Or temp = 3 Or temp = 5 Then
    MsgBox temp
Else
    MsgBox temp
End If

That is an equivalent if-else to your case statement.  It isn't quite as clean, but it works.  And like
I stated earlier, it has to check the value of temp much more than would be needed with a select
statement.
0
 
LVL 19

Expert Comment

by:Shauli
ID: 16714277
That is correct. Of course your sample works, but it is not as efficient. As MSDN put it:

"...Tip   Select Case may be more useful when evaluating a single expression that has several possible actions...."

S
0
 
LVL 8

Expert Comment

by:List244
ID: 16714282
Shauli, I have already pointed out that using the select case is more efficient in the way that it evaluates less.  I was
simply saying that your example which claimed the select case could not be done with an if-else, could.
0
 
LVL 19

Expert Comment

by:Shauli
ID: 16714305
You can always travel from Philadelphia to New York through Timbuktu. Can it be done? Sure.

S
0
 

Author Comment

by:rhinez0rz
ID: 16718985
I meant to give credit for this answer:

Select Case will be preferred when ther are more than one action possible per condition, whereas ElseIf is per one action at a time. Example:

Select Case SomeCondition
    Case 2, 4, 6 To 9, 12
        'do your stuff
    Case 1, 3, 5
        'do the other stuff
    Case Else
        'take vacation
End Select

This you cannot do with ElseIf, so the answer is more driven by the number of expecetd results and less by memory usage and performace.

S


--------

Could you fix this? Thanks
0
 
LVL 8

Expert Comment

by:List244
ID: 16719006
Rhinez0rz, that answer was incorrect.  That can be done with an if-else as I have showed you.
Also, if you read my previous comments, you will see that I already said that select would be
more efficient with multiple cases.  Only, I actually told you in addition to that, that it is because
of the fact that it must evaluate more times.  What was wrong with my answer?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question