473,385 Members | 1,645 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,385 software developers and data experts.

Is this possible

Hi there,

Is it possible to write the following function as a template where
"EnumFlags" becomes template parameter T (where T will always be an
enumerator with the "FlagsAttribute"). Nothing I've tried works, including
use of a "where" clause since T must always be of type "System.Enum". Thanks
in advance.

void SetFlag(EnumFlags Flag, ref EnumFlags Flags, bool On)
{
if (On)
{
Flags |= Flag;
}
else
{
Flags &= ~Flag;
}
}
Oct 20 '06 #1
4 2370
A pain indeed... unfortunately AFAIK there is no easy route out;
generics doesn't support enums in this way... you could cast to the
underlying type, but then you make assumptions about the specific type
used (int, short, etc). Without treating as an int etc you can't use
the static binary operators...

Sorry,

Marc

Oct 20 '06 #2
"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>A pain indeed... unfortunately AFAIK there is no easy route out;
generics doesn't support enums in this way... you could cast to the
underlying type, but then you make assumptions about the specific type
used (int, short, etc). Without treating as an int etc you can't use
the static binary operators...
Thanks for the confirmation. I didn't think so but casting is an ugly option
I wanted to avoid (notwithstanding the type issue). I'll think it's cleaner
to just replicate the same function over and over again as required (given
that I have little choice). Anyway, thanks again.
Oct 20 '06 #3
Something like the following should work. There still is casting
involved when calling the method, but you can take advantage of
generics for the return value. So you wouldn't need the method for
every enum, just every integral type.
static T SwapEnumFlag<T>(int flags, int flag) where T : struct
{
Debug.Assert(typeof(T).IsEnum, "T must be an enum");
Debug.Assert(typeof(T).IsDefined(typeof(FlagsAttri bute), false), "T
must have the System.FlagsAttribute applied to it.");

if ((flags & flag) == flag)
flags &= ~flag;
else
flags |= flag;

return (T)Enum.ToObject(typeof(T), flags);
}
FlaggedEnum flagged = FlaggedEnum.Red | FlaggedEnum.Blue;
Debug.WriteLine(flagged); // output: Red, Blue

flagged = SwapEnumFlag<FlaggedEnum>((int)flagged,
(int)FlaggedEnum.Red);
Debug.WriteLine(flagged); // output: Blue

flagged = SwapEnumFlag<FlaggedEnum>((int)flagged,
(int)FlaggedEnum.Red);
Debug.WriteLine(flagged); // output: Red, Blue
Jack Robertson wrote:
Hi there,

Is it possible to write the following function as a template where
"EnumFlags" becomes template parameter T (where T will always be an
enumerator with the "FlagsAttribute"). Nothing I've tried works, including
use of a "where" clause since T must always be of type "System.Enum". Thanks
in advance.

void SetFlag(EnumFlags Flag, ref EnumFlags Flags, bool On)
{
if (On)
{
Flags |= Flag;
}
else
{
Flags &= ~Flag;
}
}
Oct 21 '06 #4
Something like the following should work. There still is casting
involved when calling the method, but you can take advantage of
generics for the return value. So you wouldn't need the method for
every enum, just every integral type.
static T SwapEnumFlag<T>(int flags, int flag) where T : struct
{
Debug.Assert(typeof(T).IsEnum, "T must be an enum");
Debug.Assert(typeof(T).IsDefined(typeof(FlagsAttri bute), false), "T
must have the System.FlagsAttribute applied to it.");

if ((flags & flag) == flag)
flags &= ~flag;
else
flags |= flag;

return (T)Enum.ToObject(typeof(T), flags);
}
FlaggedEnum flagged = FlaggedEnum.Red | FlaggedEnum.Blue;
Debug.WriteLine(flagged); // output: Red, Blue

flagged = SwapEnumFlag<FlaggedEnum>((int)flagged,
(int)FlaggedEnum.Red);
Debug.WriteLine(flagged); // output: Blue

flagged = SwapEnumFlag<FlaggedEnum>((int)flagged,
(int)FlaggedEnum.Red);
Debug.WriteLine(flagged); // output: Red, Blue
Thanks very much. Though I think casting is ugly and I normally try to avoid
it, I'll take a look at this in greater detail. I might use it as is or
leverage it in some way.Your effort is certainly appreciated. Thanks again.
Oct 23 '06 #5

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

Similar topics

4
by: Julia Briggs | last post by:
I am struggling to create a PHP function that would take a specified image (JPG, GIF or PNG) from a link, and resize it down to a thumbnail so it will always fit in a 200x250 space. I am hoping...
36
by: rbt | last post by:
Say I have a list that has 3 letters in it: I want to print all the possible 4 digit combinations of those 3 letters: 4^3 = 64 aaaa
20
by: CHIN | last post by:
Hi all.. here s my problem ( maybe some of you saw me on other groups, but i cant find the solution !! ) I have to upload a file to an external site, so, i made a .vbs file , that logins to...
7
by: Andrzej | last post by:
Is it possible to call a function which name is given by a string? Let assume that I created a program which call some functions for example void f1(void), void f2(void), void f3(void). ...
2
by: Bhupesh Naik | last post by:
This is a query regarding my problem to make a spell and grammar check possible in text area of a web page. We have aspx pages which are used to construct letters. The browser based screens...
1
by: AAA | last post by:
hi, I'll explain fastly the program that i'm doing.. the computer asks me to enter the cardinal of a set X ( called "dimX" type integer)where X is a table of one dimension and then to fill it...
25
by: Piotr Nowak | last post by:
Hi, Say i have a server process which listens for some changes in database. When a change occurs i want to refresh my page in browser by notyfinig it. I do not want to refresh my page i.e....
4
by: RSH | last post by:
Okay my math skills aren't waht they used to be... With that being said what Im trying to do is create a matrix that given x number of columns, and y number of possible values i want to generate...
7
by: Robert S. | last post by:
Searching some time now for documents on this but still did not find anything about it: Is it possible to replace the entry screen of MS Office Access 2007 - that one presenting that default...
14
by: bjorklund.emil | last post by:
Hello pythonistas. I'm a newbie to pretty much both programming and Python. I have a task that involves writing a test script for every possible combination of preference settings for a software...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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

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.