开发者

C# Storing string,int,string in an accessable variable

I need to save a class with list of countries in statics for caching.

th开发者_JS百科e data is built with

string shortName //Primary Key - (IL or UK for example)
int ID //Unique - has no meaning, but needs to be saved
string longName //(Israel or United Kingdom for example)

I thought of saving it in a Dictionary:

Dictionary<string , Dictionary<int,string>> list = new Dictionary<string, Dictionary<int,string>>();

this is the API that i need in the class:

 Countries.getByShortName();// I dont know what to return, I'd love some advise
 Countries.getById();// I might need that
 Countries.getAll();// I dont know what to return, I'd love some advise

What do you think is the best way of handling this class?

thanks


How about using a struct or class?

class Country
{
    string shortName; // Primary Key - (IL or UK for example)
    int ID; // Unique - has no meaning, but needs to be saved
    string longName; // (Israel or United Kingdom for example)
}

You can then store your countries in a generic List, for example:

List<Country> countries = new List<Country>();
countries.Add(new Country()
{
    shortName = "UK",
    ID = 1,
    longName = "United Kingdom",
});

Implementing your given methods then becomes very straightforward:

Country getByShortName(string shortName)
{
    foreach (Country country in countries)
    {
        if (country.shortName == shortName)
        {
            return country;
        }
    }
    return null;
}


You should create a custom type:

public class Country
{
  public string ShortName {get; set;}
  public int ID {get; set;}
  public string LongName {get; set;}
}

and then store the countries in a Dictionary<string, Country> from which you'll be able to do:

var UK = _countries["UK"];
UK.ID...
UK.LongName...


why wont you make your own class?

class Country
{
   public string shortName { get; set; } //Primary Key - (IL or UK for example)
   public int ID { get; set; } //Unique - has no meaning, but needs to be saved
   public string longName { get; set; } //(Israel or United Kingdom for example)
}

then just make another class, that will contain methods you need

class Countries
{
   List<Country> countries = new List<Country>();

   public void Add(Country c)
   {
      countries.Add(c);
   }

   public List<Country> getByShortName();
   public List<Country>  getById();
   public List<Country>  getAll();
}


I think you need to create your own class incapsulating all three fields and to use own Collection of It. For example:

class CountryInfo
{
string shortName //Primary Key - (IL or UK for example)
int ID //Unique - has no meaning, but needs to be saved
string longName //(Israel or United Kingdom for example)
}

class CountryCollection : Collection <CountryInfo>
{
 //Implement methods what you need
 void getByShortName();// I dont know what to return, I'd love
 void getById();// I might need that
 void getAll();// I dont know what to return, I'd l
}

If you like quick search then to use a pair of dictionaries:

    class CountryInfo 
    {
    string shortName //Primary Key - (IL or UK for example)
    int ID //Unique - has no meaning, but needs to be saved
    string longName //(Israel or United Kingdom for example)
    }

    class CountryCollection
    {
      Dictionary <int, string> Ids = new Dictionary <int, string> ();
      Dictionary <string, string> shortNames = new Dictionary <string, string> ();

     void Add (CountryInfo info)
{
  Ids.Add (info.ID, info.longName);
  shortnames.Add(info.ID, info.longName);
}
     //Implement methods what you need
     void getByShortName();// I dont know what to return, I'd love
     void getById();// I might need that
     void getAll();// I dont know what to return, I'd l
    }


I suggest the use of static methods:

public class Country
{
    public   string ShortName {get;set;}
    public int ID {get;set;}
    public string LongName { get; set; }
}

public class Countries
{
   static  Dictionary<string, Country> dict = new Dictionary<string, Country>();
   public static void Add(Country country)
   {
       if (!dict.ContainsKey(country.ShortName))
           dict.Add(country.ShortName, country);
   }
   public static Country GetByShortName(string ShortName)
   {
       if (dict.ContainsKey(ShortName))
           return dict[ShortName];
       return null;
   }
   public static Country GetById(int id)
   {
       var result = from country in dict
                    where country.Value.ID==id
                    select new Country
                    {
                        ID = country.Value.ID,
                        ShortName = country.Value.ShortName,
                        LongName = country.Value.LongName
                    };
       return result.SingleOrDefault();
   }
   public static List<Country> GetAll()
   {
       var result = from country in dict
                    select new Country
                    {
                        ID = country.Value.ID,
                        ShortName = country.Value.ShortName,
                        LongName = country.Value.LongName
                    };
       return result.ToList();
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜