我正在创建一个WCF服务,服务中的一个项目是名为County的Enum类,其中包含处于此状态的县列表。另一项是一个名为Person的Object类,它使用了这个Enum的数组(出于商业原因需要一个数组,而不仅仅是一个County。)这不是我正在使用的这个服务中唯一的数组,但是其他数组涉及其他对象,而不是枚举,工作得很好。
我收到以下错误:
Value of type '1-dimensional array of type LAService.County' cannot be converted to '1-dimensional array of type LAService.County?' because 'LAService.County' is not derived from 'County?'
。
'?'
是什么?我之前因为使用了错误的类型而发生了这个错误,但问号是一个新东西。我如何通过此错误?
我的代码:
Public Enum County
Acadia
Allen
Ascension
...and on and on...
End Enum
<DataContract>
Public Class Person
<DataMember()>
Public ServiceCounty() As Nullable(Of County)
...and on and on...
End Class
Public Function FillPerson(ds as DataSet) As Person
Dim sPerson as Person
Dim iCounty as Integer = ds.Tables(0).Rows(0)("COUNTY")
Dim eCounty As String = eval.GetCounty(iCounty) 'This evaluates the county number to a county name string
Dim sCounty As String = DirectCast([Enum].Parse(GetType(County), eCounty), County)
Dim counties(0) As County
counties(0) = sCounty
sPerson = New Person With{.ServiceCounty = counties}
Return sPerson
End Function
在构建代码之前,Visual Studios在单词“sPerson = New Person With{.ServiceCounty = counties}
”的“counties
”行显示上述错误。同样,我使用的所有其他数组都以相同的方式创建,但使用的是对象而不是枚举。我已经尝试将Dim sCounty as String
更改为Dim sCounty As County
,但我收到同样的错误。我也尝试摆脱DirectCast
行,只使用Dim sCounty As County = County.Acadia
但仍然出错。
答案 0 :(得分:1)
?
是Nullable(Of T)
的简写。例如,Dim x As Nullable(Of Integer)
与Dim x As Integer?
的含义相同。因此,您可以通过更改此行来修复它:
Dim counties(0) As County
对此:
Dim counties(0) As Nullable(Of County)
或者,更简洁,这个:
Dim counties(0) As County?