Marcus Marcus - 1 year ago 133
C# Question

How is this parameter changing its value?

int temp;
res = int.TryParse(discountTextBox.Text, out temp);

if (res == false)
MessageBox.Show("Enter Discount in integers", "Error");

double Discount = (double) temp;
if (!transactiondao.ValidateDiscount(curremployee, Discount, retail))
MessageBox.Show("Invalid Discount entered", "error");

Above block of code calls the method below. However, while debugging discount has the value that user specified. After the if block is executed however, it changes to 0.

public bool ValidateDiscount(Employee emp, double GivenDiscount, int retailprice)
bool res = false;
double sMaxDiscount = emp.GetAuthorizedMaxDiscount();

//validating against retail price
double rMaxDiscount;
if (retailprice == 2000)
rMaxDiscount = 4;
else if (retailprice == 3000)
rMaxDiscount = 5;
else if (retailprice == 4000)
rMaxDiscount = 8;
rMaxDiscount = -1;

double MaxDiscount;
if (sMaxDiscount >= rMaxDiscount)
MaxDiscount = sMaxDiscount;
MaxDiscount = rMaxDiscount;

if (GivenDiscount < MaxDiscount)
res = true;

return res;

here is the call to ValidateDiscount. commenting out the if block and thus bypassing validatediscount works perfectly and discount retains the value entered by user. How does this happen?

Answer Source

You declared temp as an int, later you cast temp as a double. This won't give the desired result. Try this.

double temp;
res = double.TryParse(discountTextBox.Text, out temp);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download