Results 1 to 4 of 4

Thread: Simple Cast not working

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Simple Cast not working

    I cannot figure out what I am doing wrong. This is a very simple cast:
    vb Code:
    1. 'sng casting test
    2.         Dim varFF As Single = CSng(tbCalcFF.Text) - CSng(tbGoalFF.Text)
    3.         If varFF < 0 Then
    4.             tbCalcFF.BackColor = Color.Gold
    5.         Else
    6.             tbCalcFF.BackColor = Color.LightGoldenrodYellow
    7.         End If
    I am getting this error: Conversion from string "" to type 'Single' is not valid.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Simple Cast not working

    Thats because either tbCalcFF.Text or tbGoalFF.Text is equal to "", you cant turn that into a valid single value.
    What you should do is use Single.TryParse, its handy whenever you are unsure if the string can be converted or not.

    VB Code:
    1. Dim value1 As Single
    2.         Dim value2 As Single
    3.         If Single.TryParse(tbCalcFF.Text, value1) AndAlso Single.TryParse(tbGoalFF.Text, value2) THen
    4.             Dim varFF As Single = value1 - value2
    5.             If varFF < 0 Then
    6.                 tbCalcFF.BackColor = Color.Gold
    7.             Else
    8.                 tbCalcFF.BackColor = Color.LightGoldenrodYellow
    9.             End If
    10.         Else
    11.             MessageBox.Show("Enter valid single values!")
    12.         End If
    Last edited by Atheist; Aug 20th, 2007 at 06:48 PM.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Simple Cast not working

    Another thing you might try, along the same lines, would be to split that one If statement into two, because if the return from either one is false, you could simply put a 0 into that value on the assumption that an empty string is basically 0 (or some other default value if something else makes sense).

    Come to think of it, that's kind of obvious, but sometimes we try to make nice neat, tight, code, and sacrifice useability.
    My usual boring signature: Nothing

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Simple Cast not working

    Quote Originally Posted by Shaggy Hiker
    Another thing you might try, along the same lines, would be to split that one If statement into two, because if the return from either one is false, you could simply put a 0 into that value on the assumption that an empty string is basically 0 (or some other default value if something else makes sense).

    Come to think of it, that's kind of obvious, but sometimes we try to make nice neat, tight, code, and sacrifice useability.
    Yeah good thinking. Then I suppose this would work aswell.

    VB Code:
    1. Dim value1 As Single
    2.         Dim value2 As Single
    3.         Single.TryParse(tbCalcFF.Text, value1)
    4.         Single.TryParse(tbGoalFF.Text, value2)
    5.         Dim varFF As Single = value1 - value2
    6.         If varFF < 0 Then
    7.             tbCalcFF.BackColor = Color.Gold
    8.         Else
    9.             tbCalcFF.BackColor = Color.LightGoldenrodYellow
    10.         End If
    Because value1 and value2 will keep their default values of 0 if TryParse fails. However, I suppose that using an IF statement to check wether the textboxes are empty or not as you mentioned would be more efficient.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

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