Wednesday, January 06, 2010

Object Property as Key in Collection

A innovation of Dictionary got excellent solution in stocking data. After countless uses I decided to find alternative method in combination of key and object and now I offering KeyedCollection. Little example will present a using of aforementioned object:

Simple classes:

public class User
   {
       public int Id { get; set; }
       public string Name { get; set; }
       public Address Address { get; set; }
   }

   public class Address
   {
       public string Country { get; set; }
       public string City { get; set; }
       public string Zip { get; set; }
   }

Two object that inherits abstract KeyedCollections:

/// <summary>
///A key is ID of player
/// </summary>
public class UserCollection : KeyedCollection<int, User>
{
    //Implementing member
    protected override int GetKeyForItem(User item)
    {
        return item.Id;
    }
}

/// <summary>
/// A key is User Name
/// </summary>
public class UserCollection1 : KeyedCollection<string, User>
{
    //Implementing member
    protected override string GetKeyForItem(User item)
    {
        return item.Name;
    }
}

 

Little example to using:

public class Test
{
    public TestById()
    {
        var userCollection = new UserCollection();
        userCollection.Add(new User
                               {
                                   Id = 5,
                                   Name = "John Smith",
                                   Address = new Address
                                                 {
                                                     City = "NY",
                                                     Country = "USA",
                                                     Zip = "12345"
                                                 }
                               });

        userCollection.Add(new User
                               {
                                   Id = 17,
                                   Name = "James Brown",
                                   Address = new Address
                                                 {
                                                     City = "LA",
                                                     Country = "USA",
                                                     Zip = "54321"
                                                 }
                               });

        Console.Write(userCollection[6]);
    }
     public void  TestByName()
    {
        var userCollection = new UserCollection1();
        userCollection.Add(new User
                               {
                                   Id = 5,
                                   Name = "John Smith",
                                   Address = new Address
                                                 {
                                                     City = "NY",
                                                     Country = "USA",
                                                     Zip = "12345"
                                                 }
                               });

        userCollection.Add(new User
                               {
                                   Id = 17,
                                   Name = "James Brown",
                                   Address = new Address
                                                 {
                                                     City = "LA",
                                                     Country = "USA",
                                                     Zip = "54321"
                                                 }
                               });

        Console.Write(userCollection["John Smith"]);
    }
}

 

 

If a project in VS 2008 you can run in heritable object LINQ requests and more. Enjoy!

kick it on DotNetKicks.com

No comments: