Results 1 to 5 of 5

Thread: CDbl? What is it?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    14

    CDbl? What is it?

    Hi, I'm trying to create an inputbox for uiJob1TextBox, uiJob2TextBox, and uiJob3TextBox. This way the user will be asked to enter a number for each textbox. Now I have my code:

    VB Code:
    1. Const RaisePercentage As String = "Raise Percentage: "
    2.         Dim job1HP As Double, job2HP As Double, job3HP As Double
    3.         Dim job1NewHP As Double, job2NewHP As Double, job3NewHP As Double
    4.         Dim raisePercent As Double
    5.         Try
    6.             job1HP = CDbl(uiJob1TextBox.Text)
    7.             job2HP = CDbl(uiJob2TextBox.Text)
    8.             job3HP = CDbl(uiJob3TextBox.Text)
    9.         Catch ex As Exception
    10.             MsgBox(ex.Message(), MsgBoxStyle.Critical)
    11.             uiJob1TextBox.Focus()
    12.             Exit Sub
    13.         End Try
    14.         Do
    15.             Try
    16.                 raisePercent = CDbl(InputBox("Please enter the raise percentage.", "Raise Percentage")) / 100
    17.                 Exit Do
    18.             Catch ex As Exception
    19.                 MsgBox(ex.Message(), MsgBoxStyle.Critical)
    20.             End Try
    I'm trying to understand what CDbl is? Because I'm thinking if i use the 'Try' statement and make job1HP equal to something else that contains in (uiJob1Textbox)
    That then I can create a raisePercent= "blah" code which will allow me to enter a InputBox for the three textboxes. Anyone have any suggestions or what
    CDbl means and if I can use a different expression so this way I can have expression A equal uiJobTextbox, expression B equal textbox2, and expression C equal textbox3.

    Thanks!

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: CDbl? What is it?

    CDbl() = Convert.ToDouble()
    In this case it's ok as your variables are declared as Double

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    14

    Re: CDbl? What is it?

    Oh Ok, So any idea how I Could create an inputbox message allowing the user to enter data into each of the three textboxes separately?

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    14

    Re: CDbl? What is it?

    I think I'm close, I'm trying to enter
    VB Code:
    1. Try
    2.             job1HP = CDbl(uiJob1TextBox.Text)
    3.  
    4.         Catch ex As Exception
    5.             MsgBox(ex.Message(), MsgBoxStyle.Critical)
    6.             uiJob1TextBox.Focus()
    7.             Exit Sub
    8.         End Try
    9.         Do
    10.             Try
    11.                 raisePercent = CDbl(InputBox("Please enter the raise percentage for Job 1.", "Raise Percentage")) / 100
    12.                 Exit Do
    13.             Catch ex As Exception
    14.                 MsgBox(ex.Message(), MsgBoxStyle.Critical)
    15.             End Try
    As separate "Try-Catch" statements. Will this work? It looks like it could.

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

    Re: CDbl? What is it?

    You shouldn't be using CDbl like that. It is bad practice to just force a conversion and then clean up the mess if it fails. You should only use CDbl if you know for a fact that the value is, or can be converted to, a Double. You should be using Double.TryParse as it does not throw an exception if the converson fails, e.g.
    VB Code:
    1. Dim myDouble As Double
    2.  
    3.         If Double.TryParse(myString, Globalization.NumberStyles.Float, Nothing, myDouble) Then
    4.             'The conversion was successful and the value was stored in myDouble.
    5.         Else
    6.             'The conversiuon failed.
    7.         End If
    If you're using 2005 (which you haven't specified, please use the radio buttons provided when starting threads in future) you can omit the second and third parameters.
    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

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