VB.NET。 Indexator和Property冲突

时间:2014-11-11 08:39:19

标签: vb.net

我需要一个具有相同名称的索引器和属性

Public Class WsItemGrpType
    Private _redefined As WsItemIcType
    Public Sub New(ByVal redefined As WsItemIcType)
        _redefined = redefined
        WsItemIcChar = New WsItemIcCharType(_redefined)
    End Sub
    Public WsItemIcChar As WsItemIcCharType
    Public Class WsItemIcCharType
        Private _redefined As WsItemIcType
        Private _current As Integer = 0
        Private _length As Integer = 9
        Public ReadOnly Property Length() As Integer
            Get
                Return _length
            End Get
        End Property
        Public Sub New(ByVal redefined As WsItemIcType)
            _redefined = redefined
        End Sub

        Default Public Property WsItemIcChar(i As Integer) As Char
            Get
                _current = i
                Return Char.Parse(_redefined.WsItemIc.Substring(_current * 1, 1))
            End Get
            Set(value As Char)
                _redefined.WsItemIc = _redefined.WsItemIc.Remove(_current * 1, 1).Insert(_current * 1, value.ToString())
            End Set
        End Property

        Public Property WsItemIcChar() As Char
            Get
                Return Char.Parse(_redefined.WsItemIc.Substring(_current * 1, 1))
            End Get
            Set(value As Char)
                _redefined.WsItemIc = _redefined.WsItemIc.Remove(_current * 1, 1).Insert(_current * 1, value.ToString())
            End Set
        End Property

    End Class
End Class

错误:错误8'公共默认属性WsItemIcChar(i作为整数)因为Char'和'Public Property WsItemIcChar As Char'不能相互重载,因为只有一个被声明为'Default'。

如何在不更改名称的情况下同时使用Property和Indexator?

1 个答案:

答案 0 :(得分:1)

我不确定你的意思是"索引器,"但是你可以通过两种方式重载你的财产:

  1. 将第二个声明为默认值,

    Default Public Property WsItemIcChar(i As Integer) As Char
        Get
            _current = i
            Return Char.Parse(_redefined.WsItemIc.Substring(_current * 1, 1))
        End Get
        Set(value As Char)
            _redefined.WsItemIc = _redefined.WsItemIc.Remove(_current * 1, 1).Insert(_current * 1, value.ToString())
        End Set
    End Property
    
    Default Public Property WsItemIcChar() As Char
        Get
            Return Char.Parse(_redefined.WsItemIc.Substring(_current * 1, 1))
        End Get
        Set(value As Char)
            _redefined.WsItemIc = _redefined.WsItemIc.Remove(_current * 1, 1).Insert(_current * 1, value.ToString())
        End Set
    End Property
    
  2. i声明为可选,

    Default Public Property WsItemIcChar(Optional i As Integer = 0) As Char
        Get
            If (i <> 0) Then _current = i
            Return Char.Parse(_redefined.WsItemIc.Substring(_current * 1, 1))
        End Get
        Set(value As Char)
            _redefined.WsItemIc = _redefined.WsItemIc.Remove(_current * 1, 1).Insert(_current * 1, value.ToString())
        End Set
    End Property