Labels

Tuesday, March 31, 2009

Collection Myth - CollectionBase

Hi,

 

This is just an code-snippet for implementing collection using ‘ColletionBase’.

This is comparatively very easy compared to ‘ICollection’.

 

Note: ‘List’ is an inbuilt property of CollectionBase Class.

 

public class MyCollectionBase : CollectionBase

    {

        public MyCollectionBase()

        { }

 

        public int Add(UserI objUser)

        {

            return List.Add(objUser);  // Here the List is inbuilt property of CollectionBase Class

        }

 

        public void Remove(UserI objUser)

        {

            List.Remove(objUser);

        }

 

        //This is defined by me.

        public UserI Item(int i)

        {

            return List[i] as  UserI;

        }

    }

public class UserI

    {

        private string _au_id;

        private string _au_fname;

    }

 

 

 

static void Main()

{

            StringBuilder str = new StringBuilder(5);

            MyCollectionBase userList = new MyCollectionBase();

            UserI user = new UserI();

            user.Id = "1";

            user.Fname = "AA";

            userList.Add(user);

 

            user = new UserI();

            user.Id = "2";

            user.Fname = "BB";

            userList.Add(user);

 

            user = new UserI();

            user.Id = "3";

            user.Fname = "CC";

            userList.Add(user);

 

            foreach (UserI myUser in userList)

            {

              str.Append("Id: ").Append(myUser.Id).Append(", Name: ").Append(myUser.Fname).Append(Environment.NewLine);

            }

 

            MessageBox.Show(str.ToString());

}

 

 

Regards,

Arun Manglick

No comments:

Post a Comment