|
-
Apr 11th, 2005, 05:02 AM
#1
Thread Starter
Frenzied Member
select case with non integer numbers
Hello
I have got a select case structure with non integer values, it goes like this
select case power
case 0.001
'statements
case 1
'statements
case 2
'statements
case 20.001
'statements
case 40
'statements
case 40.001
'statements
case 60
'statements
case 60.001
'statements
end select
however my case structure does not work with non integer values like the 0.001, 40.001, 60.001
i tried with declaring power as a double like so
Code:
Dim power As Double
but that did not work either
-
Apr 11th, 2005, 05:05 AM
#2
Re: select case with non integer numbers
I suppose you could create a temporary variable to make the numbers strings and check it that way.
Example:
VB Code:
Select Case CStr(power)
Case "0.001"
' Statements
Case "1"
' Statements
Case "2"
' Statements
Case "20.001"
' Statements
Case "40"
' Statements
Case "40.001"
' Statements
Case "60"
' Statements
Case "60.001"
' Statements
End Select
Cheers,
RyanJ
-
Apr 11th, 2005, 05:06 AM
#3
Re: select case with non integer numbers
Select Case will work with strings.
VB Code:
Dim power as Double
Dim pString as String
pString = power
Select Case pString
'
'
'
This world is not my home. I'm just passing through.
-
Apr 11th, 2005, 05:07 AM
#4
Thread Starter
Frenzied Member
Re: select case with non integer numbers
is there no way to check for non integer values in a select structure then?
-
Apr 11th, 2005, 05:10 AM
#5
Re: select case with non integer numbers
is there no way to check for non integer values in a select structure then?
No, I don't believe there is. However, here's another thought. Why not multiply your values by 1000 for the Select statement?
VB Code:
Select Case power * 1000
Case 1 'power = 0.001
' Statements
Case 1000 'power = 1
' Statements
Case 2000 'power = 2
' Statements
Case 20001 'power = 20.001
This world is not my home. I'm just passing through.
-
Apr 11th, 2005, 05:11 AM
#6
Hyperactive Member
Re: select case with non integer numbers
G'day,
OPen a new form and place a text box on it and try this.
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then Select Case Text1.Text Case 1: MsgBox "1 was entered" Case 0.01 MsgBox "0.01 was entered" Case Else MsgBox "Something else was entered" End Select End If End Sub
-
Apr 11th, 2005, 05:15 AM
#7
Hyperactive Member
Re: select case with non integer numbers
G'day,
Not sure wheat went wrong with previous post.
Open a new form with a textbox called text1 on it and add the following code.
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Select Case Text1.Text
Case 1:
MsgBox "1 was entered"
Case 0.01
MsgBox "0.01 was entered"
Case Else
MsgBox "Something else was entered"
End Select
End If
End Sub
-
Apr 11th, 2005, 05:40 AM
#8
Thread Starter
Frenzied Member
Re: select case with non integer numbers
thanks for all the suggestions
supremus, i think your suggestion works cause it probably takes it in and comapres the text. i like the multiply by 1000. elegant !!
it's a shame that the case structure does not take into account fractional numbers
-
Apr 11th, 2005, 05:48 AM
#9
Re: select case with non integer numbers
Word of warning:
Supremus' method treats "1" and "1.0" as different values. This is not a problem with the x1000 method I suggested earlier in the thread. However, pick whichever solution works best for you.
This world is not my home. I'm just passing through.
-
Apr 11th, 2005, 05:59 AM
#10
Thread Starter
Frenzied Member
Re: select case with non integer numbers
i did the multiply by 1000 method, it has the value 60001 (checked it in the immediate window) but still drops through the case structure
i will try out the string converstion method now
-
Apr 11th, 2005, 06:07 AM
#11
Thread Starter
Frenzied Member
Re: select case with non integer numbers
this is interesting
the following structure works
Code:
powstring = power
Select Case powstring
Case "0.001"
'statements
case 1
'statements
case "60.001"
'statements
end select
but not this code
Code:
powstring = power
Select Case powstring
Case 0.001
'statements
case 1
'statements
case 60.001
'statements
end select
-
Apr 11th, 2005, 06:10 AM
#12
Re: select case with non integer numbers
That's strange that the x1000 method didn't work for you. This test code worked fine for me for all values.
VB Code:
Private Sub Command1_Click()
Dim power As Single
power = Val(InputBox("Enter value for power"))
Select Case power * 1000
Case 1
Debug.Print "0.001"
Case 1000
Debug.Print "1"
Case 2000
Debug.Print "2"
Case 20001
Debug.Print "20.001"
Case 40000
Debug.Print "40"
Case 40001
Debug.Print "40.001"
Case 60000
Debug.Print "60"
Case 60001
Debug.Print "60.001"
End Select
End Sub
This world is not my home. I'm just passing through.
-
Apr 11th, 2005, 06:10 AM
#13
Frenzied Member
Re: select case with non integer numbers
I wasn't aware that Select Case didn't work with doubles...
I tried this, and it works:
VB Code:
Private Sub Command1_Click()
CheckValue 0.001
CheckValue 0.002
CheckValue 0.003
End Sub
Private Sub CheckValue(ByVal x As Double)
Select Case x
Case 0.001
MsgBox "You passed 1E-3"
Case 0.002
MsgBox "You passed 2E-3"
Case Else
MsgBox "You passed " & CStr(x)
End Select
End Sub
Where are your values coming from, can you be sure of their precision?
-
Apr 11th, 2005, 06:12 AM
#14
Re: select case with non integer numbers
 Originally Posted by vb_student
this is interesting
the following structure works
Code:
powstring = power
Select Case powstring
Case "0.001"
'statements
case 1
'statements
case "60.001"
'statements
end select
but not this code
Code:
powstring = power
Select Case powstring
Case 0.001
'statements
case 1
'statements
case 60.001
'statements
end select
Floating point (single and double) are evil.
You just fell right into the reason for it.
DISPLAY a DOUBLE value and you see one thing - compare it in an IF or SELECT/CASE and it all of a sudden doesn't work.
Something that might display at 4.5 is really stored as 4.499999999999999999
That's the way DOUBLE works.
Why can't you use CURRENCY? It doesn't have this problem and the SELECT/CASE will all of a suddent work beautifully.
If you need double, change the CASE's to a RANGE - not an exact value - a bit below and a bit above the value you are trying to CASE on.
-
Apr 12th, 2005, 05:58 AM
#15
Thread Starter
Frenzied Member
Re: select case with non integer numbers
hey in another part of my code, power with 0.001 works OK
is this some sort of idiosynchracy in VB?
i had nto decalred the power in that module too. is it because of that, in the module where it was not working i had dxeclared it as a double.
-
Apr 12th, 2005, 06:01 AM
#16
Re: select case with non integer numbers
You must understand that DOUBLE will give you these inconsistent results.
Do you need to store your values as DOUBLE?
Why doesn't the CURRENCY datatype work for you - it precise and doesn't have the issues that DOUBLE does.
-
Apr 14th, 2005, 04:30 AM
#17
Thread Starter
Frenzied Member
Re: select case with non integer numbers
it somehow works now with double, i will leave it at that and if the big guy wants me to clean up the code, i will do it later
what is the difference between double and currency?
currency sounds something to do with dollars, pounds and yen not fractional numbers.
-
Apr 14th, 2005, 05:45 AM
#18
Re: select case with non integer numbers
Currency is a datatype that allows four digits after the decimal point.
It's actually a long, with an "implied" decimal - so it has the accuracy of long but the fake decimal point works for you...
-
Apr 14th, 2005, 09:20 AM
#19
Thread Starter
Frenzied Member
Re: select case with non integer numbers
thanks for telling me about currencies
i just tried longs elsewhere in my code and they did not work for fractions
there does not seem to be a correspondence between variable types in c and VB
doubles and longs used to accept fractional parts in c but they dont in VB, correct?
-
Apr 14th, 2005, 09:21 AM
#20
Re: select case with non integer numbers
I do not know C - I cannot imagine a LONG ever accepting a decimal fractional part...
CURRENCY is a special "made-up" type that is really a LONG, but with an implied decimal - it's nice and accurate.
-
Apr 14th, 2005, 10:12 AM
#21
Need-a-life Member
Re: select case with non integer numbers
 Originally Posted by trisuglow
No, I don't believe there is. However, here's another thought. Why not multiply your values by 1000 for the Select statement?
VB Code:
Select Case power * 1000
Case 1 'power = 0.001
' Statements
Case 1000 'power = 1
' Statements
Case 2000 'power = 2
' Statements
Case 20001 'power = 20.001
Yes you can. This code works.
VB Code:
Option Explicit
Private Sub Form_Load()
SelectCase 0.01
SelectCase 0.02
SelectCase 1
SelectCase 2
End Sub
Private Sub SelectCase(dblNumber As Double)
Select Case dblNumber
Case 0.01
Debug.Print "0.01"
Case 0.02
Debug.Print "0.02"
Case 1
Debug.Print "1"
Case 2
Debug.Print "2"
End Select
End Sub
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|