Click here to Skip to main content
16,016,613 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I have simple app c# I want to do simple math operation by input values textboxes . but because of may need to calculate two operation SIMULTANEOUS then I use 6 textbox in my form some when need calculate only two values and some when 3 or 4.
this my code :

VB
Decimal total;
            Decimal INDICATOR;
            Decimal result;
            total = ((Decimal.Parse(txtyear_price1.Text)) / (Decimal.Parse(txtindex1.Text))) + ((Decimal.Parse(txtyear_price2.Text)) / (Decimal.Parse(txtindex2.Text)));
            INDICATOR = total * (Decimal.Parse(txtnewindex.Text));
            result = (Decimal.Parse(txtnewprice.Text)) - INDICATOR;
            label1.Text = result.ToString("N", CultureInfo.CreateSpecificCulture("es-ES"));


or


private void button1_Click(object sender, EventArgs e)
       {


           Decimal txt1;
           Decimal txt2;
           Decimal txt3;
           Decimal txt4;

           Decimal.TryParse(txtyear_price1.Text.Trim(), out txt1);
           Decimal.TryParse(txtyear_price2.Text.Trim(), out txt2);
           Decimal.TryParse(txtindex1.Text.Trim(), out txt3);
           Decimal.TryParse(txtindex2.Text.Trim(), out txt4);

           Decimal total;
           Decimal INDICATOR;
           Decimal result;
           total = (txt1/txt3) + (txt2/txt4);
           INDICATOR = total * (Decimal.Parse(txtnewindex.Text));
           result = (Decimal.Parse(txtnewprice.Text)) - INDICATOR;
           label1.Text = result.ToString("N", CultureInfo.CreateSpecificCulture("es-ES"));
       }


my problem is that when I fill all of textboxes it work but when give up two of them the error :
Input string was not in a correct format

and the error for second code is :
Attempted to divide by zero.

please help me
It is OBVIOUS that I am beginner .
Posted
Updated 16-Oct-15 7:47am
v3
Comments
PIEBALDconsult 16-Oct-15 13:45pm    
Don't use a TextBox when you want a number.
BillWoodruff 17-Oct-15 2:44am    
What exactly do you mean by "when I give up two of them" ?

Since you are a beginner, I will give you a solution for both of these problems. Yes, a solution. :-) Read the answer for that!

1) First of all,

Exception:
Input string was not in a correct format

Means that the string that you are providing to parse to a decimal, is not in a correct format. Simple english applied and within a fraction of second anyone can understand that the code is complaining about the format. Are you having "," in it? Many other similar problems arise, when you cannot convert the string to a decimal value. If your locale (culture) supports to have 123,45 in a decimal format then make sure you are using the culture that supports such format, and not the default ones. Just asking, this may be a trouble, otherwise debug it to find out more. This MSDN documentation would help you in understanding the decimal formats and how you should consider a decimal value to look like (in string of course): https://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx[^]

2) Secondly, the other problem is because you are trying to divide the number by a zero. If you ever studied mathematics (I am not claiming, you never did), you do know that something divided by zero is undefined. In programming, something divided by zero is "DivideByZeroException[^]". So, you need to make sure you do not divide anything by zero. If you are about to, you can change the flow of program.

C#
// Hypothetical variables and case
int a, b, c;
// Get the values

if(b != 0) {
   c = a / b;
} else {
   // DivideByZero would be raised
}


This way, you can minimize that error too. Now, I know I didn't provide the solution, but using this general solution you can solve the problem and any problem similar that may arise again.

Now, open Visual Studio, add a breakpoint and debug the application[^] to find out what is going wrong and what may be expected or done to make it work fine.
 
Share this answer
 
Comments
BillWoodruff 17-Oct-15 2:44am    
"Giving" a beginner a solution is not educating a beginner to enable them to find their own solutions. The error the user reports is not a divide-by-zero error.
Afzaal Ahmad Zeeshan 17-Oct-15 10:53am    
I did not provide him with a solution, the first paragraph was just a "pun". :-) I didn't give him a solution, instead I tried to explain those problems as much as I can.

And, the error indeed is not an exception for DivideByZero, but generally would be the same, because he is trying to divide by zero. For this, a same thread of StackOverflow can be found at: http://stackoverflow.com/questions/15377666/how-to-solve-attempted-to-divide-by-zero-in-c. That is just how TextBox wraps everything.
1. learn to use 'Decimal.TryParse() : the way you use it now: you are not checking if the result is 'true, or 'false ... that's how you avoid errors: [^].

2. Consider making the TextBox Controls you use accept only Decimal entry. CodeProject is your friend: this article shows a variety of ways you could go about that: [^].
 
Share this answer
 
It's true, of course you can do that, but you can inspire you about this.
 
Share this answer
 
v2
Please use Decimal.TryParse() method. This will help avoid exceptions.
 
Share this answer
 
Comments
rezaeti 16-Oct-15 13:25pm    
can you say how to use tryparse in here?
rezaeti 16-Oct-15 13:25pm    
I cant do that
BillWoodruff 17-Oct-15 2:43am    
Of course you can do that ! Just look up the documentation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900