1

私は WCF サービスを作成しています。サービス内の 1 つのアイテムは、この州の郡のリストを持つ County という名前の Enum クラスです。もう 1 つの項目は、この Enum の配列を使用する Person という名前のオブジェクト クラスです (単一の郡だけでなく、ビジネス上の理由から配列が必要です)。これは、私が使用しているこのサービスの唯一の配列ではありませんが、他の配列には他の配列が含まれます。列挙型ではなくオブジェクトであり、問​​題なく動作します。

次のエラーが表示されます。

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 StudiosPerson = New Person With{.ServiceCounty = counties}は単語 ' counties' の ' ' 行に上記のエラーを表示します。繰り返しますが、使用されている他のすべての配列は同じ方法で作成されていますが、列挙型の代わりにオブジェクトを使用しています。Dim sCounty as Stringすでにtoを変更しようとしまし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 に答える