Q: how would you write this

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
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.
 
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
 
Back
Top