Results 1 to 3 of 3

Thread: [RESOLVED] [2005] Conversion from string to type Double

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Resolved [RESOLVED] [2005] Conversion from string to type Double

    Is it possible to convert the String from the text box to type Double?

    Code:
    price = CType(txtPrice.Text, Double)
    This code does'nt work when the txtPrice.Text is empty. Sometimes I need to leave this txtPrice.Text as blank. If it is not possible to leave the said textbox as blank just let me know!

    Thanks in Advance!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Conversion from string to type Double

    It's possible to convert a valid string representation of a number to a Double but if the string doesn't represent a number then what conversion could be made? If there's a chance that the string may be invalid then use the TryParse method the type you're converting to:
    vb Code:
    1. Dim price As Decimal
    2.  
    3. If Decimal.TryParse(txtPrice.Text, price) Then
    4.     'The conversion was successful and the "price" variable contains the number.
    5. Else
    6.     'The conversion failed.
    7. End If
    Note that I used Decimal instead of Double. It will work with Double too but it is generally preferable to use Decimal for currency values as there is no risk of round-off errors.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Re: [RESOLVED] [2005] Conversion from string to type Double

    Thanks jmcilhinney!

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