-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CDate(CStr(Nothing)) should not cause an exception #621
Comments
The Roslyn team said that the VB compiler of Visual Studio 2013 was doing it wrong in the first place: "This is a bug in the constant optimization phase of the old compiler. It was never the intended behavior of the language." I agree with them. For a start, VBA/VB6 never allowed for it. So the VB compiler of Visual Studio 2015 (and later) does it correctly; there is no bug to fix. If one is stuck with a code-base that has "CDate(CStr(Nothing))" issues, really it should be refactored. It was a poor practice to begin with, and one can't even claim it was ever really okay to do that. |
I think https://dotnetfiddle.net/o1Ws5L shows that it’s a bug….only cBool and CDate throw
|
It may be a more compelling case to say that CBool and CDate are correct, and that the CByte thru CUShort cases (not including CStr and CObj) are wrong. Let Yet in VBA/VB6, things like VB.NET, for whatever the reasons, seems to have a special case for It's unclear to me whether this special case for Whether intentional or a mistake, I can see why there's a lack of interest and motivation to change CDate and CBool to be more like CPdq. |
@rskar-git : I’m not looking for VB6 compatibility, but with VB.Net pre-Roslyn. It changes functional code into non-functional. And VB.NET does extra handling with strings and nothing, this is definitely one of the places I, as a VB.net programmer, would expect it do the useful thing, which is to return the default date, aka date.minvalue. Throwing is not helpful. |
Well, to each their own, I guess. I still find code like this to be super-cringy:
even if the above would work at run-time in (pre-Roslyn) VS2013 VB. (Would it be interesting to point out that the reporter of this issue is happily coding in C# and Rust today?) |
Just ran into this today, apparently it's a long standing bug: dotnet/roslyn#2780, although I disagree with the recommended behavior.
IMO the correct fix is for the call to Conversions::ToDate(string) to check to see if the string is null, and return DateTime.MinValue if so.
In my case, Option Strict was Off and the actual assignment that caused the problem was obj.d = if(function(a, b)) where sometimes the return value was nothing and sometimes it was a string that could be interpreted as a date. Obviously the better code would be DateTime.TryParse(function(a,b), obj.d), but it shouldn't break.
The text was updated successfully, but these errors were encountered: