Results 1 to 11 of 11

Thread: [RESOLVED] Multiplying Strings

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Resolved [RESOLVED] Multiplying Strings

    I have two textboxes, I want to multiply their values.

    In VB = Val(Text1.Text) * Val(Text2.Text)

    What is it in C#?

    Also, I don't know my version but I'm using 2008.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Multiplying Strings

    Try this
    Code:
    int Text1Value = Convert.ToInt32(TextBox1.Text);
    int Text2Value = Convert.ToInt32(TextBox2.Text);
    intResult = Text1Value * Text2Value

  3. #3
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: Multiplying Strings

    int.Parse or double.Parse should do it.

  4. #4
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: Multiplying Strings

    If you need a one-liner...
    C# Code:
    1. double endVal = double.Parse(textBox1.Text) * double.Parse(textBox2.Text);

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

    Re: Multiplying Strings

    double.TryParse is more suitable when dealing with user input.
    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)

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Multiplying Strings

    Quote Originally Posted by timeshifter
    If you need a one-liner...
    C# Code:
    1. double endVal = double.Parse(textBox1.Text) * double.Parse(textBox2.Text);
    Thanks - What I was looking for.

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

    Re: Multiplying Strings

    Quote Originally Posted by Zach_VB6
    Thanks - What I was looking for.
    Try entering a non-numerical value into one of those textboxes
    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)

  8. #8
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: [RESOLVED] Multiplying Strings

    Yes, anything but numbers and decimals will break it. The assumption was a controlled entry screen. If the user is dumb enough to enter "seven" into a text box labeled as "Money", then they deserve to have an app blow up in their face.

    [/personal opinion]

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

    Re: [RESOLVED] Multiplying Strings

    Quote Originally Posted by timeshifter
    Yes, anything but numbers and decimals will break it. The assumption was a controlled entry screen. If the user is dumb enough to enter "seven" into a text box labeled as "Money", then they deserve to have an app blow up in their face.

    [/personal opinion]
    If someone showed me an application that crashed for incorrect input, I'd never touch that application again.
    Last edited by Atheist; Jan 28th, 2008 at 07:22 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)

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

    Re: [RESOLVED] Multiplying Strings

    In a homework situation you can assume perfect input if the instructor says you can. In a real world situation you can never assume that the user will not enter invalid data. If a user enters "seven" instead of "7" then they're an idiot but I don't think the app crashing for that is appropriate. Also, I'm sure even timeshifter has hit the wrong key accidentally every now and again.

    In VB the Val function will not throw an exception if the input is invalid. It just keeps reading digits until there are no more or it meets an invalid character. For that reason Val is bad because it may appear to work but use the wrong data. If data is entered that is obviously wrong then the user should be notified, not the data be ignored. For instance, if the user entered "12w34" then Val would return 12.0, which is obviously not what the user intended. You should be using TryParse in VB and C#:
    CSharp Code:
    1. double num1;
    2. double num2;
    3.  
    4. if (double.TryParse(this.textBox1.Text, num1) && double.TryParse(this.textBox2.Text, num2))
    5. {
    6.     MessageBox.Show((num1 * num2).ToString(), "Product");
    7. }
    8. else
    9. {
    10.     MessageBox.Show("Please enter valid numbers only.", "Invalid Input");
    11. }
    You should only use Convert.ToInt32 or Int32.Parse when you know for sure that the data is valid. You should almost never use Val.
    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

  11. #11
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: [RESOLVED] Multiplying Strings

    Hear hear. Exactly as it should be done.

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