10

I have a class in C# like so:

public class BarChart
{
    public BarData BarChartData;
    public BarStyle BarChartStyle;

    public BarChart(BarData data, BarStyle style)
    {
        this.BarChartData = data;
        this.BarChartStyle = style;
    }

    string _uniqueName;
    public string UniqueName
    {
        get { return this._uniqueName; }
        set { this._uniqueName = value; }
    }
    string _rowNumber;
    public string RowNumber
    {
        get { return this._rowNumber; }
        set { this._rowNumber = value; }
    }

I want to create a class called Chart that will have all of the properties that BarChart class has. For example:

Chart someChart = new Chart(BarChart);
string name = someChart.UniqueName

I am relatively new to C# and the concept of inheritance is a little bit foreign to me. At the end of the day I will have multiple different chart types like LineChart, BarChart, etc., but I also want to be able to move them around and sort them like so:

List<Chart> groupedCharts = listOfCharts
.GroupBy(u => u.RowNumber)
.Select(grp => grp.ToList())
.ToList();

...hence the idea to throw them into generic Chart class for easy use with LINQ.

How would I go about setting that up?

1
  • @DarrenYoung maybe not all of them. I guess only the shared ones. For my use I will actually only need RowNumber to be shared at the moment. Commented Nov 13, 2015 at 18:04

3 Answers 3

17

Create an abstract Chart class:

abstract class Chart
{
    // Shared properties
}

Then inherit it:

class BarChart : Chart
{
    // Bar chart properties
}

Then you can create it as:

Chart someChart = new BarChart();
Sign up to request clarification or add additional context in comments.

8 Comments

Why do you believe it needs to be abstract?
Without a type (line, bar, pie), it certainly looks abstract to me, although it's not strictly relevant to the question.
@DavidPine I guess I always start a base class off abstract as a rule until it needs to be concrete (this is always during the design phase, of course -- you'd never want to make this change post-production).
I changed my answer, I would prefer an interface over an abstract class. My personal opinion.
@DavidPine I don't personally like the choice of an interface as it doesn't allow fields and if you specify properties in an interface it forces you to "implement" them which the majority of the times ends up violating DRY
|
10

You need to create an interface like this:

    public interface IChart
    {
        string UniqueName { get; set; }

        string RowNumber { get; set; }
    }

Then have the other classes inherit the base class as such...

    public class BarChart : IChart
    {
        public BarData BarChartData { get; private set; }
        public BarStyle BarChartStyle { get; private set; }

        // Other custom members you desire for your bad chart implementation

        public BarChart(BarData data, BarStyle style)
        {
            BarChartData = data;
            BarChartStyle = style;
        }
    }

MSDN examples are detailed here. Personally, I would avoid using an abstract class until you determine that there is truly common logic for all charts that could be encapsulated. No reason to over-design now, simply use an interface.

I hope this is helpful!

1 Comment

Small niggle... Rather than "inheriting the base class" you're demonstrating "inheriting/implementing an interface".
5

You probably don't want your base Chart to get instantiated since its function is nondescript, so you'd want it to be an abstract class.

public abstract class Chart
{
    // Public properties common to all charts
    public ChartData data;
    public ChartStyle style;
    public string RowNumber { get; set; }
    public string UniqueName { get; set; }

    // A common method    
    public void AddDataPoint () {}

    // A method all charts have that may change between different types of charts
    public virtual void DrawChart () {}

    // Constructor
    public Chart (ChartData cd, ChartStyle cs)
    {
        data = cd;
        style = cs;
    }

    // Protected method (like a private method, but gets inherited)
    protected void Finalize () {}
}

You'd want the inheriting class to look like this:

public class BarChart : Chart
{
    // BarChart exclusive properties

    // A method all charts have that BarCharts implements differently
    public override void DrawChart () {}

    // Constructor that calls the base constructor
    public BarChart (ChartData cd, ChartStyle cs) : base (cd, cs)
    {

    }
}

On methods you'll want to make sure you use the virtual and override keywords so that calls to the base class's methods get overridden by the child class's methods.

Abstract classes vs Interfaces

Unlike an interface, an abstract class allows you to define the methods in it. The methods in an interface all are signatures only. In the abstract class you can also have abstract methods that have only their signatures defined. Abstract classes work like regular classes in inheritance, though; you can only inherit from one. You can inherit from as many interfaces as you like. If you wanted BarChart to inherit from Chart and an interface like IComparable, you'd declare the class with the abstract class first like public class BarChart : Chart, IComparable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.