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

Untitled

By: a guest on Jun 2nd, 2012  |  syntax: None  |  size: 4.24 KB  |  hits: 23  |  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. C# Storing string,int,string in an accessable variable
  2. string shortName //Primary Key - (IL or UK for example)
  3. int ID //Unique - has no meaning, but needs to be saved
  4. string longName //(Israel or United Kingdom for example)
  5.        
  6. Dictionary<string , Dictionary<int,string>> list = new Dictionary<string, Dictionary<int,string>>();
  7.        
  8. Countries.getByShortName();// I dont know what to return, I'd love some advise
  9.  Countries.getById();// I might need that
  10.  Countries.getAll();// I dont know what to return, I'd love some advise
  11.        
  12. public class Country
  13. {
  14.     public   string ShortName {get;set;}
  15.     public int ID {get;set;}
  16.     public string LongName { get; set; }
  17. }
  18.  
  19. public class Countries
  20. {
  21.    static  Dictionary<string, Country> dict = new Dictionary<string, Country>();
  22.    public static void Add(Country country)
  23.    {
  24.        if (!dict.ContainsKey(country.ShortName))
  25.            dict.Add(country.ShortName, country);
  26.    }
  27.    public static Country GetByShortName(string ShortName)
  28.    {
  29.        if (dict.ContainsKey(ShortName))
  30.            return dict[ShortName];
  31.        return null;
  32.    }
  33.    public static Country GetById(int id)
  34.    {
  35.        var result = from country in dict
  36.                     where country.Value.ID==id
  37.                     select new Country
  38.                     {
  39.                         ID = country.Value.ID,
  40.                         ShortName = country.Value.ShortName,
  41.                         LongName = country.Value.LongName
  42.                     };
  43.        return result.SingleOrDefault();
  44.    }
  45.    public static List<Country> GetAll()
  46.    {
  47.        var result = from country in dict
  48.                     select new Country
  49.                     {
  50.                         ID = country.Value.ID,
  51.                         ShortName = country.Value.ShortName,
  52.                         LongName = country.Value.LongName
  53.                     };
  54.        return result.ToList();
  55.    }
  56. }
  57.        
  58. class Country
  59. {
  60.     string shortName; // Primary Key - (IL or UK for example)
  61.     int ID; // Unique - has no meaning, but needs to be saved
  62.     string longName; // (Israel or United Kingdom for example)
  63. }
  64.        
  65. List<Country> countries = new List<Country>();
  66. countries.Add(new Country()
  67. {
  68.     shortName = "UK",
  69.     ID = 1,
  70.     longName = "United Kingdom",
  71. });
  72.        
  73. Country getByShortName(string shortName)
  74. {
  75.     foreach (Country country in countries)
  76.     {
  77.         if (country.shortName == shortName)
  78.         {
  79.             return country;
  80.         }
  81.     }
  82.     return null;
  83. }
  84.        
  85. public class Country
  86. {
  87.   public string ShortName {get; set;}
  88.   public int ID {get; set;}
  89.   public string LongName {get; set;}
  90. }
  91.        
  92. var UK = _countries["UK"];
  93. UK.ID...
  94. UK.LongName...
  95.        
  96. class Country
  97. {
  98.    public string shortName { get; set; } //Primary Key - (IL or UK for example)
  99.    public int ID { get; set; } //Unique - has no meaning, but needs to be saved
  100.    public string longName { get; set; } //(Israel or United Kingdom for example)
  101. }
  102.        
  103. class Countries
  104. {
  105.    List<Country> countries = new List<Country>();
  106.  
  107.    public void Add(Country c)
  108.    {
  109.       countries.Add(c);
  110.    }
  111.  
  112.    public List<Country> getByShortName();
  113.    public List<Country>  getById();
  114.    public List<Country>  getAll();
  115. }
  116.        
  117. class CountryInfo
  118. {
  119. string shortName //Primary Key - (IL or UK for example)
  120. int ID //Unique - has no meaning, but needs to be saved
  121. string longName //(Israel or United Kingdom for example)
  122. }
  123.  
  124. class CountryCollection : Collection <CountryInfo>
  125. {
  126.  //Implement methods what you need
  127.  void getByShortName();// I dont know what to return, I'd love
  128.  void getById();// I might need that
  129.  void getAll();// I dont know what to return, I'd l
  130. }
  131.        
  132. class CountryInfo
  133.     {
  134.     string shortName //Primary Key - (IL or UK for example)
  135.     int ID //Unique - has no meaning, but needs to be saved
  136.     string longName //(Israel or United Kingdom for example)
  137.     }
  138.  
  139.     class CountryCollection
  140.     {
  141.       Dictionary <int, string> Ids = new Dictionary <int, string> ();
  142.       Dictionary <string, string> shortNames = new Dictionary <string, string> ();
  143.  
  144.      void Add (CountryInfo info)
  145. {
  146.   Ids.Add (info.ID, info.longName);
  147.   shortnames.Add(info.ID, info.longName);
  148. }
  149.      //Implement methods what you need
  150.      void getByShortName();// I dont know what to return, I'd love
  151.      void getById();// I might need that
  152.      void getAll();// I dont know what to return, I'd l
  153.     }