473,382 Members | 1,225 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Flagged, typed, enumerations?

i want to have an enumeration that can also act as bit-flags, and have
values.

using System;
using System.Windows.Forms;
namespace MyProgram
{
class HelloEnumerations
{
[FlagsAttribute]
enum FlagColors : uint
{
Red = 0x01,
Green = 0x02,
Blue = 0x04,
}

public static void Main()
{
DoSomething(FlagColors.Red | FlagColors.Blue);
}

private static void DoSomething(uint theFlagColors)
{
System.Windows.Forms.MessageBox.Show(theFlagColors .ToString());
}
}
}

Problem is i get a compile error on the line:
DoSomething(FlagColors.Red | FlagColors.Blue);

saying "Argument '1': cannot convert from
'MyProgram.HelloEnumerations.FlagColors' to 'uint'"

Why can it not convert it to a uint? That's why i have the " : uint" in the
declaration of
enum FlagColors : uint

What magic keyword am i missing; and how to make it work without manually
casting to a (uint)?


Jan 5 '07 #1
12 1223
From the C# Language specification (Section 6.1.2 Implicit numeric
conversion), there are no implicit conversions for enums, except Section
6.1.3 says "An implicit enumeration conversion permits the
decimal-integer-literal 0 to be converted to any enum-type."

Details:
http://msdn.microsoft.com/library/en...harpspec_6.asp

I think you need to use a cast in your example.

-Jose
Jan 5 '07 #2
My URL only takes you to a general location in the MSDN Library. The
specific location you can look at is Development Tools and Languages >
Visual Studio .NET Visual Basic and Visual C# Reference Visual C#
Language C# Language Specification 6. Conversions, so it's a short and
sweet path :-).

From there you'll see the sections I mentioned in my previous posting.

-Jose
Jan 5 '07 #3
>What magic keyword am i missing; and how to make it work without manually
>casting to a (uint)?
Why don't you simply change the parameter type in the DoSomething
method to FlagColors? You can't avoid the cast to uint otherwise,
unless you replace the enum with something like

static class FlagColors {
public const uint Red = 0x1;
public const uint Green = 0x2;
public const uint Blue = 0x4;
}
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 5 '07 #4
Hi,

Type the argument as HelloEnumerations. That's one reason for using an Enum
in the first place as opposed to constants:

private static void DoSomething(HelloEnumerations flags)
{
...
}

IntelliSense in Visual Studio will make it much easier for you to call the
method if it accepts an Enum value instead of uint.

--
Dave Sexton
http://davesexton.com/blog

"Ian Boyd" <ad***@SWIFTPA.NETwrote in message
news:u1**************@TK2MSFTNGP03.phx.gbl...
>i want to have an enumeration that can also act as bit-flags, and have
values.

using System;
using System.Windows.Forms;
namespace MyProgram
{
class HelloEnumerations
{
[FlagsAttribute]
enum FlagColors : uint
{
Red = 0x01,
Green = 0x02,
Blue = 0x04,
}

public static void Main()
{
DoSomething(FlagColors.Red | FlagColors.Blue);
}

private static void DoSomething(uint theFlagColors)
{
System.Windows.Forms.MessageBox.Show(theFlagColors .ToString());
}
}
}

Problem is i get a compile error on the line:
DoSomething(FlagColors.Red | FlagColors.Blue);

saying "Argument '1': cannot convert from
'MyProgram.HelloEnumerations.FlagColors' to 'uint'"

Why can it not convert it to a uint? That's why i have the " : uint" in
the declaration of
enum FlagColors : uint

What magic keyword am i missing; and how to make it work without manually
casting to a (uint)?


Jan 5 '07 #5
Why don't you simply change the parameter type in the DoSomething
method to FlagColors?
Because it's not my method, it's a PInvoke.
>You can't avoid the cast to uint otherwise,
unless you replace the enum with something like

static class FlagColors {
public const uint Red = 0x1;
public const uint Green = 0x2;
public const uint Blue = 0x4;
}

So what's the point of having values next to enumeration values? They
obviously do have a numerical value associated with each one. And more
importantly, what's the point of the "uint" next to it?

To say that the compiler has no way of knowing what numerical values each
enumeration member corresponds to is just unrealistic. You mean to tell me
that after specifying "uint" and (1, 2, 4), the compiler has no information
whatsoever in the universe to figure out what uint value the enumeration
corresponds to?

The compiler obviously DOES know that FlagColors ARE uint's. i don't want
person calling to have to force a cast, the cast should obviously be done by
the compiler.
i realize i can't hope to see limitations to the C# language fixed; but at
least i can a Google record of this bug.
Jan 5 '07 #6
Try to understand; this is neither a limitation or a bug; this is the
deliberate and intentional design of the C# specification. As such, it
would be an *error* for the compiler to do this, as it doesn't comply
with the spec.
For what it is worth, I believe that zero (only) is allowed as an
automatic cast (presumably to make bitwise comparisons a lot less
verbose, since the default value would be handled separately to
compiled code), but nothing else.
And both the runtime and compiler *do* retain the knowledge of the
underlying type and value - it just doesn't allow *implicit* casting
between them. Personally I can't say that adding a cast has ever
caused me a moments trouble...

Marc
Jan 5 '07 #7
Hi Ian,

You can still take the advice in my original response. Declare even your
extern methods using an enum since the framework will automatically marshal
the underlying type.

A better design for P/Invoke anyway would be to create a managed wrapper
around it for consumers instead of them calling it directly.

--
Dave Sexton
http://davesexton.com/blog

"Ian Boyd" <ad***@SWIFTPA.NETwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Why don't you simply change the parameter type in the DoSomething
method to FlagColors?

Because it's not my method, it's a PInvoke.
>>You can't avoid the cast to uint otherwise,
unless you replace the enum with something like

static class FlagColors {
public const uint Red = 0x1;
public const uint Green = 0x2;
public const uint Blue = 0x4;
}


So what's the point of having values next to enumeration values? They
obviously do have a numerical value associated with each one. And more
importantly, what's the point of the "uint" next to it?

To say that the compiler has no way of knowing what numerical values each
enumeration member corresponds to is just unrealistic. You mean to tell me
that after specifying "uint" and (1, 2, 4), the compiler has no
information whatsoever in the universe to figure out what uint value the
enumeration corresponds to?

The compiler obviously DOES know that FlagColors ARE uint's. i don't want
person calling to have to force a cast, the cast should obviously be done
by the compiler.
i realize i can't hope to see limitations to the C# language fixed; but at
least i can a Google record of this bug.

Jan 5 '07 #8
As an alternative to casting, maybe you just need an overload here?
You have the PInvoke version that accepts a uint... so you can just
add:

public static void DoSomething(FlagColors colors) {
DoSomething((unit) colors);
}

And now you can call DoSomething(FlagColors.Red);
etc

Marc
Jan 5 '07 #9
(oh - and in line with Dave's reply of a few moments ago - you could
mark the PInvoke [uint] version as private, so only the enum version
is public)
Jan 5 '07 #10
You can still take the advice in my original response. Declare even your
extern methods using an enum since the framework will automatically
marshal the underlying type.
Really? The compiler will perform an implicit cast for me?

Well okay then. That's what i was after in the first place!

A better design for P/Invoke anyway would be to create a managed wrapper
around it for consumers instead of them calling it directly.
Just so i understand what you're saying:

i write a class with "prettier" methods; and those methods in turn do the
"ugly" P/Invoke? And either way i'm still writing the code to do the
PInvoke, except that the buck stops here, as it were?
Jan 6 '07 #11
(oh - and in line with Dave's reply of a few moments ago - you could mark
the PInvoke [uint] version as private, so only the enum version is public)
i like all the ideas.

ty all vm
Jan 6 '07 #12
Hi Ian,
>You can still take the advice in my original response. Declare even your
extern methods using an enum since the framework will automatically
marshal the underlying type.

Really? The compiler will perform an implicit cast for me?

Well okay then. That's what i was after in the first place!
Really! :)

Although the compiler isn't performing any cast, it's the Marshal class that
does all the work at runtime, AFAIK.
>A better design for P/Invoke anyway would be to create a managed wrapper
around it for consumers instead of them calling it directly.

Just so i understand what you're saying:

i write a class with "prettier" methods; and those methods in turn do the
"ugly" P/Invoke? And either way i'm still writing the code to do the
PInvoke, except that the buck stops here, as it were?
Yep.

Take a look at the source code for the Framework Class Libraries using a
tool like ILDasm or .NET Reflector. You'll see that a good portion of the
framework is really just P/Invoke wrappers. Take a look at the
System.Windows.Forms assemby, for instance.

--
Dave Sexton
http://davesexton.com/blog
Jan 6 '07 #13

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

Similar topics

0
by: Plinkerton | last post by:
I'm making an Base Class that will be inherited. In my base class, I have a public enumeration that defines a list of things I want my class to be able to do. I use it for Method input...
3
by: Norma | last post by:
I am trying to capture data in a date parameter query that spans from 1 to 3 months. I have a field that is a checkbox that notes whether or not this data is 'red flagged' (so you know, the red...
1
by: someone else | last post by:
I have some code that creates dynamic enumerations for use in a PropertyGrid control. This all works perfectly but the memory usage of the program increases quite quicly when viewing the...
1
by: Oleg Ogurok | last post by:
Hi all, I've added a new DataSet (xsd file) to my project in VS.NET 2003. There I create a simple type as an enumeration of values. <xs:simpleType name="MyCustomType"> <xs:restriction...
4
by: ChrisB | last post by:
Hello: I will be creating 50+ enumerations related to a large number of classes that span a number of namespaces. I was wondering if there are any "best practices" when defining enumerations. ...
1
by: Chuck Bowling | last post by:
I'm creating a highlighting text control and I want to implement highlighting using a typed dataset. My question is; is it possible to map Color enumerations directly to XML so that I don't have...
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.