ASP 101 - Active Server Pages 101 - Web01
The Place ASP Developers Go!

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

Script Output:

This script has determined that you are an ASP developer with a intermediate level of experience.

ASP HTML Download            FAQ

ASP.NET Version

Often when writing code, it's helpful to be able to perform different actions based on the value of a variable. Normally this is done with using an "If...Then...Else...End If" statement or a "Select Case" statement. Consider however the following situation...

You've got a form that takes years of experience doing something as a parameter. Based on the given experience you need to determine which group to put the user in.

Now since you've got a nice limited number of groups that everyone should fit into one of, a Select Case would make for the nicest looking code, but how do you do a Select Case with a range of numbers? You certainly don't want to type these type of conditionals:

Select Case intExperience
	Case 0, 1, 2, 3, 4, 5,... 10, 11, 12
		' Do Something
	Case 13, 14, 15, 16, 17,... 25
		' Do Something
	Case 26, 27, 28, 29, 30,... 35
		' Do Something
End Select

So we resort back to "If...Then...Else...End If" method:

If 0 <= intExperience And intExperience <= 12 Then
	' Do Something
	If 13 <= intExperience And intExperience <= 25 Then
		' Do Something	
		If 26 <= intExperience And intExperience <= 35 Then
			' Do Something
		End If
	End If
End If

That's not much better now is it? What if we miss an End If somewhere? It get's hard to read pretty darn fast even if you're meticulous in your whitespace usage.

Now take a look at the source to this sample... it's quite a bit neater if I do say so myself.

So in reality, this entire sample was written in an effort to teach you about a feature of VBScript that does nothing except make code pretty! Those of you who know me know that this is not out of the ordinary... I'll go to almost any length for neat code. Is this going a little bit overboard? Probably... but hey you're the one who actually read it all! ;)


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers