Help on Object string representation. Use of the override keyword.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Help on Object string representation. Use of the override keyword.

  1. #1
    Join Date
    Mar 2012
    Location
    Nigeria
    Posts
    15

    Question 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

  2. #2
    Join Date
    Jan 2010
    Posts
    1,099

    Re: Help on Object string representation. Use of the override keyword.

    Because the Object class (the implicit base class for all C# classes) doesn't contain a method with the signature
    string ToString(string)
    but rather
    string ToString() // note: no parameters

    The compiler is telling you that no inherited method exists with that signature, so it doesn't know what is it that you want to override.

    If you need to override the inherited method, then you should make the cube name a field of the Cube class, and not an external parameter - making it possible for you to leave the parameter list emty.
    If you need the ToString(string) variant, then just remove the override keyword.
    Note that you can have both of them in your class.
    But only the parameterless one will work with things like Console.WriteLine(myCube);

  3. #3
    Join Date
    Mar 2012
    Location
    Nigeria
    Posts
    15

    Lightbulb Re: Help on Object string representation. Use of the override keyword.

    Thanks for that contribution. Its marvellous. Please as a support make a version of my code and include in it these variant you are talking about.

    From my understanding of what you said the parameter I passed has made that signature unidentifiable by the compiler.

    I believe a variant like you said would be:

    Code:
    public string ToString(string name = "")
            {
                cubeName = name;
                return string.Format("{0}", cubeName);
            }
    If I'm correct above: These then means code like the one below won't display a string representation of my Cube Object in the console window.

    Code:
    Console.WriteLine(myCube)// where myCube is the variable referencing my Cube object in space
    Last edited by chillaxzino; March 18th, 2012 at 04:54 PM.

  4. #4
    Join Date
    Jan 2010
    Posts
    1,099

    Re: Help on Object string representation. Use of the override keyword.

    Yes. About the other thing - the WriteLine() method will use the default string representation - which is just to write the fully qualified name of the type. Your class will have both ToString() methods - try it out yourself with your IDE.

    I just noticed you do have a member field for the cube name, so, if you want to override the inherited method, something like this would work:
    Code:
            public override string ToString()
            {
                return string.Format("{0}", cubeName);
            }
    But, you should provide a way for the user of the class to set the cubeName field - as far as I can see, the only place you're setting it is in your ToString(string) method - and that's not really a good place.
    Add a property, like this:
    Code:
    public string Name
    {
        get { return cubeName; }
        set { cubeName = value; }
    }
    
    // then set it somewhere, for example, in Main() , after you construct the Cube:
    Cube myCube = new Cube(sqrDimensions);
    myCube.Name = "super-cool-awesome cube";
    Console.WriteLine(myCube);   // should print the name
    Alternatively, you can set it by passing an additional parameter to a constructor.

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center