joonjang
joonjang

Reputation: 31

Holding different properties of multiple classes in one class

I am doing a problem from a book called "C# 7 and .Net Core: Modern Cross-Platform Development - Second Edition" and the exercises given at the end are not provided solutions.

The problem outline:

Create a console application named Ch10_Exercise02 that creates a list of shapes, uses serialization to save it to the filesystem using XML, and then deserializes it back:

// create a list of Shapes to serialize 
var listOfShapes = new List<Shape> 
{ 
  new Circle { Colour = "Red", Radius = 2.5 }, 
  new Rectangle { Colour = "Blue", Height = 20.0, Width = 10.0 }, 
  new Circle { Colour = "Green", Radius = 8 }, 
  new Circle { Colour = "Purple", Radius = 12.3 }, 
  new Rectangle { Colour = "Blue", Height = 45.0, Width = 18.0  } 
}; 

Shapes should have a read-only property named Area so that, when you deserialize, you can output a list of shapes, including their areas, as shown here:

List<Shape> loadedShapesXml = serializerXml.Deserialize(fileXml)
as List<Shape>; 
foreach (Shape item in loadedShapesXml) 
{ 
  WriteLine($"{item.GetType().Name} is {item.Colour} and has an
  area of {item.Area}"); 
} 

This is what your output should look like when you run the application:

Loading shapes from XML:
Circle is Red and has an area of 19.6349540849362
Rectangle is Blue and has an area of 200
Circle is Green and has an area of 201.061929829747
Circle is Purple and has an area of 475.2915525616
Rectangle is Blue and has an area of 810

I have made a Shape class with Circle and Rectangle class that inherit from it.

class Shape
{
    public readonly double Area;
}

class Circle : Shape
{
    public string Colour { get; set; }
    public double Radius { get; set; }
    Area = Radius* Radius * Math.PI;
}

class Rectangle : Shape
{
    public string Colour { get; set; }
    public double Height { get; set; }
    public double Width { get; set; }
    Area = Width * Height;
}

I am unsure why the inherited Area field is not recognized by my Circle and Rectangle classes. Is it because Circle and Rectangle should somehow be incorporate into just one Shape class?

Edit: This documentation helped me find the answer

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-define-abstract-properties

Upvotes: 0

Views: 759

Answers (2)

RTF
RTF

Reputation: 421

Two problems:

1) You're trying to assign to Area but you've declared it readonly in your base class.

2) The following...

Area = Radius* Radius * Math.PI;

...is a statement but not a declaration.

It needs to go in some sort of method.

The rest of the work is left to the reader. The following merely illustrates placing a statement in a method, which is part of what's at stake.

class Circle : Shape
{
    public string Colour { get; set; }
    public double Radius { get; set; }

    void DoSomething()
    {    
       //Assume Radius has a value at this point...
       Area = Radius * Radius * Math.PI;
    }
 }

Upvotes: 0

Kei
Kei

Reputation: 1026

First, public readonly double Area; is not a property. This is a field. To make a read-only property, you would create a getter-only property like this: public double Area { get; }

Next, I'm pretty sure Area = Radius* Radius * Math.PI; will result in a compiler error. If you need to override the implementation of Area, I'd make Area a virtual property in your parent class, then override it in your subclass as follows:

class Shape
{
    public virtual double Area { get;}
}

class Circle : Shape
{
    public string Colour { get; set; }
    public double Radius { get; set; }
    public override double Area
    {
        get { return Radius * Radius * Math.PI; }
    }
}

Upvotes: 2

Related Questions