0
Answer

Doubt: turn base class method into private

Paulo Amaral

Paulo Amaral

18y
1.6k
1
Hi there, this is my first post in this forum. Here goes the question...

I want to create a custom array collection (MyCollection class) , to store objects of a custom class (MyObject class). Something like this:

public class MyCollection : System.Collections.ArrayList
{
}


Now, i want that only objects of the MyObject class can be added to the collection. So, i tryed to override the Add method, like this:

public class MyCollection : System.Collections.ArrayList
{
    /* prevents external calling of this method */
    private override int Add (object o)
    {
        return base.Add(o);
    }

    /* allows add only MyObject objects */
    public int Add (MyObject o)
    {
        return base.Add(o);
    }
}


I received the following error:
Virtul or abstract methods cannot be private.

So, i tried to use "protected" instead of "private", and i still have error:
cannot change access modifiers when overriding 'public' inherited member 'System.Collections.ArrayList.Add(object)'

I want that only MyObjects objects can be included in my custom collection.

Any solution? Thanks!