473,799 Members | 3,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can I cast to a [Flags] enum?


I have an enum variable. Is there some way to cast an int or BitArray
and get my enum value? I have to communicate with an old C-style
sever so I get an array of bits("0","1"). But I'm using an enum
variable to store the data.

[Flags]
public enum LaneCapabilitie s
{
MLT = 0,
ACM = 1,
AVI = 2,
ATIM = 4,
Degraded = 8,
CCS = 16
}

Ended up with this code:

string[] flags = ReadRTMessage() .Split(' ');

LaneCapabilitie s capable = new LaneCapabilitie s();
for( int i = 0; i<flags.Length ; i++)
{
switch(i)
{
case 0 :
if (flags[0] == "1")
capable |= LaneCapabilitie s.ACM;
break;
case 1 :
if (flags[1] == "1")
capable |= LaneCapabilitie s.ATIM;
break;
case 2:
if (flags[2] == "1")
capable |= LaneCapabilitie s.AVI;
break;
case 3:
if (flags[3] == "1")
capable |= LaneCapabilitie s.CCS;
break;
case 4:
if (flags[1] == "1")
capable |= LaneCapabilitie s.Degraded;
break;
case 5:
if (flags[1] == "5")
capable |= LaneCapabilitie s.MLT;
break;
}
}

Seems like a lot of work for a .NET app. Any suggestions?

Nov 16 '05 #1
1 10557
Dw***********@t ranscore.com wrote in
news:11******** **************@ z14g2000cwz.goo glegroups.com:

I have an enum variable. Is there some way to cast an int or
BitArray and get my enum value? I have to communicate with an
old C-style sever so I get an array of bits("0","1").


Dwight,

The underlying type of an enumeration (if the type isn't explicitly
specified) is an integer (System.Int32). This means that enumeration
values and integer values are interchangeable via casting:

LaneCapabilitie s capable = (LaneCapabiliti es) 12;

// capable.ATIM and capable.Degrade d are "set".

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
4607
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
2
5392
by: babylon | last post by:
I have an enum public enum MyEnum : int { X, Y } I have to do int a = (int) MyEnum.X; can i overload the operator or other means to do something like
1
4168
by: todorov-fkt | last post by:
Hello everyone, I have a field which is 1 byte long and is used to store different flags (according to specs) - it is the flags bit in the ID3 tag header. So 0xabc00000 represents the byte, where only bits a,b and c have any meaning, the other bits are always 0. I also have the following: enum ID3_Header_Flags { NoFlag = 0x00000000, FlagC = 0x00100000, FlagB = 0x01000000,
18
11372
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
4
6076
by: John Salerno | last post by:
I created this enumeration: enum Status { Off = 0, Red = 1, Yellow = 2, Blue = 4, Overload = 8
4
5055
by: Joel Moore | last post by:
Say I have an enum similar to this: <Flags()> Enum TestFlags As Short One = 0 Two = 1 Three = 2 Four = 4 End Enum I figured I could declare variable of type TestFlags to be used in the
2
4224
by: Flo | last post by:
I have the problem, that the following Code produces neither warning nor error at compile time, at least not with the Microsoft Visual C++ 6.0 compiler (warning level 4) enum E { eOne, eTwo }; struct S { void fooDouble( double ) {}; void fooEnum( E ) {}; }; int main() {
34
11206
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
0
1212
by: Olivier J | last post by:
This is actually an answer to an old post which is closed. But it may nevertheless be useful to someone. Old Post: Has if () syntax of C# no IN operator like Pascal or SQL (IN, LIKE,BETWEEN)? Good, short and fast possibility is to define the enum as flags, like e.g.: internal enum TokenType {
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.