daily pastebin goal
69%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Using strings in an enum
  2. public enum Accounts
  3. {
  4.     BankOfAmerica = 123654,
  5.     BankOfIndia = 765091
  6. }
  7.    
  8. enum Banks
  9. {
  10.     BankOfFoo,
  11.     BankOfBar,
  12.     BankOfBlah,
  13.     BankOfABC
  14. }
  15.    
  16. var accounts = new Dictionary<string,string>();
  17. accounts.Add("Bank of America", "ABC12345");
  18. accounts.Add("Bank of India", "122-6X-666");
  19.  
  20. string number = accounts["Bank of America"];
  21.    
  22. public enum Accounts {
  23.     BankOfAmerica,
  24.     BankOfIndia
  25. }
  26.  
  27. var accounts = new Dictionary<Accounts,string>();
  28. accounts.Add(Accounts.BankOfAmerica, "ABC12345");
  29. accounts.Add(Accounts.BankOfIndia, "122-6X-666");
  30.  
  31. string number = accounts[Accounts.BankOfAmerica];
  32.    
  33. public class BankAccount
  34. {
  35.     public string Name { get; set; }
  36.     public int Number { get; set;}
  37.  
  38.     public BankAccount(string name, int number)
  39.     {
  40.         Name = name;
  41.         Number = number;
  42.     }
  43. }
  44.    
  45. public List<BankAccount> BankAccounts = new List<BankAccount>();
  46.    
  47. public BankAccount GetAccountByName(string name)
  48. {
  49.     return BankAccounts.FirstOrDefault(a => a.Name == name);
  50. }
  51.    
  52. var account = GetAccountByName("BankOfAmerica");
  53.  
  54. account.Number = 12345678;
  55.    
  56. class AccountAttribute : Attribute
  57. {
  58.     public string MyValue { get; set; }
  59.     // add any other stuff...
  60. }
  61.  
  62. public enum Accounts
  63. {
  64.     [Account(MyValue = "A")]
  65.     BankOfAmerica = 123654,
  66.  
  67.     [Account(MyValue = "B")]
  68.     BankOfIndia = 765091
  69. }
  70.    
  71. public static class Accounts
  72. {
  73.     public static dynamic BankOfAmerica { get; set; }
  74.     public static dynamic BankOfIndia { get; set; }
  75.  
  76.     static Accounts()
  77.     {
  78.         BankOfAmerica = 123654;
  79.         BankOfIndia = 765091;
  80.     }
  81.  
  82.     public static dynamic Parse(dynamic value)
  83.     {
  84.         if (value.ToString() == BankOfAmerica.ToString() || value.ToString() == "BankOfAmerica")
  85.             return BankOfAmerica;
  86.         if (value.ToString() == BankOfIndia.ToString() || value.ToString() == "BankOfIndia")
  87.             return BankOfIndia;
  88.         throw new Exception("cannot parse");
  89.     }
  90. }
  91.    
  92. Accounts.BankOfAmerica = "123";
  93.    
  94. if (account == Account.BankOfAmerica)
  95. {
  96.     // welcome to usa
  97. }
  98. else if (account == Account.BankOfIndia)
  99. {
  100.     // count us one by one if you can
  101. }
  102.    
  103. try
  104. {
  105.     account = Account.Parse("123654");
  106. }
  107. catch
  108. {
  109.     // oops
  110. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top