Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# equivalent to "Not MyEnum.SomeValue"

I'm trying to convert some VB.net code to C#. I used SharpDevelop to do the heavy lifting; but the code it generated is breaking on some of the enum manipulation and I'm not sure how to fix it manually.

Original VB.net code:

Enum ePlacement
    Left = 1
    Right = 2
    Top = 4
    Bottom = 8
    TopLeft = Top Or Left
    TopRight = Top Or Right
    BottomLeft = Bottom Or Left
    BottomRight = Bottom Or Right
End Enum

Private mPlacement As ePlacement

''...

mPlacement = (mPlacement And Not ePlacement.Left) Or ePlacement.Right

generated C# code:

public enum ePlacement
{
    Left = 1,
    Right = 2,
    Top = 4,
    Bottom = 8,
    TopLeft = Top | Left,
    TopRight = Top | Right,
    BottomLeft = Bottom | Left,
    BottomRight = Bottom | Right
}

private ePlacement mPlacement;

//...

//Generates CS0023:   Operator '!' cannot be applied to operand of type 'Popup.Popup.ePlacement'
mPlacement = (mPlacement & !ePlacement.Left) | ePlacement.Right;

Resharper suggests adding the [Flags] attribute to the enum; but doing so doesn't affect the error.

like image 715
Dan Is Fiddling By Firelight Avatar asked Nov 07 '12 18:11

Dan Is Fiddling By Firelight


Video Answer


1 Answers

In VB Not is used for both logical and bitwise NOT.

In C# ! is the boolean NOT and ~ is the bitwise NOT.

So just use:

mPlacement = (mPlacement & ~ePlacement.Left) | ePlacement.Right;
like image 50
Servy Avatar answered Sep 22 '22 06:09

Servy