Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Select Case Statement


Reply
 
Thread Tools Display Modes
  #1  
Old 06-02-2004, 02:50 PM
dazza33 dazza33 is offline
Centurion
 
Join Date: Jul 2001
Location: Victoria, Australia
Posts: 199
Default Select Case Statement


I want to use the select case method so as it triggers between to different values i.e. Case is greater than 6000 but less than say 7000.

Code:
select case variable
         case > 6000 <7000 etc,etc.
What is the correct coding for this.

Dazza.
Reply With Quote
  #2  
Old 06-02-2004, 02:52 PM
rick_deacha's Avatar
rick_deacha rick_deacha is offline
Sith Lord

Retired Leader
* Expert *
 
Join Date: Feb 2004
Location: Monterrey, Mexico
Posts: 2,179
Default

Code:
Select Case variable Case 6000 to 6999 '. '. '. End Select

EDIT: Change to 7000 --> to 6999
__________________
Rick
Use [vb][/vb] | Refer to VBA or VB | Newbie? run the Macro Recorder
Excel and VB Automation :|: Excel FAQ
Reply With Quote
  #3  
Old 06-02-2004, 02:52 PM
thingimijig thingimijig is offline
Senior Contributor

* Expert *
 
Join Date: Jul 2003
Posts: 1,300
Default

try this

Case 6001 To 6999

thingimijig.
Reply With Quote
  #4  
Old 06-02-2004, 02:53 PM
dazza33 dazza33 is offline
Centurion
 
Join Date: Jul 2001
Location: Victoria, Australia
Posts: 199
Default

Quote:
Originally Posted by thingimijig
try this

Case 6001 To 6999

thingimijig.
Thank You....

Regards Dazza.
Reply With Quote
  #5  
Old 06-02-2004, 03:10 PM
KAL_0801's Avatar
KAL_0801 KAL_0801 is offline
Centurion
 
Join Date: Apr 2004
Location: New York City
Posts: 174
Default

What do you do if it is between 2 values?
(ie: looking for letters A-Z and a-z(not specific))
__________________

"If a little knowledge is dangerous, where is a man who has so much as to be out of danger?"
-Thomas Huxley
Reply With Quote
  #6  
Old 06-02-2004, 03:17 PM
thingimijig thingimijig is offline
Senior Contributor

* Expert *
 
Join Date: Jul 2003
Posts: 1,300
Default

it is case sensitive, so you can do this
Code:
Select Case Text1.Text Case "b" To "d" MsgBox "letter from b to d" End Select
or
Code:
Select Case Text1.Text Case "B" To "D" MsgBox "letter from B to D" End Select
or both.
Code:
Select Case Text1.Text Case "B" To "D", "b" To "d" MsgBox "either" End Select

thingimijig.
Reply With Quote
  #7  
Old 06-02-2004, 03:23 PM
rick_deacha's Avatar
rick_deacha rick_deacha is offline
Sith Lord

Retired Leader
* Expert *
 
Join Date: Feb 2004
Location: Monterrey, Mexico
Posts: 2,179
Default

Or this
Code:
Select Case UCase(Text1.Text) Case "B" To "D" MsgBox "founded" End Select
__________________
Rick
Use [vb][/vb] | Refer to VBA or VB | Newbie? run the Macro Recorder
Excel and VB Automation :|: Excel FAQ
Reply With Quote
  #8  
Old 06-02-2004, 03:29 PM
KAL_0801's Avatar
KAL_0801 KAL_0801 is offline
Centurion
 
Join Date: Apr 2004
Location: New York City
Posts: 174
Default

Looking for the "," thing over there between the two statements, not the code (it was just an example ). Thanks.
__________________

"If a little knowledge is dangerous, where is a man who has so much as to be out of danger?"
-Thomas Huxley
Reply With Quote
  #9  
Old 06-02-2004, 03:30 PM
MikeJ's Avatar
MikeJ MikeJ is offline
Retread

Retired Moderator
* Expert *
 
Join Date: Sep 2002
Location: Austin, Texas
Posts: 6,742
Default

Just to specify, thingimijig's code is best if you want it to be CaSe-SeNsAtIvE, while rick's is better for if you want it to be case-insensative.
__________________
{ Lex Fori } { Locus Classicus } { Rutilus Scrinium }
Osculare pultem meam!
Reply With Quote
  #10  
Old 06-02-2004, 03:32 PM
KAL_0801's Avatar
KAL_0801 KAL_0801 is offline
Centurion
 
Join Date: Apr 2004
Location: New York City
Posts: 174
Default

What happens when you do use ">" and "<" in a case statement. VB doesn't reject it, yet it doesn't work. Why?
__________________

"If a little knowledge is dangerous, where is a man who has so much as to be out of danger?"
-Thomas Huxley
Reply With Quote
  #11  
Old 06-02-2004, 03:54 PM
thingimijig thingimijig is offline
Senior Contributor

* Expert *
 
Join Date: Jul 2003
Posts: 1,300
Default

they can be used like so.
Code:
Select Case Val(Text1.Text) Case Is < 200, Is > 400 MsgBox "value is either lower than 200 or greater than 400" End Select
you can also get the same results with.
Code:
Select Case Val(Text1.Text) Case 200 To 400 Case Else MsgBox "value is either lower than 200 or greater than 400" End Select

thingimijig.
Reply With Quote
  #12  
Old 06-02-2004, 03:56 PM
KAL_0801's Avatar
KAL_0801 KAL_0801 is offline
Centurion
 
Join Date: Apr 2004
Location: New York City
Posts: 174
Default

Yet the compiler accepts it like this, but it doesn't act like above(with comma):
Code:
Case Is > 100 < 150
__________________

"If a little knowledge is dangerous, where is a man who has so much as to be out of danger?"
-Thomas Huxley
Reply With Quote
  #13  
Old 06-02-2004, 05:24 PM
Rockoon's Avatar
Rockoon Rockoon is offline
Joseph Koss

* Guru *
 
Join Date: Aug 2003
Location: Unfashionable End
Posts: 3,615
Default

This last one looks to me like it may not be performing as expected (ie, like the others)

.... Case Is > 100 < 150

I believe this will probably be evaluated like this:

.... Case Is > (100 < 150)

Which is the same as:

... Case Is > -1


Someone else can check this.. because I just dont see the value in this sort of syntax anyways.
Reply With Quote
  #14  
Old 06-02-2004, 05:31 PM
KAL_0801's Avatar
KAL_0801 KAL_0801 is offline
Centurion
 
Join Date: Apr 2004
Location: New York City
Posts: 174
Default

Makes sense. I tried these two statements to see if it as you said:
Code:
'Will work if Case > -1 Case Is > 10 < 100 'Will fire if Case < -1 Case Is < 10 > 100
__________________

"If a little knowledge is dangerous, where is a man who has so much as to be out of danger?"
-Thomas Huxley
Reply With Quote
  #15  
Old 06-02-2004, 09:42 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,722
Default

Actually (10 > 100) is false (0) so I would expect the second one to fire if the value
was less than 0.

Case Is < 10 < 100 (10 < 100) is True (-1)
so would fire for less than -1
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #16  
Old 06-02-2004, 10:12 PM
KAL_0801's Avatar
KAL_0801 KAL_0801 is offline
Centurion
 
Join Date: Apr 2004
Location: New York City
Posts: 174
Default

Your right in the first part(I meant it to be <=).
__________________

"If a little knowledge is dangerous, where is a man who has so much as to be out of danger?"
-Thomas Huxley
Reply With Quote
  #17  
Old 06-02-2004, 10:19 PM
KAL_0801's Avatar
KAL_0801 KAL_0801 is offline
Centurion
 
Join Date: Apr 2004
Location: New York City
Posts: 174
Default

So now we have this:
Code:
'Will work if Case > -1 Case Is > 10 < 100 'Will work if Case < -1 Case Is < 10 < 100 'Will Work if Case > 0 Case Is > 10 > 100 'Will Work if Case < 0 Case Is < 10 > 100
__________________

"If a little knowledge is dangerous, where is a man who has so much as to be out of danger?"
-Thomas Huxley
Reply With Quote
  #18  
Old 06-03-2004, 05:54 AM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,722
Default

Yes, that compiles and executes, but there is little reason to express cases in that manner, since it would tend to mislead someone as to what it is doing.

Boolean Values (True and False) are represented internally in Visual Basic as a two byte integer.
True = -1
False = 0
In practice, any non-zero value is interpreted by Visual Basic as True, but the results
of a comparison (i.e. x < y, or x = y) will always return -1 (True) or 0 (False) depending
on whether the condition is True or False.
So if I had the code,

Dim i as Integer
i = 10 < 100 '10 is less than 100 so i is set to -1 (True)
Debug.Print i ' Would print -1

So your select block is essentially saying (perhaps a bit clearer).

Code:
'Will work if Case > -1 Case Is > True 'Will work if Case < -1 Case Is < True 'Will Work if Case > 0 Case Is > False 'Will Work if Case < 0 Case Is < False 'Or 'Will work if Case > -1 Case Is > -1 'Will work if Case < -1 Case Is < -1 'Will Work if Case > 0 Case Is > 0 'Will Work if Case < 0 Case Is < 0
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #19  
Old 06-16-2004, 11:49 AM
DvdKhl DvdKhl is offline
Freshman
 
Join Date: Jul 2003
Posts: 32
Default

Code:
Dim QM As String Dim LM As String Dim SRT As String Dim SWT As String Dim WH As String Dim ShtDwn As String 'Load From Registry GetKeyValue HKEY_CURRENT_USER, "Software\WaTimer", "QuietMode", QM GetKeyValue HKEY_CURRENT_USER, "Software\WaTimer", "LoudMode", LM GetKeyValue HKEY_CURRENT_USER, "Software\WaTimer", "Remain", SRT GetKeyValue HKEY_CURRENT_USER, "Software\WaTimer", "WakeTime", SWT GetKeyValue HKEY_CURRENT_USER, "Software\WaTimer", "WindowHeight", WH GetKeyValue HKEY_CURRENT_USER, "Software\WaTimer", "ShtDwn", ShtDwn Select Case Not "" ' [b]<--- There is the problem[/b] Case QM txt_SleepMode.Text = QM Case LM txt_WakeUpMode.Text = LM Case SRT txt_SetRemainTime.Text = SRT Case SWT txt_SetWakeTime.Text = SWT Case WH Me.Height = WH Case ShtDwn pn_ShtDwn.Value = ShtDwn End Select

My Problem is when there is no RegValue(s) the Variable(s) are = "". Now I only want to change the object if the value not "". But
Select Case Not ""
Select Case <> ""
won't work.
Is there any way to do that or do I have to make a workaround?
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->