As I develop more and more for a living I have been trying to perfect my developing skills with personal projects in hope to expand my professional career, one of these is the concept of Polymorphism.

The last few projects I have worked on, I have this model where I have a TableLayoutPanel, that is a grid of custom UserControls. To try and start reducing redundency coding, I am trying to build base classes of both the Component and UserControl. One part of this is I have a function in the TableLayoutPanel that is used to add the custom UserControls to the table. The function looks as follows

/// <summary>
/// Adds a new Item to the table
/// </summary>
/// <param name="item">Item to Add</param>
protected virtual void Add (object item)
{
    Items.Add(new SelectableComponentBase(((SelectableComponentBase) item).ID));

    Items.Last().Width = (this.Width - 20); //use 20 in case a scrollbar appears
    Items.Last().Anchor = (AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top);

    Items.Last().Clicked += new EventHandler(this.SelectableComponentBase_Clicked);

    Controls.Add(Items.Last());
    Controls [Controls.Count - 1].Invalidate();

    ScrollControlIntoView(Controls [Controls.Count - 1]); //scrolls down to the bottom (where the item is added).

    if (ItemAdded != null) //lastly fire the event that the item has been added
    {
        ItemAdded(Items.Last(), new SelectableComponentBaseEventArgs(Items.Last().ID, Items.FindIndex(x => x.ID.Equals(Items.Last().ID))));
    }
}

As I mentioned, this adds a customer UserControl to the table, as well as some other code related to sizing, events, ext. Now my current problem here is that first line you see Items.Add(new SelectableComponentBase(((SelectableComponentBase) item).ID));.

Items is a property within the TableLayoutPanel, that looks as follows

/// <summary>
/// Get the collection of SelectableComponentBases
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
protected virtual List<SelectableComponentBase> Items
{
    get;
    private set;
}

Here's the thing. I want to be able to preserve that "Add" function to the point that if I make a new UserControl that inherits the "SelectableComponentBase", and a TableLayoutPanel that inherits the base one you see, BUT, the TableLayoutPanel can use the base Add function, without having to override it (because most of the times that should work fine as is).

So again, I am trying to find a way where without having to override the base "Add" function, I can add a custom UserControl that inherits the base UserControl, to a custom TableLayoutPanel that inherits the base one.

Hopefully this all makes sense, I am having trouble putting the idea into word which is why I am coming here for help because I am not sure what might be the proper approach to this, or if it's even possible

Recommended Answers

All 3 Replies

Greetings,

First of all do you know what is meant by Polymorphism?. Simply, it means to treat objects of the derived class as if they were objects of the base class.
baseType btVar = new derivedType()
In addition, when you use a variable of base class type to hold a reference of the derived class, you can only call methods declared within the base class. if you try to call methods defined within the derived class only a compilation error will be thrown unless you explicitly cast the base class reference to a derived class type.
(derivedType)btVar
Hope this info. help you to figure out what you need to do :)

I do know what Polymorphism is. The way I like to explain it is build a class on top of a base class, allowing for a piece of code to contain common used code to exist in one place, allow for easy maintance and reduce of redundent code. In my case I had a base UserControl and TableLayoutPanel that offered functionality like "is an item hovered over" or "is an item selected", as well as how to add and remove items from the table, and allow for the table to resize properly (TableLayoutPanels can have an annoyance that removing items leaves empty space).

That being said, I think maybe my original question was misunderstood. I have built base UserControl and TableLayoutPanel.

I have then derived new classes that inherit these base components. Now in the BASE version of the TableLayoutPanel is the "Add" function I listed above that allows for me to add one of these base UserControls to the table. However, what I want to know is can I add a UserControl that inherited the base UserControl to the Table, using the Table's base add function? (I am trying to prevent referencing issues in the process, so it's not doing pass by reference that can occur for when you add a custom item to a List, that doesn't have a proper constructor to accept the type, and copy over each param)

Greetings,

I still have a little bit of confusion about what you want. So I will tell you what I got and you can fill the missing points OK. You made a custom user control which you will use as a base class for other custom user control(s) you are going to create. Lets say that base custom user control you have is BX OK. You also made a custom user control from the TableLayoutPanel control or...?! If that is the case. So you can make the Add function take a parameter of type (BX) and for sure you can then pass as an argument any object that inherits directly or indirectly from the BX class. I hope I answered your question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.