If the or Select Case?

G

Geoff Edwards

This is a repeat of a message which I had posted to word97vba - but
then I realised that it is beginners stuff.

Please ignore the other copy.

To test (for example) a series of option buttons I have always used If
... ElseIf thus:

If Opt1 Then
.....
ElseIf Opt2 Then
.......
ElseIf Opt3 Then
.......
Else ' must be Opt4
......
EndIf


A colleague uses:

Select Case True
Case Opt1
.....
Case Opt2
.....
Case Opt3
....
Case Else 'must be Opt4
....
End Select

The Select Case looks a lot neater.

I know that the conventional wisdom appears to be to use Select Case
to test an object or variable for various values, and If... to test
various objects for a value (or values) but is there any reason not to
use (eg) Select Case True?

It is even neater when you use it like this to check whether a few
variables are empty:

Select Case ""
Case var1, var2, var3
....
Case Else
....
End Select

Which is so much neater than:

If var1="" Or var2="" or var3="" then

Is there anything wrong with using this technique?
Geoff Edwards
Leeds, UK

e-mail: (e-mail address removed)
(replace the stopspambot bit with some
version of my name)
 
D

Dave Lett

Hi Geoff,

I can't think of anything wrong with using the technique, and I can think of
a one good reason for using the technique in the situations that you're
outlining. That is, as you put it, "it's neater". I think it's more readable
code; therefore, I've used it in both of the scenarios that you've
described.

Dave
 
G

Geoff Edwards

Many thanks

Geoff


Hi Geoff,

I can't think of anything wrong with using the technique, and I can think of
a one good reason for using the technique in the situations that you're
outlining. That is, as you put it, "it's neater". I think it's more readable
code; therefore, I've used it in both of the scenarios that you've
described.

Dave

Geoff Edwards
Leeds, UK

e-mail: (e-mail address removed)
(replace the stopspambot bit with some
version of my name)
 
P

Peter Hewett

Hi Geoff Edwards

The select case is limited to using one expression, like:

Select Case Colour
Case "Red"
' Do something red
Case "Green"
' Do something green
Case "Blue"
' Do something blue
Case Else
' Just in case there's an unhandled expression
End Select

All of the above Case statements are evaluated against "Colour". However, If
statements aren't constrained in this manner:

If Colour = "Red" Then
' Do something red
ElseIf Sound = "Loud" Then
' Do something quiet
ElseIf Taste <> Hot Then
' Do something hot
EndIf

HTH + Cheers - Peter
 
J

Jerry Bodoff

Hi,

I personally use "Select Case" whereever I can. The
construct should execute faster than "If" constructs and,
as mentioned, it is easier to see what is going on as the
code tends to "look cleaner". I have seen code with "If"
go on for pages and it is extremely difficult to "debug"
such a program. Also, it is easier to add code to a
condition using "Case" statements than adding the code
under an "If" construct. If "If" is appropos I also tend
not to use "ElseIf" and prefer to put each "If" on a
separate line for "readability" and ease in adding code
to a condition as the code is enclosed in its own "If-
EndIf".

So much for my 2 cents on the subject.

Jerry B.
 

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