The '?' character cannot be used here

纵然是瞬间 提交于 2019-12-10 02:26:38

问题


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.


回答1:


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).



来源:https://stackoverflow.com/questions/21755611/the-character-cannot-be-used-here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!