Click here to Skip to main content
15,904,346 members
Home / Discussions / C#
   

C#

 
GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Vikram A Punathambekar10-Jul-07 4:30
Vikram A Punathambekar10-Jul-07 4:30 
GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Martin#10-Jul-07 4:39
Martin#10-Jul-07 4:39 
AnswerRe: unable to cast object of type 'X' to type 'X' Pin
AFSEKI10-Jul-07 6:31
AFSEKI10-Jul-07 6:31 
QuestionSend a string to another program Pin
suntromantralalalala9-Jul-07 5:08
suntromantralalalala9-Jul-07 5:08 
AnswerRe: Send a string to another program Pin
Luc Pattyn9-Jul-07 6:51
sitebuilderLuc Pattyn9-Jul-07 6:51 
QuestionIterating through all values of an ORed enum variable... Pin
Shy Agam9-Jul-07 5:05
Shy Agam9-Jul-07 5:05 
AnswerRe: Iterating through all values of an ORed enum variable... Pin
Martin#9-Jul-07 5:39
Martin#9-Jul-07 5:39 
AnswerRe: Iterating through all values of an ORed enum variable... Pin
Luc Pattyn9-Jul-07 7:08
sitebuilderLuc Pattyn9-Jul-07 7:08 
Hi,

first of all I hope your enum behaves as a flags collection (and hence
it better have the [Flags] attribute).

There is a very nice trick to enumerate the individual bits in an int
without using a loop that checks each and every bit:

int work=val;  // copy the int whose bits need to be enumerated
while (work!=0) {
    int singleBit=work & -work; // this has the lowest bit of work set
    work=work^singleBit;        // removes work's lowest bit set
    ... do whatever you need to do with singleBit
}


Applied to enums, this leads to:

[Flags]
enum myFlags {a=1, b=2, c=4, d=8}
 
public void Run() {
	myFlags flags=myFlags.a | myFlags.c | myFlags.d;
	int work=(int)flags;  // copy the int whose bits need to be enumerated
	while (work!=0) {
	    int singleBit=work & -work;   // this has the lowest bit of work set
		work=work^singleBit;      // removes work's lowest bit set
		myFlags mySingleFlag=(myFlags)singleBit;
		log(mySingleFlag.ToString());
	}
}


this little example prints the characters a, c and d as it should.

Smile | :)



GeneralRe: Iterating through all values of an ORed enum variable... Pin
PhilDanger9-Jul-07 7:20
PhilDanger9-Jul-07 7:20 
GeneralRe: Iterating through all values of an ORed enum variable... Pin
Shy Agam9-Jul-07 8:12
Shy Agam9-Jul-07 8:12 
GeneralRe: Iterating through all values of an ORed enum variable... Pin
Luc Pattyn9-Jul-07 8:20
sitebuilderLuc Pattyn9-Jul-07 8:20 
GeneralRe: Iterating through all values of an ORed enum variable... Pin
Luc Pattyn9-Jul-07 8:44
sitebuilderLuc Pattyn9-Jul-07 8:44 
GeneralRe: Iterating through all values of an ORed enum variable... Pin
Shy Agam9-Jul-07 8:51
Shy Agam9-Jul-07 8:51 
GeneralRe: Iterating through all values of an ORed enum variable... Pin
Martin#9-Jul-07 9:06
Martin#9-Jul-07 9:06 
GeneralRe: Iterating through all values of an ORed enum variable... Pin
Luc Pattyn9-Jul-07 9:18
sitebuilderLuc Pattyn9-Jul-07 9:18 
GeneralRe: Iterating through all values of an ORed enum variable... Pin
Luc Pattyn9-Jul-07 9:27
sitebuilderLuc Pattyn9-Jul-07 9:27 
GeneralRe: Iterating through all values of an ORed enum variable... Pin
Martin#9-Jul-07 9:54
Martin#9-Jul-07 9:54 
AnswerRe: Iterating through all values of an ORed enum variable... Pin
PhilDanger9-Jul-07 7:17
PhilDanger9-Jul-07 7:17 
GeneralRe: Iterating through all values of an ORed enum variable... Pin
Shy Agam9-Jul-07 8:02
Shy Agam9-Jul-07 8:02 
AnswerRe: Iterating through all values of an ORed enum variable... Pin
PhilDanger9-Jul-07 10:55
PhilDanger9-Jul-07 10:55 
QuestionHow to play at the same time 2 or more sounds Pin
mayhem709-Jul-07 4:58
mayhem709-Jul-07 4:58 
AnswerRe: How to play at the same time 2 or more sounds Pin
mav.northwind9-Jul-07 19:21
mav.northwind9-Jul-07 19:21 
AnswerRe: How to play at the same time 2 or more sounds Pin
Hesham Yassin9-Jul-07 22:38
Hesham Yassin9-Jul-07 22:38 
Questionhow to sort datagridview ? (a numeric field) Pin
cmpeng349-Jul-07 4:53
cmpeng349-Jul-07 4:53 
AnswerRe: how to sort datagridview ? (a numeric field) Pin
velkropie9-Jul-07 6:24
velkropie9-Jul-07 6:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.