Question

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.

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top