Wrox Programmer Forums
|
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
 
Old July 12th, 2011, 02:17 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?
 
Old July 12th, 2011, 05:58 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

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.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old July 12th, 2011, 06:51 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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;
 
Old July 13th, 2011, 01:28 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

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.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old July 13th, 2011, 01:13 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old July 13th, 2011, 02:08 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to add 6 percent to a number? keyvanjan Classic ASP Basics 2 October 10th, 2007 11:31 AM
Wont Give me a Percent Odifius Access VBA 2 September 21st, 2006 12:24 PM
percent of subtotal mtlpp Excel VBA 0 May 14th, 2005 10:09 AM
Can't Get Percent Format to Work Properly twsinc Access VBA 2 November 19th, 2004 09:15 AM
calc col from one table attached to another Jackie SQL Language 3 June 29th, 2003 02:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.