0

I have several classes that have to connect to something before they can do anything. So ideally, I create a base class with a constructor with 1 parameter. Then these classes inherit from this base class, something like this:

public class BaseErase
{
    private string _connectionString;
    public string ConnectionString
    {
        get 
        {
           return _connectionString; 
        }
        set 
        {
           _connectionString = value; 
        }
    }

    public BaseErase(string connectionString)
    {
        ConnectionString = connectionString;
    }

    //public void SetConnection(string connectionString)
    //{
    //    ConnectionString = connectionString;
    //}
}

Then my derived class would inherit from BaseErase, something like this:

class ToEraseClass : BaseErase
{
    public void GetData()
    {
        string connect = ConnectionString;
        Console.WriteLine(connect);
    }
}

Then finally, I instantiate my class and call GetData():

ToEraseClass toErase = new ToEraseClass(ConnectionString);
toErase.GetData();

Unfortunately, constructors are not inherited, so none of this works. I know the fix to this, but it requires that I modify every single class.

My question is, what's the purpose of inheritance here?

Thanks.

  • possible duplicate of [How to inherit constructors?](http://stackoverflow.com/questions/223058/how-to-inherit-constructors) – mnieto Mar 26 '14 at 18:08
  • Out-of-context, it's hard to say. But if you need a collection of `BaseErase`-derived objects, then it makes sense for them to inherit from the base class. Or if you need to have a method that can handle any type of `BaseErase`-derived object. If you don't have any requirement like that, then there might not be much point to it. Inheritance isn't a panacea. – Matt Burland Mar 26 '14 at 18:08
  • @Andrei: I think the OP knows that – Matt Burland Mar 26 '14 at 18:09
  • @MattBurland, oops, missed `I know how to fix this`, sorry – Andrei Mar 26 '14 at 18:11

1 Answers1

1

EDIT: In response to your comment below:

In that case, I can just as easily add a method in every class

In that case what inheritance is doing is removing the need to add the method in every class. Because if you add the method in every class then you have to maintain the method in every class.

Duplicate code is a very strong code smell that you should look to eliminate as soon as possible

In order to remove duplication you can add a has-a relationship for e.g. make a ConnectionFangler class which all of the eraser classes reference to manage their connection. Or as is being discussed you add an is-a relationship by creating a base class which fangles the connection.


If all your base class is doing is setting a connection string then I'd say there is no point to inheritance here. Inheritance should be about an is-a relationship and not a has-a relationship.

Since it seems that you don't want to add a call to the base constructor....

If you want to ensure that every type of Erase class has a connection string you can make them implement an interface that has that property

public IEraseConnection 
{
  string ConnectionString {get;set;}
}

You can then set the connection string in the constructor or directly. Whichever works best for your application.

For completeness if you want to use a base class then I'd do something like this:

using System;

namespace ExampleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            new ToEraseClass("Boom!").GetData();
            Console.ReadLine();
        }
    }

    public class BaseErase
    {
        protected string ConnectionString
        {
            get; private set;
        }

        public BaseErase(string connectionString)
        {
            ConnectionString = connectionString;
        }
    }

    public class ToEraseClass : BaseErase
    {
        public ToEraseClass(string connectionString) : base(connectionString) 
        {}

        public void GetData()
        {
            Console.WriteLine(ConnectionString);
        }
    }
}
Paul D'Ambra
  • 7,629
  • 3
  • 51
  • 96
  • Thanks for the post. I understand that I have to explicitly set the constructor in every single class and that's how I'm doing it, but the purpose was not to modify every single class I created. In that case, I can just as easily add a method in every class. –  Mar 26 '14 at 19:24