Results 1 to 21 of 21

Thread: select case with non integer numbers

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    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

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    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:
    1. Select Case CStr(power)
    2.     Case "0.001"
    3.         ' Statements
    4.     Case "1"
    5.         ' Statements
    6.     Case "2"
    7.         ' Statements
    8.     Case "20.001"
    9.         ' Statements
    10.     Case "40"
    11.         ' Statements
    12.     Case "40.001"
    13.         ' Statements
    14.     Case "60"
    15.         ' Statements
    16.     Case "60.001"
    17.         ' Statements
    18. End Select


    Cheers,


    RyanJ
    My Blog.

    Ryan Jones.

  3. #3
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: select case with non integer numbers

    Select Case will work with strings.

    VB Code:
    1. Dim power as Double
    2.     Dim pString as String
    3.  
    4.     pString = power
    5.     Select Case pString
    6. '
    7. '
    8. '
    This world is not my home. I'm just passing through.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: select case with non integer numbers

    is there no way to check for non integer values in a select structure then?

  5. #5
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    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:
    1. Select Case power * 1000
    2.     Case 1  'power = 0.001
    3.         ' Statements
    4.     Case 1000 'power = 1
    5.         ' Statements
    6.     Case 2000 'power = 2
    7.         ' Statements
    8.     Case 20001 'power = 20.001
    This world is not my home. I'm just passing through.

  6. #6
    Hyperactive Member
    Join Date
    Feb 2005
    Location
    Wollongong. NSW. Australia
    Posts
    470

    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:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.   If KeyAscii = 13 Then
    3.  
    4.     Select Case Text1.Text
    5.     Case 1:
    6.       MsgBox "1 was entered"
    7.    
    8.     Case 0.01
    9.       MsgBox "0.01 was entered"
    10.    
    11.     Case Else
    12.        MsgBox "Something else was entered"
    13.      End Select
    14.   End If
    15. End Sub

  7. #7
    Hyperactive Member
    Join Date
    Feb 2005
    Location
    Wollongong. NSW. Australia
    Posts
    470

    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

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    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

  9. #9
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    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.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    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

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    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

  12. #12
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    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:
    1. Private Sub Command1_Click()
    2.   Dim power As Single
    3.   power = Val(InputBox("Enter value for power"))
    4.   Select Case power * 1000
    5.       Case 1
    6.           Debug.Print "0.001"
    7.       Case 1000
    8.           Debug.Print "1"
    9.       Case 2000
    10.           Debug.Print "2"
    11.       Case 20001
    12.           Debug.Print "20.001"
    13.       Case 40000
    14.           Debug.Print "40"
    15.       Case 40001
    16.           Debug.Print "40.001"
    17.       Case 60000
    18.           Debug.Print "60"
    19.       Case 60001
    20.           Debug.Print "60.001"
    21.   End Select
    22. End Sub
    This world is not my home. I'm just passing through.

  13. #13
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    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:
    1. Private Sub Command1_Click()
    2.     CheckValue 0.001
    3.     CheckValue 0.002
    4.     CheckValue 0.003
    5. End Sub
    6.  
    7. Private Sub CheckValue(ByVal x As Double)
    8.     Select Case x
    9.     Case 0.001
    10.         MsgBox "You passed 1E-3"
    11.     Case 0.002
    12.         MsgBox "You passed 2E-3"
    13.     Case Else
    14.         MsgBox "You passed " & CStr(x)
    15.     End Select
    16. End Sub
    Where are your values coming from, can you be sure of their precision?

  14. #14
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: select case with non integer numbers

    Quote 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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    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.

  16. #16
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    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.

  18. #18
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

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

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  19. #19

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    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?

  20. #20
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  21. #21
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: select case with non integer numbers

    Quote 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:
    1. Select Case power * 1000
    2.     Case 1  'power = 0.001
    3.         ' Statements
    4.     Case 1000 'power = 1
    5.         ' Statements
    6.     Case 2000 'power = 2
    7.         ' Statements
    8.     Case 20001 'power = 20.001
    Yes you can. This code works.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     SelectCase 0.01
    5.     SelectCase 0.02
    6.     SelectCase 1
    7.     SelectCase 2
    8. End Sub
    9.  
    10. Private Sub SelectCase(dblNumber As Double)
    11.     Select Case dblNumber
    12.         Case 0.01
    13.             Debug.Print "0.01"
    14.         Case 0.02
    15.             Debug.Print "0.02"
    16.         Case 1
    17.             Debug.Print "1"
    18.         Case 2
    19.             Debug.Print "2"
    20.     End Select
    21. 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
  •  



Click Here to Expand Forum to Full Width