1

I am having some trouble when calling a method from class A in class B.

Here is the method being called in class B

 public double monthlyExpense
        {
            get
            {
                return RealEstateApp.GetExpenses();
            }

The method itself from class A is

public static double GetExpenses()
{
    double insurance;
    double taxes;
    double utilities;
    string inValue;

    inValue = _insurance;
    insurance = double.Parse(inValue);
    inValue = _tax;
    taxes = double.Parse(inValue);
    inValue = _utilities;
    utilities = double.Parse(inValue);
    return (insurance / 12 + taxes / 12 + utilities);

}

I get the exception a the insurance = double.Parse(inValue); line.

The _insurance etc variables are being read from various text boxes:

RealEstateApp._insurance = txtBoxInsurance.Text;
RealEstateApp._tax = txtBoxTax.Text;
RealEstateApp._utilities = txtBoxUtilities.Text;

Any help would be hugely appreciated.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Matt
  • 75
  • 1
  • 3
  • 8

2 Answers2

0

You might have inserted wrong input like space or any other character in txtBoxInsurancetextbox firstly validate that textbox for double value then you will not get that exception.

Mohini Mhetre
  • 912
  • 10
  • 29
-1

You can use TryParse instead of parse method

 inValue = _insurance;
double.TryParse(inValue,out insurance);//this will assign double value to insurance variable 
inValue = _tax;
double.TryParse(inValue,out taxes);

This will try to parse string value to double value and return true or false if it success or fail.

Manish Parakhiya
  • 3,732
  • 3
  • 22
  • 25