Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class MyClass
  5. {
  6.    
  7.     [Flags]
  8.     public enum ErrorTypes : int {
  9.         None = 0,
  10.         MissingPassword = 1,
  11.         MissingUsername = 2,
  12.         PasswordIncorrect = 4
  13.     }
  14.  
  15.     public static void RunSnippet()
  16.     {
  17.  
  18.         //used like the following...
  19.        
  20.         ErrorTypes error = ErrorTypes.None;
  21.         error = error.Append(ErrorTypes.MissingUsername);
  22.         error = error.Append(ErrorTypes.MissingPassword);
  23.         error = error.Remove(ErrorTypes.MissingUsername);
  24.         error = error.Append(ErrorTypes.PasswordIncorrect);
  25.        
  26.         Console.WriteLine("MissingUsername:\t" + error.Has(ErrorTypes.MissingUsername));
  27.         Console.WriteLine("MissingPassword:\t" + error.Has(ErrorTypes.MissingPassword));
  28.         Console.WriteLine("PasswordIncorrect:\t" + error.Has(ErrorTypes.PasswordIncorrect));
  29.        
  30.         //then you can check using other methods
  31.         if (error.Has(ErrorTypes.MissingUsername)) {
  32.            
  33.         }
  34.  
  35.     }
  36.    
  37.    
  38.     #region Helper methods
  39.    
  40.     public static void Main()
  41.     {
  42.         try
  43.         {
  44.             RunSnippet();
  45.         }
  46.         catch (Exception e)
  47.         {
  48.             string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
  49.             Console.WriteLine(error);
  50.         }
  51.         finally
  52.         {
  53.             Console.Write("Press any key to continue...");
  54.             Console.ReadKey();
  55.         }
  56.     }
  57.  
  58.     private static void WL(object text, params object[] args)
  59.     {
  60.         Console.WriteLine(text.ToString(), args);  
  61.     }
  62.    
  63.     private static void RL()
  64.     {
  65.         Console.ReadLine();
  66.     }
  67.    
  68.     private static void Break()
  69.     {
  70.         System.Diagnostics.Debugger.Break();
  71.     }
  72.  
  73.     #endregion
  74. }
  75. public static class EnumExtensions {
  76.  
  77.     public static T Append<T>(this System.Enum type, T value) {
  78.         return (T)(object)(((int)(object)type | (int)(object)value));
  79.     }
  80.  
  81.     public static T Remove<T>(this System.Enum type, T value) {
  82.         return (T)(object)(((int)(object)type & ~(int)(object)value));
  83.     }
  84.  
  85.     public static bool Has<T>(this System.Enum type, T value) {
  86.         return (((int)(object)type & (int)(object)value) == (int)(object)value);
  87.     }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement