Binary "and" operator in enum loop

  • Thread starter Thread starter Peter Hemmingsen
  • Start date Start date
P

Peter Hemmingsen

Hi,

I'm trying to write a function taking an "flagged" enum as parameter. The
function should then loop through the enum and do some stuff for each flag
that are set. Below is some sample code showing what I'm trying to do. The
problem is in the line:

if (val->GetValue(i) & it)

as there is no binary and operator on the object returned by GetValue. I've
tried to cast the GetValue return type to a InfoTypes type but that fails as
well.

Any suggestion would be highly appreciated,- thanks.

Peter


SAMPLE CODE:

[Flags] public __value enum InfoTypes
{p=0x80000000,s=0x40000000,t=0x20000000};

InfoTypes it =p & s;
DoStuff (it);
....

void DoStuff (InfoTypes it) {
Type* ty=__typeof(InfoTypes);
Array *val=Enum::GetValues(ty);

for (i=0;i<val->Length();i++) {
if (val->GetValue(i) & it)
DoSomeThing();
}
}
 
Hi Peter,

Thanks for you posting in the group!
InfoTypes it =p & s;
I think it maybe *InfoTypes it =p | s;*

Below is some sample code showing what I'm trying to do. The
problem is in the line:

if (val->GetValue(i) & it)

Try this way:

Int32 compVal;
for (i=0;i<val->Length();i++) {
compVal = *(dynamic_cast<System::Int32*>(val->GetValue(i)));
if(compVal & it)
DoSomeThing();
}


Hope it helps!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
Hi Peter,

Another .NET choice:
compVal = Convert::ToInt32(val->GetValue(i));


Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
Back
Top