Jump to content
xisto Community
xarzu

Unisgned Integers And Global Cases In C#

Recommended Posts

I am used to C++ more than C#.In C++ you have header files and you have the ability to declare states. For example, you can declare a series of unsigned integers to be for a variety of different states to later use in switch statements or if-then statements.Since C# does not have the same sort of structure with header files, how would I impliment a simular functionality in C#?The reason why I want to use UINT is because you can do that super cool bit-wise and and or with them. Remember those good ol' days? You could define four different conditions like this:UINT state_001 2UINT state_002 4UINT state_003 8UINT state_004 16Then a variable can be any one state or any combination of states. To assign a variable a particular state, you do a bitwise and to the variable. To see if the variable was set to any of the states, you do a bitwise or.How would that look like in Visual C#?

Share this post


Link to post
Share on other sites

Couldn't you just create a thing like this (I forgot what's it called):

private Unsigned {  int var1 = 0, var2 = 2, var3 = 4;}

and just call this everytime you need it globally? something like structs in C?

btw, the code above is Java but really rough (note, unsigned integers don't exist in Java afaik).

xboxrulz

Share this post


Link to post
Share on other sites

I think I understand what you are trying to accomplish. The general term for this is flags. It can be easily created using enums in C#.

[Flags]enum MyType : uint{State1 = 1,State2 = 2,State3 = 4,State4 = 8}

MSDN states that that the Flag attribute should be applied to an enum which is to be used as a flag. Although, it'll work even if you don't use it. Also, you don't need to have the enum store the values as unsigned integers, inorder for this to work. But, its better to restrict negative values. I am not sure if doubling negative values will work or not.

When using this data type you can assign flags to the variable and concatenate multiple states by using the bitwise OR operator |

For example:-

MyType var = MyType.State1 | MyType.State3;

You can even keep adding flags to the variable like this.

MyType var = MyType.State1;var |= MyType.State3;var |= MyType.State4;

To check if a given flag is present in the variable, use this:-

if ((var & MyType.State3) == MyType.State3){Console.WriteLine("State 3 exists in var");}else{Console.WriteLine("State 3 does not exist in var");}

To remove a flag use the XOR operator ^.

var = var ^ MyType.State3;

or in one go:-

var ^= MyType.State3;

One last thing I must mention is that you can also create flag groups while delcaring the enum:-

[Flags]enum MyType : uint{State1 = 1,State2 = 2,State3 = 4,State4 = 8,State5 = State1 | State 2, // Combination of State1 & State2StateAll = State3 | State4 | State5, // Combination of all prior StatesStateAny = 1 | 2 | 4 | 8 // Same thing as above by directly using the values}

Edited by turbopowerdmaxsteel (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.