2013-02-07 95 views
1

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

我收到以下错误:

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 

之前,我建立了代码,视觉工作室出上述错误处字“counties”的“sPerson = New Person With{.ServiceCounty = counties}”线。同样,我使用的所有其他数组都是以相同的方式创建的,但是使用Objects而不是Enums。我已经尝试将我的Dim sCounty as String更改为Dim sCounty As County,但我得到相同的错误。我也试图摆脱DirectCast线,只使用Dim sCounty As County = County.Acadia仍然有错误。

回答

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? 
+0

唉唉,这是我第一次曾经不得不使用Nullable。我只是想不能要求县财产。现在我知道了(并且知道是一场战斗。) –

+0

The?意味着在VB.Net –

+0

@ChrisDunaway同样的事情谢谢! –