Innanzitutto dichiaro un enum con la lista dei possibili flag; è importante che l'enum abbia l'attributo [Flags] e che gli elementi della lista abbiano associato un valore che sia potenza di 2 (1, 2, 4, ...). Si possono definire fino a 32 elementi.

[Flags] private enum Buttons : int
{
···Ok = 1, Cancel = 2, Retry = 4, Help = 8
}

Tip: è possibile aggiungere alla enumerazione un elemento "All" così: All = Ok | Cancel | Retry | Help

Attivare più flag: concatenali con l'operatore bitwise "|" (or):
Buttons buttons;
buttons = Buttons.Ok | Buttons.Cancel;


Disattivare un flag: usa l'operatore bitwise "~" (not):
buttons &= ~Buttons.Cancel;

Verificare se un flag è attivo:

Testing to see if a certain flag is set: usa l'operatore bitwise "&" (and):
if ((buttons&Buttons.Ok)==Buttons.Ok)
···Console.WriteLine("Ok");

posted on mercoledì 14 novembre 2007 22.41
Filed Under [ Scoperte di un principiante ]

Comments

No comments posted yet.
Your Comment




 
Please add 6 and 1 and type the answer here: