I've looked at similar questions but they don't seem to be dealing with the same issue I'm having.
I have an enum for CRUD permission testing defined as
public enum CRUDOperation
{
NotNeeded = 0,
Read = 1,
Create = 2,
Update = 4,
Delete = 8
}
I didn't write this next part of code which is why I might be confused, but then the CRUD permission values associated with a role are put into a "Restrictions" string in the following manner.
Restrictions = "";
foreach (string inCrud in restrictions)
{
string crud = inCrud.ToUpper();
int res = 0;
if (crud.Contains('C'))
res |= (char)CRUDOperation.Create;
if (crud.Contains('R'))
res |= (char)CRUDOperation.Read;
if (crud.Contains('U'))
res |= (char)CRUDOperation.Update;
if (crud.Contains('D'))
res |= (char)CRUDOperation.Delete;
Restrictions += (char)res;
}
And now I need to parse through this "Restrictions" string to check if a given role has the appropriate CRUDOperation permission levels. However the string is full of random ascii characters and I'm just having a hard time understanding how to do this correctly. I haven't worked with bitmasks really at all before. I'm trying to do this;
CRUDOperation operation = (CRUDOperation)Enum.Parse(typeof(CRUDOperation), p.Restrictions);
but i'm getting the error "Requested value ' ' was not found."
Does anyone have any advice?
Your problem in that code is that when you do this:
Restrictions += (char)res;
you are actually adding the character whose code is the result of your operation. The resulting Restrictions string can't be parsed as an enum.I'm not sure why Restrictions is kept as a string. I would change the type of Restrictions to CRUDOperation and replace the first bit of code with this:
This way you have the result directly.
If for some reason you have to have a string with the result you need to use a separator for the values you are adding:
Restrictions += " " + (int)res;
After that you can parse the string by splitting it and then parsing it (make sure to remove any empty entries before parsing).