Tech Off Thread

7 posts

Hiding Interface Members

Back to Forum: Tech Off
  • User profile image
    JParrish

    I have been trying to get more experience with writing libraries.. and I have a question about how interface members can be hidden.

    I am trying to create a strongly typed collection, just like the TreeNodeCollection class. TreeNodeCollection implement IList, but it does not contain a visible definition of "public int Add(object value)" which is required for that interface. Instead it contains "public virtual new System.Int32 Add ( System.Windows.Forms.TreeNode node )" and an overloaded method "public virtual new System.Windows.Forms.TreeNode Add ( System.String text )"

    How is it that this class doesn't show a visible method for the IList version of Add? If I change the signature in my class to the object type I want to use as opposed to plain old "object" I get an error saying that my class has not implement the required IList Add method. Thanks for any info.

  • User profile image
    Matt.Berther

    You can implement interface methods using the following C# syntax:

    public class Foo : IList
    {
        public int IList.Add(object value)
        {
            // your IList Add impl
        }
    }

    Doing so is called implementing the interface explicitly and by doing this, the methods will be available only on the interface.

    This means that in order to call Add(object) on class Foo, you'd first need to cast it to an IList.

    HTH...

  • User profile image
    JParrish

    Thanks for reply.. I looked at explicit interface implementation as one possibility but  there is still a problem with this because I want my collection to enforce an expected object type when I call Add.. for example.. in my case I have an "SRSItemCollection" class that would have an "Add(SRSItem)" method signature. because this implementation doesn't have "object" as the parameter type, I still get an error stating that the interface method has not been implemented. That makes sense to me as far as interfaces are concerned, but why does the TreeNodeCollection class get away with it?

  • User profile image
    Matt.Berther

    Try something like this:

    public class SRSItemCollection : IList
    {
        public void Add(SRSItem value)
        {
        }

        void IList.Add(object value)
        {
        }
    }

    In this case, the public interface to SRSItemCollection will expose Add(SRSItem), however the IList.Add will have an explicit implementation (which the Add(SRSItem) would defer to).

  • User profile image
    JParrish

    Thanks very much! That did the trick. I didn't pick up on declaring a public method over top of the explicit interface method when I read about explicit interface methods. Thanks again =) 

  • User profile image
    Sampy

    Try inheriting from System.Collections.CollectionBase. It not only provides a lot of the collection methods but it the internal data structures as well.

    A lot less work than implementing IList manually.

  • User profile image
    JParrish

    Thanks.. its funny I was talking with a co-worker about that about 10 minutes ago. I didn't realize last night that CollectionBase implemented IList, because it did not have an Add(*) at all. I looked into it more now, and I see that within the implementation class for CollectionBase you can reference "List" which provides an ability to call Add(object value). That will save me from keeping a private ArrayList.. and accessors.. thanks for the suggestion =)

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.