|
-
Aug 25th, 2001, 05:55 PM
#1
Thread Starter
pathfinder
Is Split Buggy? Why Isn't 0 a Number?
This is weird.
Could anyone Try this code and tell me if 0 is consistently a number or not?
VB Code:
Private Sub Command2_Click()
Dim MY_STR As String
MY_STR = "1 + 0"
MY_STUFF = Split(MY_STR, " ", -1, vbTextCompare)
For Each One In MY_STUFF
Select Case One
Case ""
MsgBox "NULL"
Case "+"
MsgBox "PLUS"
Case IsNumeric(One) = True
MsgBox One & " is a Number"
Case Else
MsgBox One & " is Not a Number"
End Select
Next One
MY_STR = "0"
If IsNumeric(MY_STR) Then
MsgBox MY_STR & " is a number"
Else
MsgBox MY_STR & " is NOT a number"
End If
End Sub
The following is even worse, 1 isn't even a number anymore!
VB Code:
Private Sub Command2_Click()
Dim MY_STR As String
Dim Two As String
MY_STR = "1 + 0"
MY_STUFF = Split(MY_STR, " ", -1, vbTextCompare)
For Each One In MY_STUFF
Two = One
Select Case Two
Case ""
MsgBox "NULL"
Case "+"
MsgBox "PLUS"
Case IsNumeric(Two) = True
MsgBox Two & " is a Number"
Case Else
MsgBox Two & " is Not a Number"
End Select
Next One
End Sub
Am I Missing something? Or should I ReBoot / ReIntsall VB?
-Lou
Last edited by NotLKH; Aug 25th, 2001 at 06:11 PM.
-
Aug 25th, 2001, 06:10 PM
#2
Thread Starter
pathfinder
Filburt1,
Think you could test this too, with your VB5 Coding of the Split function?
-Thanks,
-Lou
-
Aug 25th, 2001, 06:16 PM
#3
dim myStr as string ...
-
Aug 25th, 2001, 06:17 PM
#4
Member
Originally posted by NotLKH
Filburt1,
Think you could test this too, with your VB5 Coding of the Split function?
-Thanks,
-Lou
I gotta get the Split, Replace, InStrRev, etc. functions first. Gimme a moment while I make a module...
-
Aug 25th, 2001, 06:18 PM
#5
Fanatic Member
Your expressions inside the case block are confusing. That has to be what's throwing you off. You're not testing the case of something for the result you are seeing. Like this for example:
VB Code:
Case IsNumeric(One) = True
MsgBox One & " is a Number"
That evaluates based on whether or not One is really numeric. If it is, the whole thing is True. If it's not, it becomes False. The variant in thise case (as I assume it has to be since you never dimmed it), might be treating that as "False" rather than 0, which makes you think 0 isn't a number. Anyway, it's confusing as you wrote. You need to be careful of these things.
VB Code:
Private Sub Command3_Click()
Dim v As Variant, v2 As Variant
v = Split("1 + 0")
For Each v2 In v
Select Case IsNumeric(v2)
Case True
Debug.Print v2; " is numeric"
Case False
Debug.Print v2; " is not numeric"
End Select
Next v2
End Sub
debug window
Code:
1 is numeric
+ is not numeric
0 is numeric
And yes, I have VB5. My version is nearly identical in operation to the VB6 one as I could make it based on info from MSDN.
Last edited by Kaverin; Aug 25th, 2001 at 06:22 PM.
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
-
Aug 25th, 2001, 06:21 PM
#6
Frenzied Member
IsNumeric works but your loop does not. Try this
Code:
Private Sub Command2_Click()
Dim MY_STR As String
MY_STR = "1 + 0"
MY_STUFF = Split(MY_STR, " ", -1, vbTextCompare)
For Each One In MY_STUFF
Select Case One
Case ""
MsgBox "NULL"
Case "+"
MsgBox "PLUS"
Case Else
If IsNumeric(One) Then
MsgBox One & " is a Number"
Else
MsgBox One & " is Not a Number"
End If
End Select
Next One
MY_STR = "0"
If IsNumeric(MY_STR) Then
MsgBox MY_STR & " is a number"
Else
MsgBox MY_STR & " is NOT a number"
End If
End Sub
Greg
Free VB Add-In - The Reference Librarian
Click Here for screen shot and download link.
-
Aug 25th, 2001, 06:22 PM
#7
Member
Originally posted by filburt1
I gotta get the Split, Replace, InStrRev, etc. functions first. Gimme a moment while I make a module...
About to test code using this module:
How should I test your code?
-
Aug 25th, 2001, 06:36 PM
#8
Fanatic Member
Weird......NotLKH's code works for string/variant = "1" but not if string/variant = "0"........Hmmmmmmm.....Veeeerrrry Interesting.
-
Aug 25th, 2001, 06:48 PM
#9
Thread Starter
pathfinder
Thanks for the workaround, gdebacker.
Thats what I did before I even posted.
But, My Confusion stems from, if you use any integer except 0,
My first code returns That it is Numeric. Why does it Treat 0 differently?
And, Kaverin,
I understand Your concern for useing the Case test:
Case IsNumeric(One) = True
The reason I did this was because the same thing still happened
without the "= True" Part.
For Example, tweeking your code to be more similar to Mine:
VB Code:
Private Sub Command2_Click()
Dim v As Variant, v2 As Variant
v = Split("4 + 3 + 2 + 1 + 0")
For Each v2 In v
Select Case v2
Case IsNumeric(v2)
MsgBox v2 & " is numeric"
Case Else
MsgBox v2 & " is not numeric"
End Select
Next v2
End Sub
It still tells me 0 is not numeric, while 1 thru 4 is. Why is 0 Different?
Also, I had tested dimming One as a variant before I posted. Still The same. 1 is a number, 0 isn't. Why?
So, to test if an Element passed by the split function is IsNumeric, it seems that it Has to be its own Case structure, and Not as a case itself in a case structure, Just in case 0 is possible.
Well, Thanks for the replies so far.
-Lou
-
Aug 25th, 2001, 06:49 PM
#10
Fanatic Member
It's the code itself, not VB. It's just a case of not being careful about testing something out. If you look at what I wrote, you can see that 0 and 1 won't fall through as being non numeric. In the case of NotLKH's test, it's probably testing the piece against "True" or "False" rather than a number.
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
-
Aug 25th, 2001, 06:51 PM
#11
Thread Starter
pathfinder
Filburt1,
Just tweek the code by including your Hand Coded Function Def of the split function, And alter My code to work when it uses the Split. I don't know if there needs to be any other Function Substitution but I don't think so.
-Lou
-
Aug 25th, 2001, 06:52 PM
#12
Thread Starter
pathfinder
Originally posted by Kaverin
It's the code itself, not VB. It's just a case of not being careful about testing something out. If you look at what I wrote, you can see that 0 and 1 won't fall through as being non numeric. In the case of NotLKH's test, it's probably testing the piece against "True" or "False" rather than a number.
Then, What about this?
VB Code:
Private Sub Command2_Click()
Dim v As Variant, v2 As Variant
v = Split("4 + 3 + 2 + 1 + 0")
For Each v2 In v
Select Case v2
Case IsNumeric(v2)
MsgBox v2 & " is numeric"
Case Else
MsgBox v2 & " is not numeric"
End Select
Next v2
End Sub
-Lou
-
Aug 25th, 2001, 06:54 PM
#13
Member
Originally posted by NotLKH
Filburt1,
Just tweek the code by including your Hand Coded Function Def of the split function, And alter My code to work when it uses the Split. I don't know if there needs to be any other Function Substitution but I don't think so.
-Lou
The immediate window:
VB Code:
?IsNumeric("0")
True
?IsNumeric("+ 0")
True
?IsNumeric(" 0 ")
True
Well, that function works.
-
Aug 25th, 2001, 06:59 PM
#14
Fanatic Member
This is what I saw (after changing the msgbox to debug.print, because msgbox is very annoying).
Code:
4 is numeric
+ is not numeric
3 is numeric
+ is not numeric
2 is numeric
+ is not numeric
1 is numeric
+ is not numeric
0 is numeric
I'm at a loss to explain what you're seeing, but I see it like I expect.
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
-
Aug 25th, 2001, 07:02 PM
#15
Thread Starter
pathfinder
-
Aug 25th, 2001, 07:05 PM
#16
Fanatic Member
Oh, and just to show this as well:
VB Code:
Dim v As Variant
v = "0"
Debug.Print IsNumeric(v)
Shows "True" just as I expect again, so I don't think it's misinterpreting a string "0" inside as variant as not being numeric.
And something kinda funny... I just noticed I passed 666 posts (that's the only evil looking smiley) heh heh.
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
-
Aug 25th, 2001, 07:08 PM
#17
Fanatic Member
Originally posted by NotLKH
Then, What about this?
-Lou
v = Split("4 + 3 + 2 + 1 + 0 + -1 + -2 + -3 + -4")...all numeric but zero. Something funcky going on here....But then VB has always been funky with true being all but zero.
"When other numeric types are converted to Boolean values, 0 becomes False and all other values become True. When Boolean values are converted to other data types, False becomes 0 and True becomes -1."
-
Aug 25th, 2001, 07:09 PM
#18
Fanatic Member
Ok, just to make sure I didn't overlook something, I tried another Split().
VB Code:
Private Sub Command1_Click()
Dim v As Variant, vi As Variant
v = Split("0 0 0")
For Each vi In v
Debug.Print vi;
Select Case IsNumeric(vi)
Case True
Debug.Print " is numeric"
Case False
Debug.Print " is not numeric"
End Select
Next vi
End Sub
Immediate window:
Code:
0 is numeric
0 is numeric
0 is numeric
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
-
Aug 25th, 2001, 07:09 PM
#19
Thread Starter
pathfinder
Originally posted by Kaverin
This is what I saw (after changing the msgbox to debug.print, because msgbox is very annoying).
Code:
4 is numeric
+ is not numeric
3 is numeric
+ is not numeric
2 is numeric
+ is not numeric
1 is numeric
+ is not numeric
0 is numeric
I'm at a loss to explain what you're seeing, but I see it like I expect.
This is Strange, Assuming you used My Tweek of your code, I ReTested it with DeBug.Print, and Received this:
Code:
4 is numeric
+ is not numeric
3 is numeric
+ is not numeric
2 is numeric
+ is not numeric
1 is numeric
+ is not numeric
0 is not numeric
So, the Difference, since you have VB5, could be You are passing the elements of split back as strings.
AAAHA!
Check this out!
VB Code:
Private Sub Command2_Click()
Dim v2 As Variant
v2 = "0"
Select Case v2
Case IsNumeric(v2)
MsgBox v2 & " is numeric"
Case Else
MsgBox v2 & " is not numeric"
End Select
v2 = "1"
Select Case v2
Case IsNumeric(v2)
MsgBox v2 & " is numeric"
Case Else
MsgBox v2 & " is not numeric"
End Select
End Sub
Apparantly, variants don't evaluate correctly as Numeric, if = 0!
Strange!
-Lou
-
Aug 25th, 2001, 07:13 PM
#20
Thread Starter
pathfinder
Originally posted by Kaverin
Oh, and just to show this as well:
VB Code:
Dim v As Variant
v = "0"
Debug.Print IsNumeric(v)
Shows "True" just as I expect again, so I don't think it's misinterpreting a string "0" inside as variant as not being numeric.
And something kinda funny... I just noticed I passed 666 posts (that's the only evil looking smiley) heh heh.
It seems to Happen only in the Case Structure when a Variant = 0,
again, see this code:
VB Code:
Private Sub Command2_Click()
Dim v2 As Variant
v2 = "0"
Select Case v2
Case IsNumeric(v2)
MsgBox v2 & " is numeric"
Case Else
MsgBox v2 & " is not numeric"
End Select
v2 = "1"
Select Case v2
Case IsNumeric(v2)
MsgBox v2 & " is numeric"
Case Else
MsgBox v2 & " is not numeric"
End Select
End Sub
-Lou
-
Aug 25th, 2001, 07:14 PM
#21
NotLKH, let's look at this part of your first code:
VB Code:
Select Case One
Case ""
MsgBox "NULL"
Case "+"
MsgBox "PLUS"
Case IsNumeric(One) = True
MsgBox One & " is a Number"
Case Else
MsgBox One & " is Not a Number"
End Select
Let's assume One is "1". Then we can evaluate that IsNumeric case like this:
VB Code:
Select Case One
Case ""
MsgBox "NULL"
Case "+"
MsgBox "PLUS"
Case True 'IsNumeric(One) = True
MsgBox One & " is a Number"
Case Else
MsgBox One & " is Not a Number"
End Select
Because IsNumeric("1") is true, (IsNumeric(1) = True) is also true. So in the end it looks like the select case statement is comparing "1" with True. We all know "1" does not equal True. Does this make sense now?
-
Aug 25th, 2001, 07:18 PM
#22
Thread Starter
pathfinder
Originally posted by Tygur
NotLKH, let's look at this part of your first code:
VB Code:
Select Case One
Case ""
MsgBox "NULL"
Case "+"
MsgBox "PLUS"
Case IsNumeric(One) = True
MsgBox One & " is a Number"
Case Else
MsgBox One & " is Not a Number"
End Select
Let's assume One is "1". Then we can evaluate that IsNumeric case like this:
VB Code:
Select Case One
Case ""
MsgBox "NULL"
Case "+"
MsgBox "PLUS"
Case True 'IsNumeric(One) = True
MsgBox One & " is a Number"
Case Else
MsgBox One & " is Not a Number"
End Select
Because IsNumeric("1") is true, (IsNumeric(1) = True) is also true. So in the end it looks like the select case statement is comparing "1" with True. We all know "1" does not equal True. Does this make sense now?
I saw it before, but Your point is Explicit and Direct.
Thanks.
-Lou
Last edited by NotLKH; Aug 25th, 2001 at 07:23 PM.
-
Aug 25th, 2001, 07:19 PM
#23
Originally posted by NotLKH
It seems to Happen only in the Case Structure when a Variant = 0,
again, see this code:
VB Code:
Private Sub Command2_Click()
Dim v2 As Variant
v2 = "0"
Select Case v2
Case IsNumeric(v2)
MsgBox v2 & " is numeric"
Case Else
MsgBox v2 & " is not numeric"
End Select
v2 = "1"
Select Case v2
Case IsNumeric(v2)
MsgBox v2 & " is numeric"
Case Else
MsgBox v2 & " is not numeric"
End Select
End Sub
-Lou
As for this code, try these two lines and hopefully all will be explained:
MsgBox "0" = True
MsgBox "1" = True
-
Aug 25th, 2001, 07:23 PM
#24
Fanatic Member
Originally posted by Kaverin
[B]Oh, and just to show this as well:
VB Code:
Dim v As Variant
v = "0"
Debug.Print IsNumeric(v)
Shows "True" just as I expect again, so I don't think it's misinterpreting a string "0" inside as variant as not being numeric.
That's not what I saw. I thought perhaps I'd slipped and tried it back then, and didn't get that result.
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
-
Aug 25th, 2001, 10:41 PM
#25
Thread Starter
pathfinder
Well, Thanks Everybody.
Let me just post this code, Derived from Tygur, although
I don't think he tested it. I was surprised by the results.
VB Code:
Private Sub Command1_Click()
Dim One As Variant '
Dim All As Variant
Dim MY_STR As String
MY_STR = "0 A 3.14 1 -2 -1"
All = Split(MY_STR)
For Each One In All
Select Case One
Case "+"
MsgBox One & " Is Plus"
Case "A"
MsgBox One & " Is An A"
Case True 'Instead of IsNumeric(One)
MsgBox One & " Is Numeric"
Case Else
MsgBox One & " Is Not Numeric"
End Select
Next One
'''Logically, With the String set as it is, None of the elements
'''of the Split are equal to the Boolean Value "True"
'''Therefore, Never should we see the msgbox Display ... "Is Numeric".
'''However, for each of These: 3.14, 1 ,-2, and -1, We DO see that message box.
'''but we don't see that message for the value One = "0".
'''Its a strange way to check if a variant is Numeric but <> 0, but
'''strangely, it works.
-Lou
-
Aug 25th, 2001, 10:49 PM
#26
It's not really so strange. My guess is that when it compares "1" to True, it converts "1" to 1 (the number) and then to True (1 is True because it's nonzero). And True does equal True.
-
Aug 25th, 2001, 10:54 PM
#27
Thread Starter
pathfinder
Originally posted by Tygur
It's not really so strange. My guess is that when it compares "1" to True, it converts "1" to 1 (the number) and then to True (1 is True because it's nonzero). And True does equal True.
That has to be it. Although,
"The True keyword has a value equal to -1."
So, This Displays The variant "0" is False, and All other Numeric Variants are True.
VB Code:
Private Sub Command1_Click()
Dim One As Variant '
Dim All As Variant
Dim MY_STR As String
MY_STR = "0 3.14 1 -2 -1"
For Each One In All
Select Case One
Case True
MsgBox One & " Is True"
Case False
MsgBox One & " Is False"
End Select
Next One
Thanks again!
-Lou
Last edited by NotLKH; Aug 25th, 2001 at 10:58 PM.
-
Aug 25th, 2001, 11:18 PM
#28
"The True keyword has a value equal to -1."
All that really means is that CLng(True) is -1. In other words, when you convert True to a number in VB, it's -1. But that doesn't mean that when you convert a number to a boolean value, only -1 is True. -1 is just one of many True values, as you already saw.
-
Aug 25th, 2001, 11:30 PM
#29
PowerPoster
Hi
Yeah "0" is the only oddity in the conversion to True, False. All other numeric strings / variants evaluate to True. But i still agree with Kaverin earlier on that the prob in ur original code is that u are testing the Numeric status incorrectly. U are testing the 'text' within each One and then within the Select case u change to test Isnumeric(One). The following example is a very lame attempt to try to show what i mean
VB Code:
MyVal = 2
OtherVal = 4
Select Case MyVal
Case 0: Debug.Print "I am zero"
Case 1: Debug.Print "I am one"
Case 2: Debug.Print "I am two"
Case OtherVal = 4: Debug.Print "I am four"'Wont ever get here
'Even if u change value of MyVar
End Select
'Which is similar in construct to the one that u had prev
For Each One In MY_STUFF
Select Case One
Case ""
MsgBox "NULL"
Case "+"
MsgBox "PLUS"
Case IsNumeric(One) = True'Different test
MsgBox One & " is a Number"
Case Else
MsgBox One & " is Not a Number"
End Select
Ok was a lamo attempt. But there is no real prob with how VB tests for numeric of strings.
Regards
Stuart
-
Aug 26th, 2001, 10:40 AM
#30
Thread Starter
pathfinder
Originally posted by beachbum
Hi
Yeah "0" is the only oddity in the conversion to True, False. All other numeric strings / variants evaluate to True. But i still agree with Kaverin earlier on that the prob in ur original code is that u are testing the Numeric status incorrectly. U are testing the 'text' within each One and then within the Select case u change to test Isnumeric(One). The following example is a very lame attempt to try to show what i mean
VB Code:
MyVal = 2
OtherVal = 4
Select Case MyVal
Case 0: Debug.Print "I am zero"
Case 1: Debug.Print "I am one"
Case 2: Debug.Print "I am two"
Case OtherVal = 4: Debug.Print "I am four"'Wont ever get here
'Even if u change value of MyVar
End Select
'Which is similar in construct to the one that u had prev
For Each One In MY_STUFF
Select Case One
Case ""
MsgBox "NULL"
Case "+"
MsgBox "PLUS"
Case IsNumeric(One) = True'Different test
MsgBox One & " is a Number"
Case Else
MsgBox One & " is Not a Number"
End Select
Ok was a lamo attempt. But there is no real prob with how VB tests for numeric of strings.
Regards
Stuart
I see what you mean, but Unfortunately the problem was
it DID get there, and Actually gave me the result I expected from
an erroneous perspective, except on 0. But, Thru Tygur, et al, the
Code now has become the following, with a Direct case that will
indicate if a Variant Is a Numeric Value, Including 0. The only
potential problem was if The starting string contained "true"
or "false" as an element, but if those cases are Specified even
before the Numeric test, then They are Triggered before it even
gets to the Numeric case structure.
VB Code:
Private Sub Command1_Click()
Dim MY_STR As String
Dim One As Variant
Dim MY_STUFF As Variant
MY_STR = "1 0 3.14 . True False +"
'''MY_STR will always be tested UpperCase
MY_STR = UCase(MY_STR)
MY_STUFF = Split(MY_STR, " ", -1, vbTextCompare)
For Each One In MY_STUFF
Select Case One
Case "" '''Do Nothing when Null
Case "+"
MsgBox "PLUS"
Case "TRUE", "FALSE" '''Just in Case These are in the String. In my real code, the Sub would Exit, Messaging the user about a Syntactical error.
MsgBox One & " Is Boolean"
Case True, False '''This is a test for IsNumeric
MsgBox One & " is a Number"
Case Else
MsgBox One & " is Something Not Specified"
End Select
Next One
End Sub
At least this way I don't have to have a seconde case structure,
or an if test, under the Case Else.
However, Do you see any problems with this?
-Lou
Last edited by NotLKH; Aug 26th, 2001 at 10:44 AM.
-
Aug 26th, 2001, 12:11 PM
#31
That looks like it shouldn't work (If it's not true, and it's not false, then what is it??), but it does anyway....weird..
This also seems to work:
VB Code:
Select Case "1500"
Case 0 To 9
MsgBox "Number"
Case Else
MsgBox "No Number"
End Select
-
Aug 26th, 2001, 12:22 PM
#32
To make the above code work with variants and negative numbers, there has to be some changes:
VB Code:
Dim V As Variant
V = -1500
Select Case V
Case "-" To "9"
MsgBox "Number"
Case Else
MsgBox "No Number"
End Select
But now it lets through strings that begin with a "/"..
Kinda surprising that "Case True, False" works so well..
-
Aug 26th, 2001, 12:38 PM
#33
Thread Starter
pathfinder
Heh,
Good Try, though!
But as You pointed out, Since Number strings evaluate as True,
Except for 0, which Evaluates as False, Logically its easy to
understand why it works, although from a Human perspective, it
seems really unnatural. I guess code like that should be reffered to
as Mutant Code, X-Code, a real Circus Freak, or Deviant Code.
And, you beat me to the punch with your newest code.
I was going to post this test I just did:
VB Code:
Private Sub Command1_Click()
Dim MY_STR As String
Dim One As Variant
Dim All As Variant
'''Tygurs, Applied
Debug.Print "Tygurs, Applied"
MY_STR = "1500 3.14 -2.22 - 1 0 ."
All = Split(MY_STR)
For Each One In All
Select Case One
Case 0 To 9
Debug.Print One; " Number"
Case Else
Debug.Print One; " No Number"
End Select
Next One
'''Tygurs, Direct
Debug.Print "Tygurs, Direct"
Select Case "1500"
Case 0 To 9
Debug.Print "1500 Number"
Case Else
MsgBox "1500 No Number"
End Select
End Sub
which produced the following:
Code:
Tygurs, Applied
1500 No Number
3.14 Number
-2.22 No Number
- No Number
1 Number
0 Number
. No Number
Tygurs, Direct
1500 Number
which illustrated the problems when used on a split string, with
variant elements, but I guess I don't have to now.
-Lou
-
Aug 26th, 2001, 12:44 PM
#34
Fanatic Member
Tygur, that's because the ASCII of "/" is between that of "-" and "9".
I still don't see the hang up on relying on the way VB converts things (which I think is very sloppy btw) and its true/false evaluation . I see no reason why, if you want to check for something's numerical nature, that Select Case IsNumeric(X) shouldn't be used, or If IsNumeric(X) Then ... Else ... End If since it makes the most sense and won't ever give you false impressions of something.
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
-
Aug 26th, 2001, 12:56 PM
#35
Thread Starter
pathfinder
Originally posted by Kaverin
I see no reason why, if you want to check for something's numerical nature, that Select Case IsNumeric(X) shouldn't be used, or If IsNumeric(X) Then ... Else ... End If since it makes the most sense and won't ever give you false impressions of something.
My hang up is, Nesting a select case within a select case.
If it is possible to have a check on a Variant for Multiple values,
AND a check that determines if its generally a number, all within
One Select case structure, Its less messy, more elegent, than
Testing for Direct Matches in a select case structure, then placeing
a check for IsNumeric in the Case Else area, and still dealing with the other Variants that enter into the Else area.
Thats how I've done it, but I've never liked it.
Now I have the alternative.
-Lou
-
Aug 26th, 2001, 01:03 PM
#36
Originally posted by Kaverin
Tygur, that's because the ASCII of "/" is between that of "-" and "9".
Yeah, I know that There's also a period, but I let that go, because ".9" is still a number.
I've been thinking about that code (the code that still lets the slash through) and I realized that it had lots more problems than that, because it only looks at the first character.
If you still want to use just one select case statement and don't feel like relying on vb's weird conversions, you can do something like this (Though you will have to repeat the variable name, One, lots of times):
VB Code:
Select Case True
Case One = "" '''Do Nothing when Null
Case One = "+"
MsgBox "PLUS"
Case One = "TRUE", One = "FALSE" '''Just in Case These are in the String. In my real code, the Sub would Exit, Messaging the user about a Syntactical error.
MsgBox One & " Is Boolean"
Case IsNumeric(One) '''This is a test for IsNumeric
MsgBox One & " is a Number"
Case Else
MsgBox One & " is Something Not Specified"
End Select
-
Aug 26th, 2001, 01:08 PM
#37
Thread Starter
pathfinder
Nope. I like this better:
VB Code:
Private Sub Command1_Click()
Dim MY_STR As String
Dim One As Variant
Dim MY_STUFF As Variant
MY_STR = "1 0 3.14 . -1 True False +"
'''MY_STR will always be tested UpperCase
MY_STR = UCase(MY_STR)
MY_STUFF = Split(MY_STR, " ", -1, vbTextCompare)
For Each One In MY_STUFF
Select Case One
Case "" '''Do Nothing when Null
Case "+"
MsgBox "PLUS"
Case "TRUE", "FALSE" '''Just in Case These are in the String. In my real code, the Sub would Exit, Messaging the user about a Syntactical error.
MsgBox One & " Is Boolean"
Case True, False '''This is a test for IsNumeric
MsgBox One & " is a Number"
Case Else
MsgBox One & " is Something Not Specified"
End Select
Next One
End Sub
In the code that you propose to use, With IsNumeric(One), Which is Really True when one is a Number string, It fails on 0. Not Good.
And as you already saw, this code {above} works perfectly.
-Lou
-
Aug 26th, 2001, 01:31 PM
#38
The code I most recently proposed does work with "0", look at it a little closer.
Use whatever code you prefer. I was just pointing out that another way existed. I myself feel very uncomfortable with that "Case True, False" section, but it does appear to work.
-
Aug 26th, 2001, 01:35 PM
#39
Thread Starter
pathfinder
[embarrased]OOPS,[/embarrased]
Missed the Select Case True line.
Well, Let me test it out and see what happens.
-Lou
-
Aug 26th, 2001, 01:42 PM
#40
Thread Starter
pathfinder
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
|