vbscript Select Case for Range of Values
December 23rd, 2008 | Published in Web Development | 6 Comments
Here is a snippet of vbscript I use in ASP and/or VBS applications where I need a select case statement for a range of values for each case. There isn’t too much documentation online about this, and it’s a bit of a hack, but this will do the trick.
1 2 3 4 5 6 7 8 | select case TRUE case your_var >= 0 and your_var < = 4 'do your stuff case your_var >= 5 and your_var < = 10 'do your stuff case else 'handle the else end select |
Basically, it tells the vbscript engine to select the case that evaluates to TRUE (or you could set to false, or any other value, such as “select case 199” where it would choose the case whose statement evaluated to 199, depending on your requirements)
I know this is a bit of a hack but hey, classic asp is not as robust as some other languages, and so we have to inprovise. one other note, my example has values hardcoded into the select case statements. This is poor programming practice, and was only used for simplicity’s sake in demonstrating how the vbscript select case for a range of values statement works.
Comments welcome.
January 14th, 2009 at 12:17 pm (#)
Like it!
February 26th, 2009 at 11:54 am (#)
Thanks for the information. You’ve saved me tons of time.
July 6th, 2009 at 4:05 pm (#)
Very nice!!!
February 23rd, 2010 at 8:44 pm (#)
Sweet! Been trying to get this to work. Had the “>= AND >=” condition worked out but couldn’t figure out why it wasn’t working. Then I found your post and see that I needed “select case TRUE”. D’OH! I had “select case avgSkill”.
It works now though! Thanks!
September 19th, 2011 at 5:11 pm (#)
MOST helpful–many thanks!
May 2nd, 2012 at 6:35 am (#)
Thank you very much! Great tips!