|
|
Unisgned Integers And Global Cases In C# | ||
Discussion by xarzu with 2 Replies.
Last Update: January 10, 2009, 7:23 am | |||
![]() |
|
|
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 2
UINT state_002 4
UINT state_003 8
UINT state_004 16
Then 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#?
CODE
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
CODE
[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:-
CODE
MyType var = MyType.State1 | MyType.State3;You can even keep adding flags to the variable like this.
CODE
MyType var = MyType.State1;var |= MyType.State3;
var |= MyType.State4;
To check if a given flag is present in the variable, use this:-
CODE
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 ^.
CODE
var = var ^ MyType.State3;or in one go:-
CODE
var ^= MyType.State3;One last thing I must mention is that you can also create flag groups while delcaring the enum:-
CODE
[Flags]enum MyType : uint
{
State1 = 1,
State2 = 2,
State3 = 4,
State4 = 8,
State5 = State1 | State 2, // Combination of State1 & State2
StateAll = State3 | State4 | State5, // Combination of all prior States
StateAny = 1 | 2 | 4 | 8 // Same thing as above by directly using the values
}
Similar Topics:
Global Warming Countermeasures
Global Warming global Cooling Cli...
Global Warming - Global Catastrophe
Linq Overview (5)
|
(1) C# Socket Programming Help Required
|
HOME 





Global Concept 1.0 - Positive Integers and Non-integers (Indian Accent)
Global Concept 1.0 - Positive Integers and Non-integers (Hindi)
Seventh Grade Math Chapter One - Topic 3: Integer Addition (Part 1)

