CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Raymond Lewallen

Framework Design, Agile Coach, President Oklahoma City Developers Group, Microsoft MVP C#, TDD, Continuous Integration, Patterns and Practices, Domain Driven Design, Speaker, VB.Net, C# and Sql Server

December 2006 - Posts

  • Covariance and Contravariance

    So here is something that came up in a discussion I was having.  It revolved around delegates and their parameter types because we were trying to find an easier way to solve an issue without having to write more code than we really wanted to.  If you program entirely in VB or are not using .Net framework 2.0 with C#, then you are missing out on 2 features of delegates that are very useful: covariance and contravariance.  Now I have personally spent, until very recently, the vast majority of my time in VB and Framework 1.1, so even though I knew about the features, I never fully understood what I could do with them and I was never able to make use of them until now when it came up as part of a way to solve a problem in a program that I am helping a friend with.  He knew nothing of these new features, so maybe there are more of you out there that benefit from this, as it doesn’t seem to be well documented in easy to find places.  Just for fun I tried to google "covariance" and a few other things with no success, so maybe putting this out there will make it easier for others to find.  These are fine examples of how the languages continue to evolve and allow us to create more flexible, maintainable and efficient code.

    Covariance basically means that the return value of a method that is referenced by your delegate can have a different return type than that specified by the delegate itself, so long as the return type of the method is a subclass of the return type of the delegate.  In the example below, you can see that MyMethod returns Car, but xyz is calling FunctionTwo, which returns type Toyota.  Because Toyota is derived from Car, this works.

    Covariance
    public class Car
    { } public class Toyota : Car
    { } class Demo
    { static void Main() { MyMethod abc = FunctionOne; MyMethod xyz = FunctionTwo; } public delegate Car MyMethod(); public static Car FunctionOne() { return new Car(); } public static Toyota FunctionTwo() { return new Toyota(); } }

     

    Contravariance is kind of the same thing, but deals with the parameters rather than return types.  Contravariance allows you to use delegate parameters who’s types are those that inherit from the parameter types used in the method the delegate references.  In the following example, you can see that FunctionOne specifies the Car type as the parameter, while the delegate has the signature that specifies Toyota.  Contravariance allows this to happen.

    Contravariance
    class Demo
    { public delegate void MyMethod(Toyota value); static void Main(string[] args) { MyMethod abc = FunctionOne; MyMethod xyz = FunctionTwo; } public static void FunctionOne(Car value) { } public static void FunctionTwo(Toyota value) { } }
  • My new laptop I wear on my hip.

    Last week I decided its time to get a new phone.  I’ve always had just a “phone”.  You couldn’t do anything more with it than dial numbers and talk to people (although my last phone, the Motorola V300 did have a camera).  This time I went all out.  I didn’t get just a phone.  I got a miniature laptop that fits on my hip.

    I now have a Blackberry Pearl smartphone, the latest and greatest in mobile handheld technology.  I have to tell you, after having this phone for a few days, I am absolutely loving it.  The only thing this phone doesn’t do is record video, unless it does and I just haven’t figured it out yet.  Having just stepped into the boundries of Blackberry, I’m awed and impressed.

    The Pearl has seemless integration with GMail, which was important to me.  The email features are outstanding.  This doesn’t have a full 1 key per letter keyboard, but typing is just as fast (most of the keys are doubled up i.e. A/S are on one key), because of the QWERTY keyboard layout.  It has built in support for Yahoo, AIM, MSN, ICQ and Blackberry instant messenging.

    The trackball navigation is pretty slick.  You can pretty much do everything you need to with your thumb just by moving and pressing the trackball.  Voice activated dialing is also built in.

    The address book is nice and take me back to the days of an old phone I had many years ago.  My V300, I had to store HOME and WIFE CELL seperately, as did for DAD HOME, DAD OFFICE, DAD CELL etc.  Now, I just have an entry for DAD, and in that entry I have all his phone numbers, email, addresses etc.

    The address book synchronizes with the Contact list from my Microsoft Outlook.  Same with the calendar, tasks and memos, so I can keep them all synched up just like a regular Palm thingy.  Right now I do the synchronizations via USB, but hoping that a new laptop soon with also have Bluetooth installed and then I can go all wireless.

    Browsing the internet is just as fast on my phone as it is at my house it seems (I have 1.5 Mbps down at home).  Now, I know that my phone doesn’t process the backgrounds, styles etc (I have them turned off) but going to CodeBetter came up in a flash.

    The picture quality of photos I take is much better with the Pearl than it was with my V300.  Its 1.3 megapixels with 5x zoom and built-in flash.

    Blackberry Maps is very cool.  I just punch in an address and it brings up the map, or I can get driving directions as well.  It doesn’t have a GPS receiver in it, so this is the next best thing.

    This thing knows when you have it holstered or not.  You can set it up so that if you have it holstered, it will only vibrate when an email comes in, or ring and vibrate when you get a phone call, or play a tune when somebody IMs you.  When you take it out, it knows it and plays different tunes, doesn’t vibrate, or however you set it up.  Cool stuff.

    Tons of applications available for this phone at Handango.  547 for the Pearl.  Many other devices are listed there as well.

    I also bought a 1 GB memory chip for the Pearl.  I can load tons of MP3s and play them right on my phone, just like an IPod.

    So in short, I have a phone, Palm, IPod, camera, email etc all in one device.  Sounds just like a laptop, eh?

  • Things that have come to pass. Things that have still yet to come.

    I would like to wish my wonderful wife of 10 years a Happy Anniversary today.  This year has been the most trying year of our lives.  To quote a line from the new Rocky Balboa movie I watched last night regarding life, “… its about how hard you can get it, and keep moving forward.”  We took our punches, and we’re still standing.

    Also some exciting news is that I will begin a new job on January 15th for one of the best consulting firms there is: Geniant.  I’m really looking forward to moving back to the private/commercial sector after having spent the last 9 years working entirely in the public sector.  Geniant has some awesome individuals on staff, including Chip Wilson, who is 1 of about 50 people world-wide to hold the Microsoft Certified Architect honor, as well as certifications from IBM and Sun.  Exciting opportunity indeed.

    April 16th, 1 year to the day when our son died, we shall have a new son arrive.  For those of you who know the story from earlier this year, this baby looks completely healthy and everything is great at this point. 

    I did a lot of presentations around the Midwest this year, and have 5 setup for this coming winter and spring, so I’ll see some of you around early this coming year.  I really enjoy doing this and am looking forward to the tour before my son arrives.

    I’m looking forward to blogging more again.  My new job and all the industries and technologies it shall touch is going to be exciting and I’ll have plenty of things to share with you as I learn new tricks, new tools and new people.

    Stay tuned, its going to be a great 2007!

More Posts