The problem:
Create a class named Square that contains fi elds for area and
the length of a side and whose constructor requires a parameter
for the length of one side of a Square. Th e constructor
assigns its parameter to the length of the Square's side fi eld
and calls a private method that computes the area fi eld. Also
include read-only properties to get a Square's side and area.
Create a class named DemoSquares that instantiates an array
of ten Square objects with sides that have values of 1 through
10. Display the values for each Square
This is what I have done:
class Square
{
public double area;
public double sideLength;
Square(double length)
{
sideLength = length;
return;
}
public double SquareSide
{
get { return sideLength; }
}
public double SquareArea
{
get { return area; }
}
private static void AreaMethod()
{
}
I cant figure out how to call private method... How can they ask questions when they never taught how to solve!
Loading
VulpesPosted Feb 2, 2012, 5:45 AM
However, a private method will only be accessible within the class in which it is defined. This means that:
1. In the case of a private instance method, there's no need to precede it with an object reference - 'this' is assumed.
2. In the case of a private static method, there's no need to precede it with the name of a class - the current class name is assumed.
However, in both cases, you can optionally specify 'this' or the current class name, when calling the private method, if you wish.
Jaganathan BantheswaranPosted Feb 2, 2012, 2:04 AM
What is the difficulties you found while call the private method.
The below lines of code is working fine,
namespace CSharpTestConsoleApp
{
class Program
{
static void Main(string[] args)
{
Square f = new Square(10);
}
}
public class Square
{
public double area;
public double sideLength;
public Square(double length)
{
sideLength = length;
AreaMethod();
}
public double SquareSide
{
get { return sideLength; }
}
public double SquareArea
{
get { return area; }
}
private static void AreaMethod()
{
// do some stuff here
}
}
}