I, Codemonkey

Codemonkeying Around
posts - 4, comments - 4, trackbacks - 0

My Links

News

Archives

Sunday, October 07, 2007

C# Extensions and Casting

One of the things in VS 2008 that has intrigued me has been Extension Methods. I've been trying to think of ways to use these in my applications and coming up with ways to move functions associated with various processes to Extensions in our own APIs. ScottGu recently posted an interesting article on his weblog about writing an Extension for creating JSON.

One thing I dislike doing and have to do fairly often is cast Object sender to various things to pull information out of it. The most common reason to do this is that I'm making my buttons dynamically and I need to know which button was pressed so that I can have the program act accordingly. Usually I do something like this:

Button b = (Button)sender;

Then grab something out of it like b.Text and use that value.
I dislike this because the casting is just straight up, no holds barred, you were one thing and now you're this. And, if I'm wrong I throw an exception. So, I should be doing some checking, some Exception handling, and what have you. But, I really don't want to clutter up the code with all that since the Event is only tied to these buttons. Of course, a nagging in the back of my mind asks if it will always be that way (probably) and another nagging asks if I'm compromising my ethics by not doing these checks.

Then along comes Extension methods in the VS 2008 (Orcas) Beta. I can do basically the same things using Extensions but hide some error checking behind the scenes without having to write the code over and over. I can also update my casting method in one place and it will be changed everywhere, because right now it is very basic. Here's what I have so far:

        public static Button AsButton(this object o)
        {
            if (o is Button)
                return (Button)o;
            return null;
        }

Then in the forms code:

        Button btn = sender.AsButton() ?? new Button();

This is, of course, very simplistic, but it does allow me to create a place where I can handle this casting in one place, something I am very fond of doing. Although I'm still contemplating whether or not returning null is the right answer. Basically what I'm saying is that it will always have to be a Button, and if it isn't a button... then its a Button. I'm not sure if I like that. But, that's not the the functionality of the Extension doing that, so what I do with that returned null can vary from place to place.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Sunday, October 07, 2007 3:24 PM | Feedback (4) |

Sunday, August 19, 2007

You made my program the SHELL???

So, a while back I created what I consider a neat .NET program. It opens a remote desktop session to a Virtual Machine we have running on VMWare and logs into the machine. Then, if the remote session is closed, it shuts down the computer. It did have the downside that the remote session could, of course, be minimized and the users could access the computer. We've got some Neoware boxes now, but these were good for some training sessions we were doing throughout the company.

In any case, I found out today that the shells of all these laptops had been replaced by my program. That means no taskbar, and it pretty much locks out the computer completely. But, I swear, I swear! that nobody had told me about this change. It took me about 20 minutes before I gave up trying to fix one of these laptops and asked, only to be assured that I had agreed to this crazy idea. It's pretty cool, I have to admit. But, now getting the computers back is going to be a pain. And, here I thought the program could just be removed from Windows Startup folder.

Acronis, here I come!
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Sunday, August 19, 2007 2:06 PM | Feedback (0) |

Tuesday, July 03, 2007

Custom Exceptions and Exception Wrapping

I work almost only with code written by predecessors at my job. There's good code and bad code. Recently, I've been trying to clean up the exceptions. Just about everything is throw Exception() instead of customized exceptions, so you never really know what you're catching. It could be an IO exception, a null pointer, or whatever. The message is generally something that can be displayed to the user without them becoming confused, but with a mix of "good" message exceptions and "
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Tuesday, July 03, 2007 9:05 AM | Feedback (0) |

Monday, June 25, 2007

Nested String Statements

Here's a strange one. Last night I had a dream about nested strings. The most obvious use of this would be in a nested SQL statement, so here's a made up on the fly example:

select   phone_number
from Addressbook
where name = (select name from customers where city = 'Atlanta')

But instead of writing that out as a string in my code, I did this crazy thing:

string sQuery = "select name from customers where city = 'Atlanta'";
sQuery = " select   phone_number from Addressbook where name = (" + sQuery + ")";

Mostly, I would never use that. It's far too difficult to go back and read later. However, I got to thinking and it might be interesting to use that with a dynamically created SQL statement. Say I was going to do a phone look up, but I wasn't sure what I was going to do the test against, I could use this little pattern. That way I could do a lookup versus city, zip code, PO numbers, or anything else, and it might actually be easier to read in the future this way than with some kind of nested if statement that creates the SQL through conditional statements.

I'll have to think on this a little more.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Monday, June 25, 2007 8:11 AM | Feedback (0) |

Powered by: