March 2010 Entries

NativeMethods classes are special

“Whatever doesn’t kill you makes you stronger” That’s how I feel about Code Analysis. It’s painful, but in the end, it teaches me stuff that I didn’t know or hadn’t thought about. Richard Broida challenged me to see if I could rework some WIN32 P/Invoke code to get it to pass Code Analysis rules. My initial response was pretty close to whining, but I recovered from my temper tantrum and did some investigation. Luckily, I didn’t have to look too far… The info on the rule “CA1060:MovePInvokesToNativeMethodsClass” provided some useful guidance and explanation. So I created a class in my project called...

posted @ Monday, March 15, 2010 3:07 PM | Feedback (0)

Who knew overriding GetHashCode() was so much fun?

So, I have a project with Code Analysis turned on and it treats warnings as errors. In this project, I have a class that overrides Equals() so that I can use some of the nice collection based linq expression (like Except()). Well, code analysis doesn’t like it if you override Equals, but forget to override GetHashCode(). Needless to say, I have to override GetHashCode(). But how does one do this? Some searching turned up a bunch of interesting resources showing how to do this. However, there isn’t some cut and paste code that solves the problem. You have...

posted @ Friday, March 05, 2010 2:23 PM | Feedback (0)

Making WPF Remember Window Size, State, and Position…

I had a need to make my WPF app remember it’s size, position and state for the main window. A bit of “Binging” and I eventually heard the sound of found. Turns out there are a couple ideas out there, but the WINDOWPLACEMENT solution from MSDN seemed to fit the bill the best. This solution requires using some WIN32 API calls to get and set the information. The upside is that it seems to leverage the smarts built in to Windows regarding multi-monitor detection. The sample says that if the app was previously displayed on a secondary monitor that...

posted @ Thursday, March 04, 2010 11:15 AM | Feedback (1)

Except() Extension method Returns the “Difference”

Maybe I’m late to the party, but I just found the Except() LINQ extension method. I knew I loved .NET! For those of you familiar with set theory, this returns the “difference” between two collections. In my case, I needed to append a set of items to an existing collection, but only if they’re not already in the target collection. This thing was just what I needed. Here’s the definition: // // Summary: // Produces the set difference of two sequences by using the default equality // ...

posted @ Tuesday, March 02, 2010 5:44 PM | Feedback (0)