Code-tip - SELECT CASE and Randomize statement

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 7
February 28, 2002
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Randomize and SELECT CASE code-tip



We recently used the randomize function and a SELECT CASE statement  on some specific webpages on ASPFree.  We discovered the randomize statement begins with 0 when using VB.NET.  Let us explain.

'SELECT CASE starting at 1 and no ELSE statement
'Problem is once in a while nothing displays

<html>
          <body>
<%
Dim r as new Random()
Select r.Next(3)

Case 1
      response.write("
some bold text")
Case 2
      response.write("
some underlined text")
Case 3
      response.write("
some italic text")
End Select
%>

             </body>
</html>

Every so often nothing would be displayed.  After some investigation and understanding if there isn't an ELSE in the SELECT CASE statement why text to wouldn't display?  In the specific function,  an ELSE statement wasn't appropriate.  What we found is to start the CASE statement at zero(0) instead of 1.   The other option is to put an ELSE statement if applies. We wanted to understand why this was happening.

'SELECT CASE starting at 0 and no ELSE statement
'an ELSE statement would solve it also.

<html>
          <body>
<%
Dim r as new Random()
Select r.Next(3)

Case 1
      response.write("
some bold text")
Case 2
      response.write("
some underlined text")
Case 3
      response.write("some italic text")
Else
      response.write("Default text to be displayed")

End Select
%>
          </body>
</html> 

OR

'SELECT CASE starting at 0 and no ELSE statement
'Something will display everytime.


<html>
          <body>
<%
Dim r as new Random()

Select r.Next(3)

Case 0
      response.write("
some bold text")
Case 1
      response.write("
some underlined text")
Case 2
      response.write("some italic text")
End Select
%>  
        </body>
</html> 

This probably isn't rocket science, after an hour or so of going WHAT!#@%@.    This problem was solved, the easiest things can make a developer go crazy when working on a project.  Hopefully this tip saves you the frustration we discovered!

blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials