Nov 22, 2009

Unisgned Integers And Global Cases In C#

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > C# .NET

Unisgned Integers And Global Cases In C#

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


Comment/Reply (w/o sign-up)

xboxrulz
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

Comment/Reply (w/o sign-up)

turbopowerdmaxsteel
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
}

 

 

 


Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Unisgned Integers Global Cases


    Looking for Unisgned, Integers, And, Global, Cases, In, C#

See Also,

*SIMILAR VIDEOS*
Searching Video's for Unisgned, Integers, And, Global, Cases, In, C#
advertisement



Unisgned Integers And Global Cases In C#

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com