5 Replies - 386 Views - Last Post: 02 February 2013 - 05:54 AM Rate Topic: -----

#1 MissBeery  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-January 13

C# Parse question

Posted 01 February 2013 - 09:37 PM

Hello!
I'm having trouble converting input from a text box to a double. I tried two different ways to code and I get errors (invalid arguments) for both ways. I know I'm headed in the right direction.. where am I off? I underlined where I'm getting the errors.

   private void btnVolumeCalculate_Click(object sender, EventArgs e)
        {
        // Convert text input to double data type 
            // Radius Input
            double Radius;
            Radius = [u]int.Parse(txtBoxRadiusInput)[/u];

            // Height Input
            string.[u]Height[/u] = txtBoxHeightInput.Text;
            double HeightNumber = [u]double.Parse(Height)[/u];

        
        // Math calculations to follow



Thank you in advance for any nudges in the right direction!

Newbie to coding,
Bethany :)/>

This post has been edited by tlhIn`toq: 02 February 2013 - 07:27 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: C# Parse question

#2 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,534
  • Joined: 08-April 09

Re: C# Parse question

Posted 02 February 2013 - 02:13 AM

Why don't you try NumericUpDown Control instead of textbox it is intended for numbers, but if you really need to use Convert class and it's ToDouble method

This post has been edited by Michael26: 02 February 2013 - 02:13 AM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3615
  • View blog
  • Posts: 12,438
  • Joined: 12-December 12

Re: C# Parse question

Posted 02 February 2013 - 05:02 AM

double Radius;
Radius = int.Parse(txtBoxRadiusInput.Text); // should be Double.Parse()

// Height Input
//string.Height = txtBoxHeightInput.Text;
// Should be:
string Height;
Height = txtBoxHeightInput.Text;

This post has been edited by andrewsw: 02 February 2013 - 05:52 AM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3615
  • View blog
  • Posts: 12,438
  • Joined: 12-December 12

Re: C# Parse question

Posted 02 February 2013 - 05:13 AM

View PostMichael26, on 02 February 2013 - 02:13 AM, said:

Why don't you try NumericUpDown Control instead of textbox it is intended for numbers, but if you really need to use Convert class and it's ToDouble method

There is little difference between Convert.ToDouble and Double.Parse.

IMO It is best to attempt to answer the OPs question before suggesting an alternative approach (NumericUpDown).

This post has been edited by andrewsw: 02 February 2013 - 05:15 AM

Was This Post Helpful? 3
  • +
  • -

#5 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: C# Parse question

Posted 02 February 2013 - 05:33 AM

Yep, Convert.ToDouble(string) actually uses double.Parse() internally, but adds a null check on the passed in string:

Spoiler


@OP

You are trying to convert the text box to an int on this line:

int.Parse(txtBoxRadiusInput);


rather than the string contents of the text box. Use the .Text property of the text box to access its content. Plus, you should be converting it to a double, rather than an int, as mentioned in post #3.

Finally, I'll just mention as well that whenever you find yourself trying to convert strings input by users into doubles, you should use double.TryParse(), rather than double.Parse(), as TryParse() won't throw an exception on failure. This goes for the other numeric types also.

This post has been edited by CodingSup3rnatur@l-360: 02 February 2013 - 05:45 AM

Was This Post Helpful? 3
  • +
  • -

#6 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3615
  • View blog
  • Posts: 12,438
  • Joined: 12-December 12

Re: C# Parse question

Posted 02 February 2013 - 05:54 AM

I forgot the .Text in my previous code as well (copying the OPs code!), and have amended it from txtBoxRadiusInput to txtBoxRadiusInput.Text.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1