9/6/2006 2:28:41 PM
In creating a custom user control for a smart-client today I stumbled across what I think is a very simple gem for determining if an integer is even or odd. This makes sense for alternating row background colors when rolling your own controls.
You wouldn't believe the other methods I have seen people use! Here is a snippet of what I am trying to accomplish.
The code uses C#'s Modulus Operator (%). How this works is the operator computes the remainder after it divides it's first operand by the second. In the case below, the number of controls (i.e. count) is divided by 2. If it's an even-numbered row there won't be a remainder and if it's an odd-numbered row....
if(_pnlMenuItems.Controls.Count % 2 == 0)
{
newMenuItem.BackColor = System.Drawing.Color.LightBlue;
}
else
{
newMenuItem.BackColor = System.Drawing.SystemColors.Window;
}
C#,
Code

Comments

A quick Google and you`ve got the code-snippet. Just what I was needing. Thanks!
Posted by: Erik Lane | 10/3/2006 11:19:13 PM

Glad I could help Erik!
- will
Posted by: Will | 10/5/2006 11:35:46 AM

Just what I needed!
Posted by: Tom | 10/13/2006 11:10:34 AM

Thanks, I needed to verify this for C#.
As FYI, the modulus operator is available in any modern language, at least as far as back to Pascal, which is where my memory TTL ends. :-)
/Anders
Posted by: Anders | 1/8/2007 8:26:46 AM
Ups! hehe, sorry for that - of course it should be:
So, instead of write
bool even = !(int % 2);
bool odd = (int % 2);
write
bool even = !(int & 1);
bool odd = (int & 1);
Posted by: Marooned | 4/18/2007 9:28:31 PM

That code is really for beginners ain`t it ? why try to optimize something that easy :)You could have opened a post on prime numbers for example and how to efficiently find them.
Posted by: kids bedroom furniture | 5/10/2007 7:37:12 AM

That code is really for beginners ain`t it ?
Posted by: kids bedroom furniture | 5/10/2007 7:37:29 AM

I guess I could`ve written a more complicated example using Modulus. Hell, I could`ve written a post about the number of salmon spawning each year and how that affects the number of herring which in turn affect the health of many sea lions in Alaska. ;-)
Believe it or not, this thread generates an insane amount of traffic via Google.
Crazy.
Posted by: Will | 7/9/2007 12:56:48 PM

<STALE>
ahh Pascal.... we must be about the same age.....
BTW: your last awnser made my day. what was that guy trying to argue about? I can see how this thread gets so much traffic.; Awnsered my question... :^)
believe it or not I`m an american, just with really bad spelling... were in the buisness of making up words after all arent we....hahahhahaha
Dave
</STALE>
Posted by: Dave Smith | 8/25/2007 6:45:43 PM

I`m glad I could help Dave!
Posted by: Will Asrari | 8/25/2007 6:57:37 PM

Well, if you want a more complex example, I used the modulus feature extensively to calculate the check digit on a standard UPC here:
http://atomiton.com/2007/12/calculating-checkdigit-using.html
Most people use the modulus operator only to determine if a number is even or odd, but i`m using it with (x % 10). An entire code sample is shown too.
Posted by: Atomiton | 12/20/2007 6:32:14 PM

Well it hasn`t helped me, but then I am a beginner...but I have bookmarked it as Im sure i will need it one day....Thank!!!
Mandy
Posted by: Mandy | 11/18/2008 5:21:35 AM