VB.NET If (triple) error

I found a simple error in VB.NET that can be easily reproduced:

Dim pDate As Date? Dim pString As String = "" ' works fine as expected pDate = If(False, "", Nothing) ' expected: pDate will be set to Nothing. ' BUG: Conversion from string "" to type 'Date' is not valid. pDate = If(False, pString, Nothing) 'These both fail with the same error pDate = pString Dim pDate2 As Date? = "" 

Question: Is this a mistake? Or is something wrong with me or my PC? If this is an error, is there an error report (I cannot find it)?

Lessons learned:

  • It's not a mistake
  • nullable date takes an object nothing
  • nullable date rejects string nothing

 pDate = Nothing ' ok. nullable date accepts object nothing pString = Nothing pDate = pString ' error. nullable date rejects string nothing 
+4
source share
2 answers

Error when using If() for the first time, and not in the second. Unlike your comment, the result is not β€œexpected”. This call should fail because "" cannot convert the date, and the ternary operator is typical at all levels, regardless of whether the expression is used.

I suspect this succeeds due to compiler optimization: since all are literals, the condition is optimized. The second time it’s harder to do the optimization, because the pString variable can be changed by another thread, which the compiler does not know about yet.

Someone who deals with IL is likely to confirm this.

The real surprise for me is that it is not caught before the execution time. I expect the compiler to notice a type mismatch and complain at that level, rather than waiting for execution. The VB Option settings may have something to do with this.

+5
source

This is pretty interesting. In the above example, it is pretty clear that something strange is happening. However, Qaru is actually not a good place to "report" errors. If you think you really found a bug, you can post your results to Microsoft connect .

I did a connection search, and there are a few quirks with both VB.NET and the C # triple operator, especially when Nullable types are involved. Could this be just one of them?

For what it's worth, you can even simplify things to look like this:

 Dim pDate As Date? pDate = If(False, "", Nothing) ' Works fine pDate = If(False, String.Empty, Nothing) ' Doesn't work 

It is worth noting that every situation that seems violated (all cases expect the use of "" ) works when the line looks like this: pDate = If(False, String.Empty, CType(Nothing, Date?))

In addition, Option Strict [On|Off] plays a very large role in this. When Option Strict On installed, then all these are compilation errors. This behavior can only be seen with Option Strict Off . I have compiled an example of all situations here .

In the end, I don't think this is really a mistake, but just one of the pitfalls of using Option Strict Off . It seems strange (illogical), but again it matters Option Strict Off .;)

+3
source

Source: https://habr.com/ru/post/1369253/


All Articles