 |
| C# 4.0 aka C# 2010 General Discussion Discussions about the C# 4.0, C# 4, Visual C# 2010 language and tool not related to any specific Wrox book |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# 4.0 aka C# 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 12th, 2011, 02:17 PM
|
|
Authorized User
|
|
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Percent calc.
I am trying to build a form that contains several text boxes and buttons that will increment results, then calculate the percent of the total. As long as there are values in the boxes it works, but I suspect that when the box is empty, C# is trying to divide by nothing, and I'm getting an Input string not in correct format. Here are the percent calc. lines.
private void percentbutton_Click(object sender, EventArgs e)
{
float pers = 200;
float percen = 100;
float segt = float.Parse(segtextBox.Text, NumberStyles.Any);
float pseg = segt / pers * percen;
persegtextBox.Text = pseg.ToString();
float lympdt = float.Parse(lymphtextBox.Text, NumberStyles.Any);
float plymph = lympdt / pers * percen;
perlymphtextBox.Text = plymph.ToString();
float bandt = float.Parse(bandstextBox.Text, NumberStyles.Any);
float pband = bandt / pers * percen;
perbandstextBox.Text = pband.ToString();
I've tried adding
if segt > 0.0, and enclosing the cal. in {}, but that doesn't seem to work. What would the best way to approach this be?
|
|

July 12th, 2011, 05:58 PM
|
|
Wrox Author
|
|
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
|
|
the float.Parse method you are using throws a FormatException if the input cannot be converted to a float. See the MSDN documentation for Single.Parse. You can catch the exception. Instead of using float.Parse, you can use float.TryParse. This method returns a boolean value if the conversion succeeds or not.
If the conversion to float succeeds, and you do a divide by 0, an exception of type DivideByZeroException is thrown. You can catch this as well.
Hope this helps.
|
|

July 12th, 2011, 06:51 PM
|
|
Authorized User
|
|
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Christian, thanks a bunch for taking the time to look at this. I have this so far, and Visual Studio is erroring because the method doesn't have a return value. Is this kind of what you were referring to?
private percentbutton_Click(object sender, EventArgs e)
{
float pers = 200;
float percen = 100;
try
{
float segt = float.Parse(segtextBox.Text, NumberStyles.Any);
if (segt < 0.0)
return false;
}
catch (Exception ex)
{
return false;
}
try
{
float pseg = segt / pers * percen;
persegtextBox.Text = pseg.ToString();
}
catch (Exception ex)
{
return false;
}
return true;
|
|

July 13th, 2011, 01:28 AM
|
|
Wrox Author
|
|
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
|
|
Mark, there are several issues with your code.
The method percentbutton_Click must be declared to return void, as the EventHandler delegate of the Click event defines:
Code:
private void percentbutton_Click(object sender, EventArgs e)
Then you cannot return bool from the method - return true/false. Instead of returning true/false you can inform the user, e.g. by showing a message box - MessageBox.Show(), or by setting UI elements.
Single.TryParse can be used like this:
Code:
private void percentbutton_Click(object sender, EventArgs e)
{
float pers = 200;
float percen = 100;
float segt;
if (!float.TryParse(segtextBox.Text, NumberStyles.Any, null, out segt))
{
MessageBox.Show("not a number");
}
//...
Hope this helps.
|
|

July 13th, 2011, 01:13 PM
|
|
Authorized User
|
|
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Christian, thanks again for your input. Of the twelve text boxes that the user can enter data into, some will be blank, what I want to do is enter some code so that C# skips over the blank ones when the percent button is clicked.
|
|

July 13th, 2011, 02:08 PM
|
|
Authorized User
|
|
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I figured it out. I removed the ! from the if statement, and put the percent calc. lines in a bracket below it. I have a couple more things to code, but your assistance got me heading in the right direction. Many thanks.
|
|
 |