|
-
Jan 28th, 2008, 01:15 PM
#1
Thread Starter
Frenzied Member
[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.
-
Jan 28th, 2008, 01:20 PM
#2
Re: Multiplying Strings
Try this
Code:
int Text1Value = Convert.ToInt32(TextBox1.Text);
int Text2Value = Convert.ToInt32(TextBox2.Text);
intResult = Text1Value * Text2Value
-
Jan 28th, 2008, 01:20 PM
#3
Re: Multiplying Strings
int.Parse or double.Parse should do it.
-
Jan 28th, 2008, 01:25 PM
#4
Re: Multiplying Strings
If you need a one-liner...
C# Code:
double endVal = double.Parse(textBox1.Text) * double.Parse(textBox2.Text);
-
Jan 28th, 2008, 02:15 PM
#5
Re: Multiplying Strings
double.TryParse is more suitable when dealing with user input.
-
Jan 28th, 2008, 02:27 PM
#6
Thread Starter
Frenzied Member
Re: Multiplying Strings
 Originally Posted by timeshifter
If you need a one-liner...
C# Code:
double endVal = double.Parse(textBox1.Text) * double.Parse(textBox2.Text);
Thanks - What I was looking for.
-
Jan 28th, 2008, 02:30 PM
#7
Re: Multiplying Strings
 Originally Posted by Zach_VB6
Thanks - What I was looking for.
Try entering a non-numerical value into one of those textboxes
-
Jan 28th, 2008, 03:21 PM
#8
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]
-
Jan 28th, 2008, 03:55 PM
#9
Re: [RESOLVED] Multiplying Strings
 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.
-
Jan 28th, 2008, 06:10 PM
#10
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:
double num1; double num2; if (double.TryParse(this.textBox1.Text, num1) && double.TryParse(this.textBox2.Text, num2)) { MessageBox.Show((num1 * num2).ToString(), "Product"); } else { MessageBox.Show("Please enter valid numbers only.", "Invalid Input"); }
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.
-
Jan 28th, 2008, 07:53 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|