logo
  • Jobs
  • About Me
  • Contact
  • Home
« A flexible plugin loader
Strange error when using COM+ applications »

A WTF moment

Posted July 13th, 2005 by Matt Berther

Stumbled across this chunk of code while tracking down a bug… Although this had nothing to do with the bug, it did leave my scratching my head saying WTF… Variable names have been changed to protect the guilty… :)

private int SOME_CONSTANT = System.Convert.ToInt32("1", 16);
// ED: numerous similar lines snipped.
private int SOME_OTHER_CONSTANT = System.Convert.ToInt32("200000", 16);

These are supposed to be constants that are being passed in as XOR’d options to an external program. The first thing that you can see going on is that they are not constants (note the lack of a const keyword).

You cant use the const keyword with an expression that is not a compile time constant (ie: the Convert.ToInt32 method). This leads me to my second point… Why arent we using hex codes to define these and save the initialization? Everytime the class gets instantiated all of these expressions need to be evaluated.

private const int SOME_CONSTANT = 0x80;

Once you do this, you can get the const keyword, because 0×80 is a compile time constant. Also, you save the reinitialization of these values everytime the class gets instantiated.

3 Comments

This entry was posted on Wednesday, July 13th, 2005 at 11:40 am and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Geoff Appleby
July 13th, 2005

Hey,

What are you saying that they have to be hex? Plain old integers are just as constant.

private const int SOME_CONSTANT = 11;

Matt Berther
July 13th, 2005

Sure… but the original code showed hex, and the documentation for the external app showed hex, so I was trying to keep the look consistent when changing this…

David Starr
July 13th, 2005

OK, now THIS time I can say fo sure that it wasn’t me.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
-->

flag
Favorite Charity
wounded warrior project
Search
Social
  • mattberther on twitter
  • mattberther on linkedin
Syndication
Archives
  • January 2010
  • September 2009
  • July 2009
  • June 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • September 2008
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
mattberther.com © 2003 - 2010