PDA

Click to See Complete Forum and Search --> : Creating Classes (RESOLVED)


JAtkinson
May 8th, 2004, 08:04 PM
Why does VS.net let you use System.Math.Abs(double) without creating a new instance of it (System.Math math = new System.Math)? Ive been trying to make my own class that will find the area of circle, but it makes me create a new object first (ADGMathPro.Circle circle = new ADGMathPro.Circle();

What I was it to be able to do this....

ADGMathPro.Circle.Area(34);
instead of
circle.Area(34);


namespace ADGMathPro
{
/// <summary>
/// Summary description for Circle.
/// </summary>

public class Circle
{

const double pi = System.Math.PI;

public double Area(double radius)
{
double area;
area = pi * (radius * radius);
return area;
}

public double Circumference(double radius)
{
double circumference;
circumference = 2 * pi * radius;
return circumference;
}

}
}


What am I doing wrong??

Thanks

axion_sa
May 9th, 2004, 01:42 AM
For those looking for an answer, it's a static method, which doesn't require an instance of the class to be created before it may be called; but it cannot access any non-static methods or properties of the containing class:

public MyClass
{
private string _myStringVariable = String.Empty; // This cannot be accessed by "MyMethod".

public static void MyMethod()
{
}
}


And to call the method:

MyClass.MyMethod();