Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am trying to have an AND done between two numbers...

I expose the whole problem.

I have a ToolBar with some buttons. The buttons are by itself indexed.

My function must enable/disable the buttons due to the numerical value which I pass to him.

The value is one and contains all the buttons to be enabled, it is a series of bits converted in decimal.

The AND must be done between this value and a mask of bits, which changes at second of the button which I am verifying.

If returns the masks ( in decimal ) then the button will be enabled, otherwise it will be disabled.

Afterwards the code:

 

public void abilita(long mint)
 {
   for(int i=0;i<toolbarMain.Buttons.Count;i++)
   {
     if((mint && Math.Pow(2,i))==Math.Pow(2,i))
     {
       toolbarMain.Buttons[i].Enabled = true;
     }
     else
     {
       toolbarMain.Buttons[i].Enabled = false;
     }
   }
 }
 

 

The C# does not accept that the AND is done between two numbers, of any kind ... Is it normal?

Posted

Two things:

 

1. You want to use the bitwise & operator, not the logical && operator.

 

2. Math.Pow() returns a double, so you'll have to cast the result to long to do the ANDing.

 

Like this:

 

   public void abilita(long mint)
   {
       for (int i = 0; i < toolbarMain.Buttons.Count; i++)
       {
           toolbarMain.Buttons[i].Enabled =
               (mint & (long) Math.Pow(2, i)) == (long) Math.Pow(2, i);
       }
   }

 

Having said that, I wouldn't use Pow() in this situation. It gets very long-winded, looking at that; and all those method calls can't be good. I'd have thought something like this would be a good solution:

 

   public void abilita(long mint)
   {
       for (int i = 0; i < toolbarMain.Buttons.Count; i++)
           toolbarMain.Buttons[i].Enabled = (mint & 1 << i) != 0;
   }

  • *Experts*
Posted

Optimization is great, but readability counts for a lot! I'm not sure how you're deciding what buttons to enable/disable, but you might want to consider using an enum for the buttons. An enum can be specified with a [Flags] attribute which is ideal for things like this.

 

Here's some sample code with a new version of your abilita function:

[Flags]
public enum ToolbarEnum
{
NoButton = 0,
Button1 = 1,
Button2 = 2,
Button3 = 4,
Button4 = 8,
Button5 = 16,
Button6 = 32
}


public void abilita(ToolbarEnum enabledButtons)
{
long mint = (long)enabledButtons;
for (int i = 0; i < toolbarMain.Buttons.Count; i++)
	toolbarMain.Buttons[i].Enabled = (mint & (1 << i)) != 0;
}


// Here's how you would call the new function
// to enable buttons 1, 2 and 5:
abilita(ToolbarEnum.Button1 | ToolbarEnum.Button2 | ToolbarEnum.Button5);

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Yes, it's ok...

But my main goal is to remember the status of a toolbar and a mainmenu with another function (that already have) then passing the result to abilita. So it's not very important to have an enumerator. But it's good example for another thing i have to do, thank u :-)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...