Click here to Skip to main content
15,888,124 members
Home / Discussions / C#
   

C#

 
GeneralRe: Adding control to a form delets the custom control from the form Pin
Jay Shankar17-Sep-04 16:03
Jay Shankar17-Sep-04 16:03 
Generalprotect my picture Pin
oOomen17-Sep-04 6:39
oOomen17-Sep-04 6:39 
GeneralRe: protect my picture Pin
Heath Stewart17-Sep-04 7:00
protectorHeath Stewart17-Sep-04 7:00 
GeneralUnbound checkbox column in datagrid Pin
blakeb_117-Sep-04 6:12
blakeb_117-Sep-04 6:12 
GeneralRe: Unbound checkbox column in datagrid Pin
sreejith ss nair17-Sep-04 23:50
sreejith ss nair17-Sep-04 23:50 
QuestionBeginner: How to use BitFlags? Pin
matthias s.17-Sep-04 5:19
matthias s.17-Sep-04 5:19 
AnswerRe: Beginner: How to use BitFlags? Pin
leppie17-Sep-04 5:32
leppie17-Sep-04 5:32 
AnswerRe: Beginner: How to use BitFlags? Pin
Heath Stewart17-Sep-04 6:28
protectorHeath Stewart17-Sep-04 6:28 
Define an enumeration with the [Flags] attribute, which I recommend you read about. See FlagsAttribute in the .NET Framework SDK. Do not define them as properties or fields of your class unless they're simply numbers, not complex types. Enumerations are best used, and are used throughout the .NET BCL, like the Anchor enumeration used for the Control.Anchor property, among many, many others. Here's an example:
[Flags]
public enum Options
{
  None = 0, // Common to have 0 be "none" or something
  Option1 = 1, // 0x01
  Option2 = 2, // 0x02
  Option3 = 4, // 0x04
  Option4 = 8, // 0x08
  Option5 = 16, // 0x10
  Option6 = 32, // 0x20
  Option7 = 64, // 0x40
  Option8 = 128 // 0x80
}
Without the flags, you get a compiler error stating that you can't perform bitwise manipulation with your enumeration.

To check if an enumeration is present, you use bitwise operators which you can read about at http://msdn.microsoft.com/library/en-us/csref/html/vclrfCSharpOperators.asp[^].

For example, if you want to check if your property contains Options.Option4, you could do:
if ((myClass1.Options & Options.Option4) != 0)
{
  // Do something if Options.Option4 is set
}
Note that the naming guidelines instruct you to name enumerations without the FlagsAttribute as singular, while enumerations with the FlagsAttribute should be plural. This is because the former can only set one, while the latter can set multiple.

To "store" (assign) these in your class, define your property as your enumeration type:
public class MyClass
{
  private Options options;
  public Options Options
  {
    get { return this.options; }
    set { this.options = value; }
  }
}


This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles]
GeneralRe: Beginner: How to use BitFlags? Pin
matthias s.17-Sep-04 7:01
matthias s.17-Sep-04 7:01 
AnswerRe: Beginner: How to use BitFlags? Pin
Grimolfr17-Sep-04 7:31
Grimolfr17-Sep-04 7:31 
GeneralInvisible Text on Grid Pin
dbetting17-Sep-04 5:02
dbetting17-Sep-04 5:02 
QuestionIs this a compiler error? Pin
gordonpagan17-Sep-04 4:37
gordonpagan17-Sep-04 4:37 
AnswerRe: Is this a compiler error? Pin
leppie17-Sep-04 5:19
leppie17-Sep-04 5:19 
AnswerRe: Is this a compiler error? Pin
Steven Campbell17-Sep-04 7:46
Steven Campbell17-Sep-04 7:46 
GeneralWideChar Pin
yyf17-Sep-04 3:14
yyf17-Sep-04 3:14 
GeneralRe: WideChar Pin
Heath Stewart17-Sep-04 6:33
protectorHeath Stewart17-Sep-04 6:33 
GeneralFilefield postback woes Pin
Vodstok17-Sep-04 3:08
Vodstok17-Sep-04 3:08 
GeneralRe: Filefield postback woes Pin
sreejith ss nair17-Sep-04 3:40
sreejith ss nair17-Sep-04 3:40 
GeneralRe: Filefield postback woes Pin
Vodstok17-Sep-04 4:36
Vodstok17-Sep-04 4:36 
GeneralStream writing html Pin
Mikke_x17-Sep-04 2:50
Mikke_x17-Sep-04 2:50 
GeneralRe: Stream writing html Pin
Steven Campbell17-Sep-04 4:33
Steven Campbell17-Sep-04 4:33 
GeneralRe: Stream writing html Pin
Heath Stewart17-Sep-04 6:53
protectorHeath Stewart17-Sep-04 6:53 
GeneralRe: Stream writing html Pin
Mikke_x17-Sep-04 7:14
Mikke_x17-Sep-04 7:14 
Generalposting data to another url using webclient Pin
Abraham Durairaj17-Sep-04 0:31
Abraham Durairaj17-Sep-04 0:31 
GeneralMaskEdit Pin
jzb17-Sep-04 0:09
jzb17-Sep-04 0:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.