Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 1.09 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to access a custom collection property by its property name
  2. public class Property : IComparable<Property>
  3. {
  4.    public string Name;
  5.    public string Value;
  6.    public string Group;
  7.    public string Id;
  8.    ...
  9.    ...
  10.  
  11.    public int CompareTo(Property other)
  12.    {
  13.       return Name.CompareTo(other.Name);
  14.    }
  15. }
  16.        
  17. Public List<Property> properties;
  18.        
  19. var myColor = properties["Color"].Value;
  20.        
  21. var props = properties.ToDictionary( x => x.Name );
  22. Property prop = props["some name"];
  23.        
  24. public class PropertyCollection : List<Property>
  25. {
  26.     public Property this[string name]
  27.     {
  28.         get
  29.         {
  30.             foreach (Property prop in this)
  31.             {
  32.                 if (prop.Name == name)
  33.                     return prop;
  34.             }
  35.             return null;
  36.         }
  37.     }
  38. }
  39.        
  40. PropertyCollection col = new PropertyCollection();
  41. col.Add(new Property(...));
  42. Property prop = col["some name"];
  43.        
  44. Dictionary<string, Property> properties = new Dictionary<string, Property>();
  45.  
  46. //you add it like that:
  47. properties[prop.Name] = prop;
  48.  
  49. //then access it like that:
  50. var myColor = properties["Color"];