in

Microsoft Philippines Community

A community for users, customers, and partners of Microsoft products in the Philippines :)

Trivia #4

Last post 03-17-2006 8:21 AM by joeycalisay. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 03-16-2006 8:47 PM

    Trivia #4

    We're all familiar with creating stronly-typed collection in .NET 1.1 and the MyItemCollection class below demonstrates this:

    public class MyItemCollection : CollectionBase
    {
        public int Add(MyItem myItem)
        {
            return List.Add(myItem);
        }
     
        public void Remove(MyItem myItem)
        {
            List.Remove(myItem);
        }

        public void Insert(int index, MyItem myItem)
        {
            List.Insert(index, myItem);
        }

        public int IndexOf(MyItem myItem)
        {
            return List.IndexOf(myItem);
        }
     
        public bool Contains(MyItem myItem)
        {
            return List.Contains(myItem);
        }

        public MyItem this[int index]
        {
            get { return List[index] as MyItem; }
            set { List[index] = value; }
        }
    }

    The said collection is still not strongly-typed, anybody who knows how I can add a non-MyItem type (and non-MyItem subtype) instance to a MyItemCollection instance?
  • 03-16-2006 9:14 PM In reply to

    • bonskijr
    • Top 50 Contributor
      Male
    • Joined on 09-01-2004
    • Repatriated
    • Posts 873
    • Points 8,646

    Re: Trivia #4

    by passing a generic object

    MyItemCollection  myItems = new MyTemCollection();
    myItems.Add ((object) "string item not a my item");

    Bonski's Box | Buhay Saudi
  • 03-16-2006 9:19 PM In reply to

    Re: Trivia #4

    you'll be issued a compile error with that...
  • 03-16-2006 9:41 PM In reply to

    • bonskijr
    • Top 50 Contributor
      Male
    • Joined on 09-01-2004
    • Repatriated
    • Posts 873
    • Points 8,646

    Re: Trivia #4

    sorry on top of my head solution kse Stick out tongue [:P] .. anyhow no restriction ba or inherit the collection and hide or shadow the add method to accept non-MyItems.. although that is cheating already,,
    Bonski's Box | Buhay Saudi
  • 03-16-2006 9:50 PM In reply to

    Re: Trivia #4

    by inheriting from it, you'll not be adding to MyItemCollection instance but from its subclass instance...
  • 03-16-2006 9:56 PM In reply to

    • bonskijr
    • Top 50 Contributor
      Male
    • Joined on 09-01-2004
    • Repatriated
    • Posts 873
    • Points 8,646

    Re: Trivia #4

    Ok clarification when you mean MyItem sub-type all those non MyItem object and their descendant? Coz the other way around is to inherit from MyItem and the descendant is technically no longer the same as with the base class, except for its inherited members/properties.
    Bonski's Box | Buhay Saudi
  • 03-16-2006 10:01 PM In reply to

    Re: Trivia #4

    sub-type = subclass = descendant

    obviously you can add a MyItem subtype instance since it will be boxed as a MyItem type.
  • 03-16-2006 10:05 PM In reply to

    • JonathanV
    • Top 50 Contributor
    • Joined on 10-24-2005
    • Unknown
    • Posts 731
    • Points 4,866

    Re: Trivia #4

    hmmm.
    -"The World is flat..... scared?"-
  • 03-17-2006 1:05 AM In reply to

    • bonskijr
    • Top 50 Contributor
      Male
    • Joined on 09-01-2004
    • Repatriated
    • Posts 873
    • Points 8,646

    Re: Trivia #4

     bonskijr wrote:
    by passing a generic object

    MyItemCollection  myItems = new MyTemCollection();
    myItems.Add ((object) "string item not a my item");



    How about typecasting the Collectionbase to IList interface:
    ((IList)myItems).Add("Not MyItem Type");
    ((IList)myItems).Add(new Object());
    ((IList)myItems).Add(new OtherType());
    Bonski's Box | Buhay Saudi
  • 03-17-2006 8:21 AM In reply to

    Re: Trivia #4

     bonskijr wrote:
     bonskijr wrote:
    by passing a generic object

    MyItemCollection  myItems = new MyTemCollection();
    myItems.Add ((object) "string item not a my item");



    How about typecasting the Collectionbase to IList interface:
    ((IList)myItems).Add("Not MyItem Type");
    ((IList)myItems).Add(new Object());
    ((IList)myItems).Add(new OtherType());


    good job!  di mo na pinagbigyan iba ah...  so in order to prevent this, you'd have to override OnValidate.  For more info see my blog post here.
Page 1 of 1 (10 items)
Copyright © 2008 Microsoft Philippines Community

Powered by Community Server (Commercial Edition), by Telligent Systems