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?

Upvotes: 0

Views: 56

Answers (1)

Justin Ryan
Justin Ryan

Reputation: 1404

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
    

Upvotes: 1

Related Questions