Loading...


bookmark - Unisgned Integers And Global Cases In C#

Unisgned Integers And Global Cases In C#

 
 Discussion by xarzu with 2 Replies.
 Last Update: January 10, 2009, 7:23 am
 
bookmark - Unisgned Integers And Global Cases In C#  
Quickly Post to Unisgned Integers And Global Cases In C#  w/o signup Share Info about Unisgned Integers And Global Cases In C#  using Facebook, Twitter etc. email your friend about Unisgned Integers And Global Cases In C# Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

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 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#?

   Fri Jan 9, 2009    Reply         

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

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

   Sat Jan 10, 2009    Reply         

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#.

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
}

   Sat Jan 10, 2009    Reply         


Quickly Post to Unisgned Integers And Global Cases In C#  w/o signup Share Info about Unisgned Integers And Global Cases In C#  using Facebook, Twitter etc. email your friend about Unisgned Integers And Global Cases In C# Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

Similar Topics:

Global Warming Countermeasures

Okay, I was reading this article at Popular science and found it pretty interesting. It demonstrates with the various ways that are used to/experimented on to counteract global warming, let me give a brief explanat ...more

   17-Jul-2005    Reply         

Global Warming global Cooling Cli...

Global warming is at presented lately by some expecially Al Gore (who tried to become president three times and lost every time) who failed his science courses in college are tell us that driving cars, burning charoal to grill hamburger or hotdogs, cutting grass with a gas powered lawn mower using f ...more

   24-Jun-2006    Reply         

Global Warming - Global Catastrophe

Global Warming, it's a major deal. But, is Global Warming really what scientists say it is? Could it really happen? Speak your mind about it. Are us humans adding to the destruction that Global Warming may do to our planet? In 20 years, could our world look as in the movie "The Day Afte ...more

   09-Jul-2007    Reply         

Linq Overview    Linq Overview (5) (1) C# Socket Programming Help Required  C# Socket Programming Help Required