简体   繁体   中英

VB.NET. Indexator and Property conflict

I need to have an indexator and property with the same names

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

Error: Error 8 'Public Default Property WsItemIcChar(i As Integer) As Char' and 'Public Property WsItemIcChar As Char' cannot overload each other because only one is declared 'Default'.

How can I use both Property and Indexator without changing their names?

I'm not sure what you mean by "indexator," but you can overload your property in two ways:

  1. Declare the second as Default,

     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. Declare i as Optional,

     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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM