1

我正在创建一个 WCF 服务,服务中的一项是一个名为 County 的枚举类,其中包含该州的县列表。另一个项目是一个名为 Person 的 Object 类,它使用此 Enum 的数组(出于业务原因,需要一个数组,而不仅仅是一个县。)这不是我正在使用的此服务中唯一的数组,但其他数组涉及其他对象,而不是枚举,并且工作得很好。

我收到以下错误:

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

sPerson = New Person With{.ServiceCounty = counties}在我构建代码之前,Visual Studios在单词 ' '的 ' ' 行显示上述错误counties。同样,我使用的所有其他数组都是以相同的方式创建的,但使用对象而不是枚举。我已经尝试将我的更改Dim sCounty as String为,Dim sCounty As County但我得到了同样的错误。我也试图摆脱这DirectCast条线,只是使用Dim sCounty As County = County.Acadia并仍然得到错误。

4

1 回答 1

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?
于 2013-02-07T19:17:33.260 回答