ASP – Using the Case Select Statement – When x to y doesn’t work!

Using ASP we often use the Case Select statement to choose an action dependant on a value.

e.g.

Select Case intPrice

Case 100

response.write(“Item Costs £100.00”)

Case 200

response.write(“Item Costs £200.00”)

Case 300

response.write(“Item Costs £300.00”)

Case Else

response.write(“Invalid Price”)

End Select

However, what happens if we want to choose an action when a value is between two other values?

If you’ve used VB then you’ll have used something like Case 0 To 100.

But, this doesn’t work in ASP.

There is, however, a workaround to this. In our example above we might want to show a message in one of 4 cases (with a value being stored in a variable called myValue):

  1. When the value is between £0 and £100;
  2. When the value is between £101 and £200;
  3. When the value is between £201 and £300;
  4. When the value is greater than £300;

We can do this using the following:

Select Case True

Case (myValue > 0 AND myValue <101)
response.write(“Item Cost Is Low”)

Case (myValue > 100 AND myValue <201)
response.write(“Item Cost Is Medium”)

Case (myValue > 200 AND myValue <301)
response.write(“Item Cost Is High”)

Case Else
response.write(“Invalid Item Cost”)

End Select

Here the Select Case True makes sure that we will consider all the possible Case statements and then the comparison within each statement dictates which will be TRUE.

I’m not sure if this is legitimate ASP or if it’s just a hack but it works!

Advertisements

1 Response to “ASP – Using the Case Select Statement – When x to y doesn’t work!”


  1. 1 Morten July 21, 2008 at 12:06 pm

    Thanks. It works just perfect.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Blog Stats

  • 221,277 hits

Counter

ClustrMap


%d bloggers like this: