Stupid Code To The Rescue!

A big dose of .NetFrustration this morning.

When you upgrade an assembly from version 1.0 to version 1.1 you are likely to get a lot of squiggley blue lines and warnings such as:

OR:

OR worse yet:

Google turned up blank, MSDN turned up blank, but I finally got rid of the warnings, by writing stupid code.

(Read on for some dodgy solutions and some stern lectures. :+))

For example:

Rather than:

I would write:

And voila! it works just fine.

The next failed piece of upgraded code was this

In this case you need to add two more parameters, an 'XMLResolver' and an 'evidence' param. This worked:

Evil Moral of the story is:

If a parameter is required, but you don't know why, just set it to 'nothing.'

That's not the sort of lesson I'd like to teach people. But it's what Microsoft taught me today.

Third situation was a bit trickier and called for an even nastier hack.

This worked in version 1.0, but threw a warning in 1.1:

The warning was a familiar one:

My first thought was: 'add a new parameter, value of nothing'. But this failed because the compiler couldn't resolve which overloaded interface I was after. The result of such an approach was:

i.e., 'Sure you want nothing -- but what type of nothing??' Fair cop, so I tried this instead:

Now there were no warnings and it did compile. But did it work? Who cares? Because right then i thought of a cleverer and nastier solution, that would be bound to confuse other developers. Naturally, I gravitated toward it immediately:

I didn't test that it worked, because 'hey, it should, right?'

At that moment, another, nastier solution occurred to me. I call this one: "What type of 'nothing'? **This** type of nothing!"

No time to test it, but the compiler doesn't freak, so my money says she works.

And now I can get on with writing other, better, faster bugs.

As an old friend said to me just yesterday: "One man's bug is another man's paycheck."

 

My book "Choose Your First Product" is available now.

It gives you 4 easy steps to find and validate a humble product idea.

Learn more.

Kathleen Dollard on April 24, 2004 19:02 sez:

I know this looks stupid, but I think there's some logical reason behind this. The new parameters are important, quite important in a few scenarios. If they had not made the old ones obsolete, there would have been a gazillion and you were forced to look at this issue. I actaully investigated what the resolvers were before making the same changes a year ago.

While the CType(Nothing, whatever) approach feels like a hack, it appears to be the appropriate way to resolve overloads anytime Nothing is confusing the issue.


secretGeek on April 26, 2004 01:58 sez:

Thanks Kathleen! I agree that they should make old methods obsolete -- but I think that the documentation they provide for these upgrades is quite poorly thought out.

The comment 'You should pass XmlResolver to Transform()' (for example) is a redundant comment, since the intellisense will show you that an XmlResolver parameter is now required. What I think the upgrade comments should do is point you toward documentation that explains:

Why the change was made.
How to cater for the change (giving practical examples).
What benefits you will get from making this upgrade.

Generally the comments they choose to include just have a statement like 'Hey, don't use this, use that instead' -- in this era of secure computing we're expected to know what we're coding and why -- not just to blindly follow. SO i think this is one little area where microsoft could pay more attention to the needs of developers.

For example, looking up what an XmlResolver is didn't do anything to tell me why it was previously unimportant but now is important. Clearly there's a design design they've made under the covers -- but there's no clue as to what it is. Okay, my XmlDocument didn't have any external references that needed to be resolved -- so a resolver of nothing seemed fine. But it felt wrong -- they've chosen to include it for a reason I guess, so i felt like there was something I was missing.

Having said that, i do think that MS generally do an excellent job of looking after developers -- this just seems to be a chink in the armour. A place where the developers are taking shortcuts and noone is stopping them.

cheers
lb


Farhan Ahmad on September 26, 2004 19:51 sez:

Hey Leon,

Thanks for this post! It saved me quite some time and frustration!


Thanks!


Thraïn on October 06, 2004 06:31 sez:

Leon,

My search for a solution for the same problems ended after 3 hours with your post. Thank you so much!


Greetings


secretGeek on October 06, 2004 16:43 sez:

cheers Farhan!

and Thrain: i'm glad to know that this post is helping the peoples of middle earth.


ricpue@hotmail.com on February 12, 2005 14:57 sez:

Funny and good.


harveysburger on November 12, 2005 15:08 sez:

thanks for this easy fix :)

thats hilarious...
, nothing, nothing....


Dan F on January 15, 2006 14:22 sez:

Almost 2 years on, and this post is still going strong. You're the number one google hit for "you should pass xmlresolver" (with quotes). How spesh do you feel :)

Thanks Leon!


(By the way, I read every comment and often respond.)

Your comment, please?

Your Name
Your Url (optional)
Note: I may edit, reuse or delete your comment. Don't be mean.