通过


无法在对象初始值设定项表达式中初始化属性“<propertyname>”,原因是所有可访问的重载都需要参数

对象初始值设定项列表中初始化的成员必须是字段或属性。 此外,初始值设定项列表中的属性不能有参数。 导致此错误的属性重载,其每个版本都需要参数。 因此,无法在对象初始值设定项列表中初始化该属性。

错误 ID: BC30993

更正此错误

  • 删除需要初始值设定项列表中的参数的属性。

示例

以下类包括三种属性定义:一种用于 TotalItems ,两种用于 Item,这种情况为重载。

Class CollectionOfItems  
    Property TotalItems() As Integer  
        Get  
        End Get  
        Set(ByVal value As Integer)  
        End Set  
    End Property  
    Property Item(ByVal Key As String) As Object  
        Get  
        End Get  
        Set(ByVal value As Object)  
        End Set  
    End Property  
    Property Item(ByVal Index As Integer) As Object  
        Get  
        End Get  
        Set(ByVal value As Object)  
        End Set  
    End Property  
End Class  

TotalItems 属性不需要任何参数,并可在对象初始化列表中初始化,如以下声明中所示。

Dim coinCollection As New CollectionOfItems With { .TotalItems = 0 }  

Item 属性为重载,并且每个重载都需要一个参数。 因此, Item 不能出现在对象初始值设定项列表中。

' The following declaration is not valid.  
' Dim coinCollection As New CollectionOfItems With { .TotalItems = 0, _  
'    .Item = aCoinObject }  

若要避免此错误,在对象初始值设定项之外初始化 Item 属性。

Dim coinCollection As New CollectionOfItems With { .TotalItems = 0 }  
coinCollection.Item(1) = aCoinObject  

另请参阅