|
-
March 18th, 2012, 03:52 PM
#1
Help on Object string representation. Use of the override keyword.
Please review this code and proffer a solution where possible.
Problem: - Error as stated by my compiler is as follows "Cube.ToString(string)': no suitable method found to override"
Please tell me why these override isn't working and how best to make a string representation of my Cube objects.
Code:
class Cube
{
//Declaring instance variables
private double areaResult;// areaResult instance variable to hold a Cube object's area
private double perimeterResult;// perimeterResult instance variable to hold a Cube object's perimeter
//cube name
private string cubeName;
// instance variable "dimensions" array of type double to hold a Cube object's dimensions
private double[] dimensions;
// instance variable "sideNames" array of type double to hold a Cube object's dimensions
private string[] sideNames = { "length", "height", "breath" };
//Declaring properties
//Area property... This property exposes a Cube object's areaResult instance variable
//This property is readonly
public double Area
{
get
{
return areaResult;
}
}
//Declaring properties
//Perimeter property... This property exposes a Cube object's perimeterResult instance variable
//This property is readonly
public double Perimeter
{
get
{
return perimeterResult;
}
}// end of property Perimeter
//Declaring indexers
//These indexers provides access to a Cube object's array instances variables in a fashion way
//Access a Cube object's array instance variable via an integer index
public double this[int dimensionIndex]
{
//To read an indexed value in a Cube object array
get
{
//locate the value in the array
if ((dimensionIndex < 0) || (dimensionIndex >= dimensions.Length))
{
return -1;//if index is invalid return -1
}
else
{
return dimensions[dimensionIndex];//else return the indexed value
}
}
//To set an indexed value in a Cube object array
set
{
//locate the value in the array
if ((dimensionIndex >= 0) && (dimensionIndex > dimensions.Length))
{
dimensions[dimensionIndex] = value; //set it only if it meet the requirment
}
}
}
//Access a Cube object's array instance variable via a string index
public double this[string sideNameIndex]
{
//To read an indexed value in a Cube object array
get
{
//locate the value in the array
int i = 0;
while ((i < dimensions.Length) && (sideNames[i] != sideNameIndex.ToLower()))
{
++i;
}
return (i == sideNames.Length) ? -1 : dimensions[i];
}
//To set an indexed value in a Cube object array
set
{
//locate the value in the array
int i = 0;
while ((i < dimensions.Length) && (sideNames[i] != sideNameIndex.ToLower()))
{
++i;
}
if (i != sideNames.Length)
{
dimensions[i] = value; //set it only if it meet the requirment
}
}
}
//Declaring methods
//SqrArea method computes the area of a Cube object
private void SqrArea()
{
double area = 1.00;// assume area is one
for (int i = 0; i < dimensions.Length; i++)
{
area *= dimensions[i];//multiply through all the dimensions...
}
areaResult = area; // initialise areaResult
}
//SqrPerimeter method computes the perimeter of a Cube object
private void SqrPerimeter()
{
double perimeter = 0.00;// assume area is zero
for (int i = 0; i < dimensions.Length; i++)
{
perimeter += dimensions[i];//add through all the dimensions...
}
perimeterResult = perimeter; // initialise perimeterResult
}
//Declaring constructors... There are two constructors for a Cube object
public Cube(double[] sqrDimensions)
{
dimensions = sqrDimensions;// initialise instance variable dimensions
//Compute the objects area
SqrArea(); // call SqrArea method
SqrPerimeter(); // call SqrPerimeter method
}
public Cube(double[] srqDimensions, string[] sideNames)
{
dimensions = srqDimensions; // initialise instance variable dimensions
//this is used here coz this class instance variable sideNames is hidden
//as a result of using the same name as parameter
this.sideNames = sideNames;// initialise sideNames instance variable
//Compute the objects area
SqrArea(); // call SqrArea method
SqrPerimeter(); // call SqrPerimeter method
}
//String Representation of a cube object
public override string ToString(string name = "")
{
cubeName = name;
return string.Format("{0}", cubeName);
}
}// end of class Cube
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|