Page 1 of 2 12 LastLast
Results 1 to 40 of 45

Thread: Is Split Buggy? Why Isn't 0 a Number?

  1. #1

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    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:
    1. Private Sub Command2_Click()
    2.     Dim MY_STR As String
    3.     MY_STR = "1 + 0"
    4.     MY_STUFF = Split(MY_STR, " ", -1, vbTextCompare)
    5.     For Each One In MY_STUFF
    6.         Select Case One
    7.             Case ""
    8.                 MsgBox "NULL"
    9.             Case "+"
    10.                 MsgBox "PLUS"
    11.             Case IsNumeric(One) = True
    12.                 MsgBox One & " is a Number"
    13.             Case Else
    14.                 MsgBox One & " is Not a Number"
    15.         End Select
    16.     Next One
    17.     MY_STR = "0"
    18.     If IsNumeric(MY_STR) Then
    19.         MsgBox MY_STR & " is a number"
    20.     Else
    21.         MsgBox MY_STR & " is NOT a number"
    22.     End If
    23. End Sub

    The following is even worse, 1 isn't even a number anymore!

    VB Code:
    1. Private Sub Command2_Click()
    2.     Dim MY_STR As String
    3.     Dim Two As String
    4.     MY_STR = "1 + 0"
    5.     MY_STUFF = Split(MY_STR, " ", -1, vbTextCompare)
    6.     For Each One In MY_STUFF
    7.         Two = One
    8.         Select Case Two
    9.             Case ""
    10.                 MsgBox "NULL"
    11.             Case "+"
    12.                 MsgBox "PLUS"
    13.             Case IsNumeric(Two) = True
    14.                 MsgBox Two & " is a Number"
    15.             Case Else
    16.                 MsgBox Two & " is Not a Number"
    17.         End Select
    18.     Next One
    19. End Sub

    Am I Missing something? Or should I ReBoot / ReIntsall VB?


    -Lou

  2. #2

  3. #3
    Helger
    Guest
    dim myStr as string ...

  4. #4
    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...

  5. #5
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    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:
    1. Case IsNumeric(One) = True
    2.    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:
    1. Private Sub Command3_Click()
    2.    Dim v As Variant, v2 As Variant
    3.    v = Split("1 + 0")
    4.    For Each v2 In v
    5.       Select Case IsNumeric(v2)
    6.          Case True
    7.             Debug.Print v2; " is numeric"
    8.          Case False
    9.             Debug.Print v2; " is not numeric"
    10.       End Select
    11.    Next v2
    12. 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

  6. #6
    Frenzied Member
    Join Date
    Aug 2001
    Posts
    1,075
    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.

  7. #7
    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?

  8. #8
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Weird......NotLKH's code works for string/variant = "1" but not if string/variant = "0"........Hmmmmmmm.....Veeeerrrry Interesting.

  9. #9

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    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:
    1. Private Sub Command2_Click()
    2.    Dim v As Variant, v2 As Variant
    3.    v = Split("4 + 3 + 2 + 1 + 0")
    4.    For Each v2 In v
    5.         Select Case v2
    6.             Case IsNumeric(v2)
    7.                 MsgBox v2 & " is numeric"
    8.             Case Else
    9.                 MsgBox v2 & " is not numeric"
    10.         End Select
    11.    Next v2
    12. 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

  10. #10
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    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

  11. #11

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    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

  12. #12

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    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:
    1. Private Sub Command2_Click()
    2.    Dim v As Variant, v2 As Variant
    3.    v = Split("4 + 3 + 2 + 1 + 0")
    4.    For Each v2 In v
    5.         Select Case v2
    6.             Case IsNumeric(v2)
    7.                 MsgBox v2 & " is numeric"
    8.             Case Else
    9.                 MsgBox v2 & " is not numeric"
    10.         End Select
    11.    Next v2
    12. End Sub

    -Lou

  13. #13
    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:
    1. ?IsNumeric("0")
    2. True
    3. ?IsNumeric("+ 0")
    4. True
    5. ?IsNumeric(" 0 ")
    6. True

    Well, that function works.

  14. #14
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    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

  15. #15

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Heh,
    Its not the IsNumeric function on Individual cases, Just only when
    used with Split and if an Element of Split = "0"

    Also, Kaverin, I think you only
    looked at the second code in My origional Post, since you said:

    "you can see that 0 and 1 won't fall through"

    That second bit of code was posted as an illustration of what happens when you assign a Variable dimmed as a string to the
    value passed by a split element. Yes, Both 0 and 1 fell out, BUT
    The first Code Passed 1 as Numeric, and 0 as NonNumeric.

    Obvously The 2nd code has something wrong, But the 1st?
    Besides your point about "= True".


    -Lou

  16. #16
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    Oh, and just to show this as well:
    VB Code:
    1. Dim v As Variant
    2. v = "0"
    3. 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

  17. #17
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    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."

  18. #18
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    Ok, just to make sure I didn't overlook something, I tried another Split().
    VB Code:
    1. Private Sub Command1_Click()
    2.    Dim v As Variant, vi As Variant
    3.    v = Split("0 0 0")
    4.    For Each vi In v
    5.       Debug.Print vi;
    6.       Select Case IsNumeric(vi)
    7.          Case True
    8.             Debug.Print " is numeric"
    9.          Case False
    10.             Debug.Print " is not numeric"
    11.       End Select
    12.    Next vi
    13. 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

  19. #19

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    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:
    1. Private Sub Command2_Click()
    2.     Dim v2 As Variant
    3.     v2 = "0"
    4.         Select Case v2
    5.             Case IsNumeric(v2)
    6.                 MsgBox v2 & " is numeric"
    7.             Case Else
    8.                 MsgBox v2 & " is not numeric"
    9.         End Select
    10.     v2 = "1"
    11.         Select Case v2
    12.             Case IsNumeric(v2)
    13.                 MsgBox v2 & " is numeric"
    14.             Case Else
    15.                 MsgBox v2 & " is not numeric"
    16.         End Select
    17. End Sub

    Apparantly, variants don't evaluate correctly as Numeric, if = 0!

    Strange!

    -Lou

  20. #20

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Originally posted by Kaverin
    Oh, and just to show this as well:
    VB Code:
    1. Dim v As Variant
    2. v = "0"
    3. 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:
    1. Private Sub Command2_Click()
    2.     Dim v2 As Variant
    3.     v2 = "0"
    4.         Select Case v2
    5.             Case IsNumeric(v2)
    6.                 MsgBox v2 & " is numeric"
    7.             Case Else
    8.                 MsgBox v2 & " is not numeric"
    9.         End Select
    10.     v2 = "1"
    11.         Select Case v2
    12.             Case IsNumeric(v2)
    13.                 MsgBox v2 & " is numeric"
    14.             Case Else
    15.                 MsgBox v2 & " is not numeric"
    16.         End Select
    17. End Sub

    -Lou

  21. #21
    Tygur
    Guest
    NotLKH, let's look at this part of your first code:
    VB Code:
    1. Select Case One
    2.     Case ""
    3.         MsgBox "NULL"
    4.     Case "+"
    5.         MsgBox "PLUS"
    6.     Case IsNumeric(One) = True
    7.         MsgBox One & " is a Number"
    8.     Case Else
    9.         MsgBox One & " is Not a Number"
    10. End Select
    Let's assume One is "1". Then we can evaluate that IsNumeric case like this:
    VB Code:
    1. Select Case One
    2.     Case ""
    3.         MsgBox "NULL"
    4.     Case "+"
    5.         MsgBox "PLUS"
    6.     Case True 'IsNumeric(One) = True
    7.         MsgBox One & " is a Number"
    8.     Case Else
    9.         MsgBox One & " is Not a Number"
    10. 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?

  22. #22

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Originally posted by Tygur
    NotLKH, let's look at this part of your first code:
    VB Code:
    1. Select Case One
    2.     Case ""
    3.         MsgBox "NULL"
    4.     Case "+"
    5.         MsgBox "PLUS"
    6.     Case IsNumeric(One) = True
    7.         MsgBox One & " is a Number"
    8.     Case Else
    9.         MsgBox One & " is Not a Number"
    10. End Select
    Let's assume One is "1". Then we can evaluate that IsNumeric case like this:
    VB Code:
    1. Select Case One
    2.     Case ""
    3.         MsgBox "NULL"
    4.     Case "+"
    5.         MsgBox "PLUS"
    6.     Case True 'IsNumeric(One) = True
    7.         MsgBox One & " is a Number"
    8.     Case Else
    9.         MsgBox One & " is Not a Number"
    10. 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

  23. #23
    Tygur
    Guest
    Originally posted by NotLKH


    It seems to Happen only in the Case Structure when a Variant = 0,
    again, see this code:

    VB Code:
    1. Private Sub Command2_Click()
    2.     Dim v2 As Variant
    3.     v2 = "0"
    4.         Select Case v2
    5.             Case IsNumeric(v2)
    6.                 MsgBox v2 & " is numeric"
    7.             Case Else
    8.                 MsgBox v2 & " is not numeric"
    9.         End Select
    10.     v2 = "1"
    11.         Select Case v2
    12.             Case IsNumeric(v2)
    13.                 MsgBox v2 & " is numeric"
    14.             Case Else
    15.                 MsgBox v2 & " is not numeric"
    16.         End Select
    17. End Sub

    -Lou
    As for this code, try these two lines and hopefully all will be explained:
    MsgBox "0" = True
    MsgBox "1" = True

  24. #24
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    Originally posted by Kaverin
    [B]Oh, and just to show this as well:
    VB Code:
    1. Dim v As Variant
    2. v = "0"
    3. 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

  25. #25

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    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:
    1. Private Sub Command1_Click()
    2. Dim One As Variant '
    3. Dim All As Variant
    4. Dim MY_STR As String
    5. MY_STR = "0 A 3.14 1 -2 -1"
    6. All = Split(MY_STR)
    7. For Each One In All
    8.     Select Case One
    9.         Case "+"
    10.             MsgBox One & " Is Plus"
    11.         Case "A"
    12.             MsgBox One & " Is An A"
    13.         Case True 'Instead of IsNumeric(One)
    14.             MsgBox One & " Is Numeric"
    15.         Case Else
    16.             MsgBox One & " Is Not Numeric"
    17.     End Select
    18. Next One
    19. '''Logically, With the String set as it is, None of the elements
    20. '''of the Split are equal to the Boolean Value "True"
    21. '''Therefore, Never should we see the msgbox Display ... "Is Numeric".
    22.  
    23. '''However, for each of These: 3.14, 1 ,-2, and -1, We DO see that message box.
    24. '''but we don't see that message for the value One = "0".
    25.  
    26. '''Its a strange way to check if a variant is Numeric but <> 0, but
    27. '''strangely, it works.




    -Lou

  26. #26
    Tygur
    Guest
    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.

  27. #27

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    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:
    1. Private Sub Command1_Click()
    2. Dim One As Variant '
    3. Dim All As Variant
    4. Dim MY_STR As String
    5. MY_STR = "0 3.14 1 -2 -1"
    6. For Each One In All
    7.     Select Case One
    8.         Case True
    9.             MsgBox One & " Is True"
    10.         Case False
    11.             MsgBox One & " Is False"
    12.     End Select
    13. Next One

    Thanks again!

    -Lou

  28. #28
    Tygur
    Guest
    "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.

  29. #29
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    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:
    1. MyVal = 2
    2.     OtherVal = 4
    3.     Select Case MyVal
    4.         Case 0: Debug.Print "I am zero"
    5.         Case 1: Debug.Print "I am one"
    6.         Case 2: Debug.Print "I am two"
    7.         Case OtherVal = 4: Debug.Print "I am four"'Wont ever get here
    8.         'Even if u change value of MyVar
    9.     End Select
    10.  
    11. 'Which is similar in construct to the one that u had prev
    12.     For Each One In MY_STUFF
    13.         Select Case One
    14.             Case ""
    15.                 MsgBox "NULL"
    16.             Case "+"
    17.                 MsgBox "PLUS"
    18.             Case IsNumeric(One) = True'Different test
    19.                 MsgBox One & " is a Number"
    20.             Case Else
    21.                 MsgBox One & " is Not a Number"
    22.         End Select

    Ok was a lamo attempt. But there is no real prob with how VB tests for numeric of strings.
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  30. #30

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    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:
    1. MyVal = 2
    2.     OtherVal = 4
    3.     Select Case MyVal
    4.         Case 0: Debug.Print "I am zero"
    5.         Case 1: Debug.Print "I am one"
    6.         Case 2: Debug.Print "I am two"
    7.         Case OtherVal = 4: Debug.Print "I am four"'Wont ever get here
    8.         'Even if u change value of MyVar
    9.     End Select
    10.  
    11. 'Which is similar in construct to the one that u had prev
    12.     For Each One In MY_STUFF
    13.         Select Case One
    14.             Case ""
    15.                 MsgBox "NULL"
    16.             Case "+"
    17.                 MsgBox "PLUS"
    18.             Case IsNumeric(One) = True'Different test
    19.                 MsgBox One & " is a Number"
    20.             Case Else
    21.                 MsgBox One & " is Not a Number"
    22.         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:
    1. Private Sub Command1_Click()
    2.     Dim MY_STR As String
    3.     Dim One As Variant
    4.     Dim MY_STUFF As Variant
    5.     MY_STR = "1  0 3.14 . True False +"
    6.     '''MY_STR will always be tested UpperCase
    7.     MY_STR = UCase(MY_STR)
    8.     MY_STUFF = Split(MY_STR, " ", -1, vbTextCompare)
    9.     For Each One In MY_STUFF
    10.         Select Case One
    11.             Case ""                 '''Do Nothing when Null
    12.             Case "+"
    13.                 MsgBox "PLUS"
    14.             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.
    15.                 MsgBox One & " Is Boolean"
    16.             Case True, False        '''This is a test for IsNumeric
    17.                 MsgBox One & " is a Number"
    18.             Case Else
    19.                 MsgBox One & " is Something Not Specified"
    20.         End Select
    21.     Next One
    22. 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

  31. #31
    Tygur
    Guest
    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:
    1. Select Case "1500"
    2.     Case 0 To 9
    3.         MsgBox "Number"
    4.     Case Else
    5.         MsgBox "No Number"
    6. End Select

  32. #32
    Tygur
    Guest
    To make the above code work with variants and negative numbers, there has to be some changes:
    VB Code:
    1. Dim V As Variant
    2. V = -1500
    3. Select Case V
    4.     Case "-" To "9"
    5.         MsgBox "Number"
    6.     Case Else
    7.         MsgBox "No Number"
    8. End Select

    But now it lets through strings that begin with a "/"..

    Kinda surprising that "Case True, False" works so well..

  33. #33

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    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:
    1. Private Sub Command1_Click()
    2. Dim MY_STR As String
    3. Dim One As Variant
    4. Dim All As Variant
    5. '''Tygurs, Applied
    6. Debug.Print "Tygurs, Applied"
    7. MY_STR = "1500 3.14 -2.22 - 1 0 ."
    8. All = Split(MY_STR)
    9. For Each One In All
    10. Select Case One
    11.     Case 0 To 9
    12.         Debug.Print One; " Number"
    13.     Case Else
    14.         Debug.Print One; " No Number"
    15. End Select
    16. Next One
    17. '''Tygurs, Direct
    18. Debug.Print "Tygurs, Direct"
    19. Select Case "1500"
    20.     Case 0 To 9
    21.         Debug.Print "1500 Number"
    22.     Case Else
    23.         MsgBox "1500 No Number"
    24. End Select
    25. 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

  34. #34
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    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

  35. #35

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    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

  36. #36
    Tygur
    Guest
    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:
    1. Select Case True
    2.     Case One = ""                 '''Do Nothing when Null
    3.     Case One = "+"
    4.         MsgBox "PLUS"
    5.     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.
    6.         MsgBox One & " Is Boolean"
    7.     Case IsNumeric(One)        '''This is a test for IsNumeric
    8.         MsgBox One & " is a Number"
    9.     Case Else
    10.         MsgBox One & " is Something Not Specified"
    11. End Select

  37. #37

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Nope. I like this better:

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim MY_STR As String
    3.     Dim One As Variant
    4.     Dim MY_STUFF As Variant
    5.     MY_STR = "1  0 3.14 . -1 True False +"
    6.     '''MY_STR will always be tested UpperCase
    7.     MY_STR = UCase(MY_STR)
    8.     MY_STUFF = Split(MY_STR, " ", -1, vbTextCompare)
    9.     For Each One In MY_STUFF
    10.         Select Case One
    11.             Case ""                 '''Do Nothing when Null
    12.             Case "+"
    13.                 MsgBox "PLUS"
    14.             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.
    15.                 MsgBox One & " Is Boolean"
    16.             Case True, False        '''This is a test for IsNumeric
    17.                 MsgBox One & " is a Number"
    18.             Case Else
    19.                 MsgBox One & " is Something Not Specified"
    20.         End Select
    21.     Next One
    22. 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

  38. #38
    Tygur
    Guest
    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.

  39. #39

  40. #40

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width