Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public class MyClass
- {
- [Flags]
- public enum ErrorTypes : int {
- None = 0,
- MissingPassword = 1,
- MissingUsername = 2,
- PasswordIncorrect = 4
- }
- public static void RunSnippet()
- {
- //used like the following...
- ErrorTypes error = ErrorTypes.None;
- error = error.Append(ErrorTypes.MissingUsername);
- error = error.Append(ErrorTypes.MissingPassword);
- error = error.Remove(ErrorTypes.MissingUsername);
- error = error.Append(ErrorTypes.PasswordIncorrect);
- Console.WriteLine("MissingUsername:\t" + error.Has(ErrorTypes.MissingUsername));
- Console.WriteLine("MissingPassword:\t" + error.Has(ErrorTypes.MissingPassword));
- Console.WriteLine("PasswordIncorrect:\t" + error.Has(ErrorTypes.PasswordIncorrect));
- //then you can check using other methods
- if (error.Has(ErrorTypes.MissingUsername)) {
- }
- }
- #region Helper methods
- public static void Main()
- {
- try
- {
- RunSnippet();
- }
- catch (Exception e)
- {
- string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
- Console.WriteLine(error);
- }
- finally
- {
- Console.Write("Press any key to continue...");
- Console.ReadKey();
- }
- }
- private static void WL(object text, params object[] args)
- {
- Console.WriteLine(text.ToString(), args);
- }
- private static void RL()
- {
- Console.ReadLine();
- }
- private static void Break()
- {
- System.Diagnostics.Debugger.Break();
- }
- #endregion
- }
- public static class EnumExtensions {
- public static T Append<T>(this System.Enum type, T value) {
- return (T)(object)(((int)(object)type | (int)(object)value));
- }
- public static T Remove<T>(this System.Enum type, T value) {
- return (T)(object)(((int)(object)type & ~(int)(object)value));
- }
- public static bool Has<T>(this System.Enum type, T value) {
- return (((int)(object)type & (int)(object)value) == (int)(object)value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement