I have an enum and a usercontrol, both in the same assembly (a plain .NET 4 web site).
In the Constants class:
public Enum CrudOperations
Add
Edit
Delete
This controls the columns in a GridView on a UserControl via a property on the UserControl
Public Property Mode() As CrudOperations
Get
Return [Enum].Parse(GetType(CrudOperations), If(ViewState.Item("Mode"), "0"), True)
End Get
Set(ByVal value As CrudOperations)
ViewState.Item("Mode") = value
grdItems.Columns(3).Visible = ((value Or CrudOperations.Add) = CrudOperations.Add)
grdItems.Columns(4).Visible = ((value Or CrudOperations.Edit) = CrudOperations.Edit)
End Set
End Property
In C#, I’ve specified the columns to show with markup as Mode="Edit,Delete", but in VB.NET, this does nothing. The only way I can get anything to show is with the codebehind, but if on the containing page I use userGrid.Mode = CrudOperations.Edit And CrudOperations.Delete, I get all the columns (there’s also a delete column), but userGrid.Mode = CrudOperations.Edit Or CrudOperations.Delete shows nothing.
Is there a way to do the C# equivalent?
You need to use the
Flagsattribute where an enumeration can be treated as a bit field.C#:
VB.NET: