Q: how would you write this

G

Guest

Hello,
Here is my table.
ToolBar=1
StatusBar=2
MenuBar=4
CaptionBar=8
And, if users type 1, only ToolBar visiable, if they type 6, StatusBar and
MenuBar should be visible, and so on. I should write my code in a way that it
should work even if there are more options. What is the easiest and best way
to write to decode this and find which bars should be visible?
Thanks,
Jim.
 
G

Guest

You can loop through your list of bars and AND it with whatever the value is.
So if the value is 6 then 6 & 2 (StatusBar) you still get 2, 6 & 4 (MenuBar)
you still get 4. And so on.
 
W

William F. Robertson, Jr.

It would be easiest to use an enum with the Flags attribute.
[Flags()]
public enum bob
{
ToolBar = 1,
StatusBar = 2,
MenuBar = 4,
CaptionBar = 8
}

public static Main()
{
bob b = ( bob ) 6;

if ( ( b & bob.ToolBar ) != 0 )
Console.WriteLine( "toolbar" );
if ( ( b & bob.StatusBar ) != 0 )
Console.WriteLine( "StatusBar" );
if ( ( b & bob.MenuBar ) != 0 )
Console.WriteLine( "MenuBar" );
if ( ( b & bob.CaptionBar ) != 0 )
Console.WriteLine( "CaptionBar" );
}

Try it out.

bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top