Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The '?' character cannot be used here

With these variables:

Dim d1 As Date? = Nothing
Dim d2 As DateTime? = Nothing
Dim i1 As Integer? = Nothing
Dim i2 As Int32? = Nothing

Why am I allowed to do this?:

Dim flag1 As Boolean = Date?.Equals(d1, d2)
Dim flag2 As Boolean = Integer?.Equals(i1, i2)

...but not allowed to do this?:

Dim flag3 As Boolean = DateTime?.Equals(d2, d1)
Dim flag4 As Boolean = Int32?.Equals(i2, i1) 

The last code will fail with an error saying:

The '?' character cannot be used here.

like image 615
Bjørn-Roger Kringsjå Avatar asked Feb 13 '14 13:02

Bjørn-Roger Kringsjå


1 Answers

VB.NET developers are not supposed to be using C# keywords (religion, you know). Seriously though, I agree with @Konrad this looks like a compiler bug. If you have other VS, try it there, I only tried in VS 2010 SP1, cause that's what I have at work. If you notice consistency, perhaps you should report it on connect. As a workaround, you can try this:

Dim flag3 As Boolean = d1.Equals(d1)
Dim flag4 As Boolean = i2.Equals(i1)

Or this:

Dim flag3 As Boolean = Nullable(Of DateTime).Equals(d1, d2)
Dim flag4 As Boolean = Nullable(Of Int32).Equals(i1, i2)

I personally prefer the last option in my code, to explicitly say Nullable(Of T), because VB language is supposed to be verbose, more English-like, i.e. no :? || && constructs (no offense, C# devs).

like image 50
Neolisk Avatar answered Oct 16 '22 08:10

Neolisk