- Feature Articles
- CodeSOD
-
Error'd
- Most Recent Articles
- The Song that Never Ends?
- Princess Pricing
- Einfach so
- Kaids Hen 2025
- Fi fa foe
- Microbits
- No Rush
- Bridge for Sale
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I agree, this is horrible, but it might be trying to avoid issues when an empty string would convert to 0 by default.
What is the real WTF about this is that Convert take a string and returns null, and ConvertBack take a double? and returns null.
Using the keyword 'as' does not do type conversion, it just casts to that type, so what they were thinking about, I do not know.
Admin
I am under the impression that this does not convert anything.
Convertonly casts theobject valuetodoubleand then directly returns thatdoubleback asobject. The only effect here is, ifobject valueisn't a double, it'll returnnullsubsequently.The same goes for
ConvertBack, except with the additional comparison forstring.Emtpyto return null.So it seems to me, that this does not - as then name suggest - converts
doubletostringand vice versa. This only ensures the returned object is of the expected type or null.As for the convert/convert-back direction it's quite simple: From the view-model to the view is "converting", from the view to the view-model is "converting-back". It got nothing to do with the attributes. I believe, those are only for automatic converter selection of WPF but I never used that.
Edit Admin
The real WTF here is all the different kinds of double we can have in c#.
WPF predates double? so it knows nothing about items 3 and 4. WPF explicitly handles the conversions for #2, and binding to a #1 gets automagically boxed to #2 when you try to assign a double to an object. The author wants to edit a double? in a textbox so they need a converter. it turns out that converting case 3 to and from case 2 is enough to make this work. I still think this is bad code. If you are going to write your own converter you might as convert all the way to the type you need. (Double has ToString and TryParse methods that make this trivial to implement.)
WPF has its quirks, but no worse than any other framework. (Or maybe after 23 years they've become normal to me.) Given its age, I am not ready to call all of WPF a WTF!
Admin
#4 is not true. Nullable is special in a number of ways, one of which is that is never gets boxed. If you convert a nullable to object (or to any reference typed variable e.g. an interface), then it either turns into a boxed version of its underlying type (if it had a value), in this case double, or a straight up null (if it was null). This has been the case since it was introduced in net2.0. I wouldn't call any of this a wtf. You really only have double, There are no other variants worth mentioning. You never have to care about boxed doubles, because the runtime handles everything for you. And nullable doubles aren't a different type of double, but rather a wrapper that handles nullability (unless you want to consider any monadesque type like Task<double> as a different type of double, which is just a weird way of looking at thing).
Admin
Also wpf absolutely does not predate nullable.
Admin
Hmm, correction.
The name of the converter is back to front.
It is not a StringToDoubleConverter, but an attempt at a DoubleToStringConverter. It still does not actually convert anything though, that is left to WPF.
Admin
It looks like you are correct.
They just needed to add TargetNullValue='' to the binding.
Every day is a learning day.
Edit Admin
"Convert" means convert some other object to the type of which the Convert method is a member. "ConvertBack" means convert an object of the same type of which ConvertBack method is a member to something else.
Could better be named as ConvertTo and ConvertFrom I suppose, like Powershell does. But the intent is clear even if you don't like the naming.