- 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
Also wpf absolutely does not predate nullable.