1

I am trying to set an integer variable to a combination of an enum values, the enum is:

public enum WebBrowserDownloadControlFlags : uint { DLIMAGES = 0x00000010, VIDEOS = 0x00000020, BGSOUNDS = 0x00000040, NO_SCRIPTS = 0x00000080, NO_JAVA = 0x00000100, NO_RUNACTIVEXCTLS = 0x00000200, NO_DLACTIVEXCTLS = 0x00000400, DOWNLOADONLY = 0x00000800, NO_FRAMEDOWNLOAD = 0x00001000, RESYNCHRONIZE = 0x00002000, PRAGMA_NO_CACHE = 0x00004000, NO_BEHAVIORS = 0x00008000, NO_METACHARSET = 0x00010000, URL_ENCODING_DISABLE_UTF8 = 0x00020000, URL_ENCODING_ENABLE_UTF8 = 0x00040000, NOFRAMES = 0x00080000, FORCEOFFLINE = 0x10000000, NO_CLIENTPULL = 0x20000000, SILENT = 0x40000000, OFFLINEIFNOTCONNECTED = 0x80000000, OFFLINE = OFFLINEIFNOTCONNECTED, } 

The DownloadControlFlags is an integer field:

 public int DownloadControlFlags { get { return _downloadControlFlags; } set { if (_downloadControlFlags == value) return; _downloadControlFlags = value; IOleControl ctl = (IOleControl)ActiveXInstance; ctl.OnAmbientPropertyChange(DISPID_AMBIENT_DLCONTROL); } } 

To set this variable I used summation as :

 webBrowser1.DownloadControlFlags = (int)WebBrowserDownloadControlFlags.DLIMAGES + (int)WebBrowserDownloadControlFlags.NOFRAMES + (int)WebBrowserDownloadControlFlags.NO_FRAMEDOWNLOAD + (int)WebBrowserDownloadControlFlags.NO_JAVA + (int)WebBrowserDownloadControlFlags.NO_DLACTIVEXCTLS + (int)WebBrowserDownloadControlFlags.NO_BEHAVIORS + (int)WebBrowserDownloadControlFlags.NO_RUNACTIVEXCTLS + (int)WebBrowserDownloadControlFlags.SILENT; 

I am not sure if it is the correct way, but it seems that it works. But now I want to check if the variable contains an specific value or not. For normal enum the solution could be the answer of this question How to check if any flags of a flag combination are set?, but I am not sure if I can use the same way in my case.

How can I check if a specific flag has been set or not?! for example if the DownloadControlFlags contains WebBrowserDownloadControlFlags.NO_JAVA or not!

1
  • You can convert the int to an enum, and check the flags on it: WebBrowserDownloadControlFlags foo = (WebBrowserDownloadControlFlags)yourInt; Commented Dec 29, 2015 at 18:23

1 Answer 1

3

For normal enum the solution could be the answer of this question How to check if any flags of a flag combination are set?, but I am not sure if I can use the same way in my case.

Have you tried it?

int flags = (int)WebBrowserDownloadControlFlags.DLIMAGES + (int)WebBrowserDownloadControlFlags.NOFRAMES + (int)WebBrowserDownloadControlFlags.NO_FRAMEDOWNLOAD + (int)WebBrowserDownloadControlFlags.NO_JAVA + (int)WebBrowserDownloadControlFlags.NO_DLACTIVEXCTLS + (int)WebBrowserDownloadControlFlags.NO_BEHAVIORS + (int)WebBrowserDownloadControlFlags.NO_RUNACTIVEXCTLS + (int)WebBrowserDownloadControlFlags.SILENT; 

You can either use Enum.HasFlag by casting it back to your enum type:

Console.WriteLine(((WebBrowserDownloadControlFlags)flags).HasFlag(WebBrowserDownloadControlFlags.NOFRAMES)); // true Console.WriteLine(((WebBrowserDownloadControlFlags)flags).HasFlag(WebBrowserDownloadControlFlags.VIDEOS)); // false 

Or you can cast the enum flag you want to compare against into an int and then make a bit-wise comparison:

Console.WriteLine((flags & (int)WebBrowserDownloadControlFlags.NO_JAVA) != 0); // true Console.WriteLine((flags & (int)WebBrowserDownloadControlFlags.VIDEOS) != 0); // false 

But you should really consider whether you actually don’t want to store the flags internally as a WebBrowserDownloadControlFlags type. There is no reason to convert it into an int early. And you could just as well do the following:

WebBrowserDownloadControlFlags flags = WebBrowserDownloadControlFlags.DLIMAGES | WebBrowserDownloadControlFlags.NOFRAMES | WebBrowserDownloadControlFlags.NO_FRAMEDOWNLOAD | WebBrowserDownloadControlFlags.NO_JAVA | WebBrowserDownloadControlFlags.NO_DLACTIVEXCTLS | WebBrowserDownloadControlFlags.NO_BEHAVIORS | WebBrowserDownloadControlFlags.NO_RUNACTIVEXCTLS | WebBrowserDownloadControlFlags.SILENT; Console.WriteLine(flags.HasFlag(WebBrowserDownloadControlFlags.NOFRAMES)); // true Console.WriteLine(flags.HasFlag(WebBrowserDownloadControlFlags.VIDEOS)); // false Console.WriteLine((flags & WebBrowserDownloadControlFlags.NO_JAVA) != 0); // true Console.WriteLine((flags & WebBrowserDownloadControlFlags.VIDEOS) != 0); // false 

And whenever you really need an int value, you can just convert the whole thing into an int:

Console.WriteLine((int)flags); // 1074304784 
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much, as I used a code in which they defined the variable as integer, I wasn't sure how to change their code!
To answer your question, yes I tried it and & and | operators are not applicable on integers.
Yes, they are. They are bitwise operators and work fine on integers (as shown in the second example above).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.