I need help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: I need help

  1. #1
    Join Date
    Mar 2012
    Posts
    5

    I need help

    I'm trying to multiply a numericUpDown input.Here is my code so far:

    double pi = Math.PI;
    decimal number1 = numericUpDown1.Value;
    int RadiusToCircumference = number1 * 2 * pi;
    MessageBox.Show("The circumference is" + RadiusToCircumference);

    Dont think im stupid that i didnt figure this out yet. just started programming yesterday.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: I need help

    No one thinks you are stupid, but you need to tell us exactly what is not working. What is wrong exactly? The only problem I see is this line:

    Code:
    int RadiusToCircumference = number1 * 2 * pi;
    So that will truncate the result to an integer. Perhaps you wanted it that way, but I don't know because you didn't explain what the actual problem is.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  3. #3
    Join Date
    Mar 2012
    Location
    Nigeria
    Posts
    15

    Post Re: I need help

    I think the problem with your code is in the statement below:

    int RadiusToCircumference = number1 * 2 * pi;

    There are rules for working and converting between C# types. See my recommendation below:

    double pi = Math.PI;
    decimal number1 = Convert.ToInt32(textBox1.Text);
    double RadiusToCircumference = (double)number1 * 2 * pi;
    MessageBox.Show("The circumference is " + RadiusToCircumference);

    These code should work based on these assumptions:

    - For your own code I have assumed you are using C# 4.0 so I've done same.

    - Here's what I think, if these varaible numericUpDown1.Value is of type double then that line of code should not be a problem else you'd need to explicitly cast(i.e. force a conversion from whatever type it is to type decimal) to do so just add (decimal) in front of that variable. Because I
    have little idea about your numericUpDown1.Value, I initialized number1 variable from a textbox input.


    As for the statement:
    int RadiusToCircumference = number1 * 2 * pi;
    observed that I've forced a conversion using a cast operator i.e. (double).

    ***********I hope these makes a good contribution to your understanding of C#***********
    ***********You can download my own version of your code code. See attachment.**********
    Attached Files Attached Files

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: I need help

    It's best (numerically) to delay casting as late as possible:

    [double]
    double circum = (double)(radius * 2) * Math.Pi;
    [/double]

    In this case, it makes no difference, but it might if you were doing something more complicated.

    Actually, an explicit cast is not required here. int * double will be automatically promoted to double.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Mar 2012
    Posts
    5

    Re: I need help

    Quote Originally Posted by chillaxzino View Post
    I think the problem with your code is in the statement below:

    int RadiusToCircumference = number1 * 2 * pi;

    There are rules for working and converting between C# types. See my recommendation below:

    double pi = Math.PI;
    decimal number1 = Convert.ToInt32(textBox1.Text);
    double RadiusToCircumference = (double)number1 * 2 * pi;
    MessageBox.Show("The circumference is " + RadiusToCircumference);

    These code should work based on these assumptions:

    - For your own code I have assumed you are using C# 4.0 so I've done same.

    - Here's what I think, if these varaible numericUpDown1.Value is of type double then that line of code should not be a problem else you'd need to explicitly cast(i.e. force a conversion from whatever type it is to type decimal) to do so just add (decimal) in front of that variable. Because I
    have little idea about your numericUpDown1.Value, I initialized number1 variable from a textbox input.


    As for the statement:
    int RadiusToCircumference = number1 * 2 * pi;
    observed that I've forced a conversion using a cast operator i.e. (double).

    ***********I hope these makes a good contribution to your understanding of C#***********
    ***********You can download my own version of your code code. See attachment.**********
    Thanks for the help. I will test it out tonight.

  6. #6
    Join Date
    Mar 2012
    Posts
    5

    Re: I need help

    the problem has something to do with not being able to multiply a decimal variable. And numericUpDowns cannot be int.

    by the way.I use visual c# express to build the program.
    Last edited by yoda493; March 15th, 2012 at 11:05 AM.

  7. #7
    Join Date
    Mar 2012
    Location
    Nigeria
    Posts
    15

    Question Re: I need help

    Hasn't your code worked yet?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center