jawed jawed - 3 months ago 22
ASP.NET (C#) Question

Calling methods from one class to another?

I have two classes one contain data and behavior. another is just for inputting and displaying of data when I call the methods from one class to another for displaying of the result it returns zero any help...thanks
class CalculateArea
{
private const double PI = 3.14;
private double cirArea;
private double recArea;
private double cilArea;
private double radius;
private double length, width;
private int height;

// Constructors
public CalculateArea(double radius1)
{
radius = radius1;
}
public double CalculateCircleArea()
{
cirArea = (PI * (radius * radius));
return cirArea;
}

public CalculateArea(int height1)
{
height = height1;
}

public CalculateArea(double lenght1 , double width1)
{
length = lenght1;
width = width1;

}

//public CalculateArea()
//{
// TODO: Complete member initialization
//}

// methods


//
public double CalculateRectangleArea()
{
recArea = (length * width);
return recArea;
}
//
public double CalculateCylinderArea()
{
cilArea = (PI * (radius * radius) * height);
return cilArea;
}
}


//
class Program
{
static void Main(string[] args)
{
string number;
Console.WriteLine("Which figure do you want to see calculated select a number");
Console.WriteLine("Circle : 1");
Console.WriteLine("Rectangle : 2");
Console.WriteLine("Cylinder : 3");

number= Console.ReadLine();
int numb=Convert.ToInt32(number);

if (numb == 1)
{
Console.WriteLine("Enter the radius for circle");
string rad;
rad = Console.ReadLine();
int radiusX = Convert.ToInt32(rad);
CalculateArea newCal1 = new CalculateArea(radiusX);
Console.WriteLine("The result of calculation is {0}", newCal1.CalculateCircleArea());
}

else if(numb == 2)
{
Console.WriteLine("Enter the length ");
number = Console.ReadLine();
double lenght;
lenght = Convert.ToDouble(number);
Console.WriteLine("Enter the width ");
number = Console.ReadLine();
double width;
width = Convert.ToDouble(number);
CalculateArea newCal2=new CalculateArea(lenght , width);
Console.WriteLine("The result of calculation is {0}" , newCal2.CalculateRectangleArea());
}

else if (numb == 3)
{
Console.WriteLine("Enter the height ");
number = Console.ReadLine();
int height;
height = Convert.ToInt32(number);
Console.WriteLine("Enter the radius");
number = Console.ReadLine();
int radius;
radius = Convert.ToInt32(number);
CalculateArea newCal3 = new CalculateArea(height, radius);
Console.WriteLine("The reslut of calculation is {0}", newCal3.CalculateCylinderArea());
}

else
{
Console.WriteLine("There is no any calculation number check the information and try again");
}

Console.ReadLine();

}

Answer

The following code:

CalculateArea newCal1 = new CalculateArea(radiusX);

Is calling the following constructor:

public CalculateArea(int height1) 
{
    height = height1;
}

Because radiusX is an INTEGER...

So, the value is stored in height, not in radius. Thus, when you call the method to calculate the area of the circle, it computes using radius, that is still 0.0.

The CalculateCylinderArea is using also radius and height, but your constructor is taking only two parameters, height and width. So, in method 3, it also "fails", as radius and width is 0.0

The computing of the rectangle works.

To solve the problem with the circle, you need to change the raiusX variable to double.

To solve the problem with the cylinder, for example, you could change the routine to use length and width instead of radius and height.

But as a side note, there are still better ways to do what you are trying to do :)