Dave Burke : Freelance .NET Web Developer specializing in Online Communities

Code Diddy of the Day: Determining Odd or Even Integer

I need to post some raw .NET code every so often or I get constipated.  Actually, I've not been blogging or posting much code lately because I've been banging out so much code for The Man.  Ironic.

Here's a little diddy to determine if an integer is odd or even.  The scenario, we're painting a row of a Windows Form control and want to alternate row colors between lavender (my favorite) and white.  The [e.Index & 1 == 0] does the trick in CSharp bristle fashion.

protected override void OnDrawItem(DrawItemEventArgs e, StreamsDataSet.dtResourcesRow dr)
{
    Brush brushBack = (e.Index & 1) == 0  ? Brushes.Lavender : SystemBrushes.Window;
    .....
}

Technorati Tags:

Comments (7) | Post RSS RSS comment feed

Posted on 2/6/2006 8:10:00 PM by Dave Burke
Categories:
Tags:

Related posts

Comments (7) -

2/6/2006 8:56:24 PM Permalink

I always thought green was your favourite.

Tim Haines |

2/6/2006 10:31:59 PM Permalink

That's very cool. I would have gone for the more traditional approach of "modulo 2" ... I wouldn't have considered bitwise-and.

And yeah, I think I'd go with green, too, Tim. Smile

mabster |

2/7/2006 4:37:55 AM Permalink

How excellent!  Hearing from two of my favorite New Zealanders on the same post!  Yeah, green would be a good call.  I live in what is called the Green Mountain State, and since my brand is "Dave Burke: Live From Vermont" I went with the green.  I only said Lavender was my favorite here for effect, since what manly-man like myself would ever say that lavender was their favorite color?  Am I right or am I right or am I right?

Great hearing from you guys.  Have a Zealishious day!

daveburke |

2/7/2006 9:56:03 AM Permalink

Bitwise-and. Now that is cool. I guess my modulo2 days are over.

And Dave, I hear that lavendar is the new green now days. Wink

-Ryan

Ryan Farley |

2/7/2006 9:58:33 AM Permalink

Ryan, Thanks for visiting and particularly your vote of confidence in Lavender Futures.  Seriously, when it comes time to redesign my CS 2.0 look, I think I'll go with whatever color YOU go with.  You're the one with the great eye.

Then again, Lavender IS very soothing...

daveburke |

2/7/2006 6:42:50 PM Permalink

Dave,

Whilst I'm sure your average New Zealander would just *love* being mistaken for an Aussie, I can assure you that it doesn't work the other way around.

Just kidding, Tim. Wink

Albury, NSW, Australia, Dave. Look it up on Google Maps. Smile

mabster |

2/7/2006 7:12:32 PM Permalink

I guess Australia and New Zealand are actually different, now that you remind me of it.  I only know Hipsters from that area of the world, so it's easy to get the specific points of origin confused.  Sorry 'bout that.  G'DAY, MABSTER!  (Pretty bloody realistic, eh?)

daveburke |


Powered by BlogEngine.NET 2.0.0.36
Theme by Dave Burke