【问题标题】:implementing IDictionary interface in vb.net在 vb.net 中实现 IDictionary 接口
【发布时间】:2013-10-11 05:34:58
【问题描述】:

我正在尝试在 VB.net 中实现 IDictionary 接口,但出现错误。 请帮助我。

Imports System.Collections.Generic

Public Class DataDictionary Implements IDictionary(Of String, Object)
Private _ce As CalcEngine.CalcEngine
Private _dct As Dictionary(Of String, Object)

Public Sub New(ByVal ce As CalcEngine.CalcEngine)
    _ce = ce
    _dct = New Dictionary(Of String, Object)()
End Sub

#Region "IDictionary<string,object> Members"

Public Sub Add(ByVal key As String, ByVal value As Object) Implements Collections.Generic.IDictionary(Of String, Object).Add
    _dct.Add(key, value)
End Sub
Public Function ContainsKey(ByVal key As String) As Boolean Implements System.Collections.Generic.IDictionary(Of String, Object).ContainsKey
    Return _dct.ContainsKey(key)
End Function
Public ReadOnly Property Keys() As ICollection(Of String) Implements System.Collections.Generic.IDictionary(Of String, Object).Keys
    Get
        Return _dct.Keys
    End Get
End Property
Public Function Remove(ByVal key As String) As Boolean Implements System.Collections.Generic.IDictionary(Of String, Object).Remove
    Return _dct.Remove(key)
End Function
Public ReadOnly Property Values() As ICollection(Of Object) Implements System.Collections.Generic.IDictionary(Of String, Object).Values
    Get
        Return _dct.Values
    End Get
End Property
Public Function TryGetValue(ByVal key As String, ByRef value As Object) As Boolean Implements System.Collections.Generic.IDictionary(Of String, Object).TryGetValue
    If _dct.TryGetValue(key, value) Then
        Dim expr = TryCast(value, String)
        If expr IsNot Nothing AndAlso expr.Length > 0 AndAlso expr(0) = "="c Then
            value = _ce.Evaluate(expr.Substring(1))
        End If
        Return True
    End If
    Return False
End Function
Default Public Property Item(ByVal key As String) As Object Implements System.Collections.Generic.IDictionary(Of String, Object).Item
    Get
        Dim value As Object
        If TryGetValue(key, value) Then
            Return value
        End If
        Throw New Exception("invalid index")
    End Get
    Set(ByVal value As Object)
        _dct(key) = value
    End Set
End Property
#End Region


 #Region "ICollection<KeyValuePair<string,object>> Members"

Public Sub Add(ByVal item As KeyValuePair(Of String, Object)) Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, Object)).Add
    Dim d = TryCast(_dct, ICollection(Of KeyValuePair(Of String, Object)))
    d.Add(item)
End Sub
Public Sub Clear() Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, Object)).Clear
    _dct.Clear()
End Sub
Public Function Contains(ByVal item As KeyValuePair(Of String, Object)) As Boolean Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, Object)).Contains
    Dim d = TryCast(_dct, ICollection(Of KeyValuePair(Of String, Object)))
    Return d.Contains(item)
End Function
Public Sub CopyTo(ByVal array As KeyValuePair(Of String, Object)(), ByVal arrayIndex As Integer) Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, Object)).CopyTo
    Dim d = TryCast(_dct, ICollection(Of KeyValuePair(Of String, Object)))
    d.CopyTo(array, arrayIndex)
End Sub
Public ReadOnly Property Count() As Integer Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, Object)).Count
    Get
        Return _dct.Count
    End Get
End Property
Public ReadOnly Property IsReadOnly() As Boolean Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, Object)).IsReadOnly
    Get
        Return False
    End Get
End Property
Public Function Remove(ByVal item As KeyValuePair(Of String, Object)) As Boolean Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, Object)).Remove
    Dim d = TryCast(_dct, ICollection(Of KeyValuePair(Of String, Object)))
    Return d.Remove(item)
End Function
#End Region


#Region "IEnumerable<KeyValuePair<string,object>> Members"

    Public Function GetEnumerator() As IEnumerator(Of KeyValuePair(Of String, Object)) Implements IEnumerable(Of KeyValuePair(Of String, Object)).GetEnumerator

    Return TryCast(_dct.GetEnumerator(), IEnumerator(Of KeyValuePair(Of String, Object)))
End Function

#End Region

#Region "IEnumerable Members"

Private Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
    Return TryCast(_dct.GetEnumerator(), System.Collections.IEnumerator)
End Function

#End Region
End Class

当我编译时出现以下错误:

错误 1 ​​类 'DataDictionary' 必须实现 'Function GetEnumerator() As IEnumerator(Of KeyValuePair(Of String, Object))' 对于接口 'System.Collections.Generic.IEnumerable(Of System.Collections.Generic.KeyValuePair(字符串, 目的))'。 H:\Test\WindowsApplication1\WindowsApplication1\ProbeDataDictionary.vb 4 16 WindowsApplication1

错误 3 '公共函数 GetEnumerator() As System.Collections.Generic.IEnumerator(的 System.Collections.Generic.KeyValuePair(Of String, Object))' 和 '私有函数 GetEnumerator() 作为 System.Collections.IEnumerator' 不能互相超载,因为它们的区别仅在于返回 类型。 H:\Test\WindowsApplication1\WindowsApplication1\ProbeDataDictionary.vb 96 21 WindowsApplication1

提前致谢。

【问题讨论】:

    标签: .net vb.net


    【解决方案1】:

    您错过了一个GetEnumerator 方法的接口实现声明:

    Public Function GetEnumerator() As IEnumerator(Of KeyValuePair(Of String, Object))
    

    应该是

    Public Function GetEnumerator() As IEnumerator(Of KeyValuePair(Of String, Object)) Implements IEnumerable(Of KeyValuePair(Of String, Object)).GetEnumerator
    

    更新

    好的,你的方法应该如下:

    Public Function GetEnumeratorGeneric() As IEnumerator(Of KeyValuePair(Of String, Object)) Implements IEnumerable(Of KeyValuePair(Of String, Object)).GetEnumerator
        Return _dct.GetEnumerator()
    End Function
    

    这是因为你不能有两个同名的方法,即使它们实现了不同的接口。

    【讨论】:

    • 谢谢,我已经完成了更新,但它给了我以下信息,请帮助我 错误 3 无法实现 'System.Collections.Generic.IEnumerable(Of System.Collections.Generic.KeyValuePair(Of String, Object)).GetEnumerator' 因为它的实现可能与某些类型参数的 'System.Collections.Generic.IEnumerable(Of System.Collections.Generic.KeyValuePair(Of String, Object)).GetEnumerator' 的实现冲突。 H:\Test\WindowsApplication1\WindowsApplication1\ProbeDataDictionary.vb 108 106 WindowsApplication1
    【解决方案2】:

    以下代码可用于实现 IDictionary 接口

    Imports System.Collections.Generic
    
    Public Class DataDictionary
    Implements IDictionary(Of String, Object)
    Private _ce As CalcEngine.CalcEngine
    Private _dct As Dictionary(Of String, Object)
    
    Public Sub New(ByVal ce As CalcEngine.CalcEngine)
        _ce = ce
        _dct = New Dictionary(Of String, Object)()
    End Sub
    
    '---------------------------------------------------------------
    #Region "IDictionary<string,object> Members"
    
    Public Sub Add(ByVal key As String, ByVal value As Object) Implements Collections.Generic.IDictionary(Of String, Object).Add
        _dct.Add(key, value)
    End Sub
    Public Function ContainsKey(ByVal key As String) As Boolean Implements System.Collections.Generic.IDictionary(Of String, Object).ContainsKey
        Return _dct.ContainsKey(key)
    End Function
    Public ReadOnly Property Keys() As ICollection(Of String) Implements System.Collections.Generic.IDictionary(Of String, Object).Keys
        Get
            Return _dct.Keys
        End Get
    End Property
    Public Function Remove(ByVal key As String) As Boolean Implements System.Collections.Generic.IDictionary(Of String, Object).Remove
        Return _dct.Remove(key)
    End Function
    Public ReadOnly Property Values() As ICollection(Of Object) Implements System.Collections.Generic.IDictionary(Of String, Object).Values
        Get
            Return _dct.Values
        End Get
    End Property
    Public Function TryGetValue(ByVal key As String, ByRef value As Object) As Boolean Implements System.Collections.Generic.IDictionary(Of String, Object).TryGetValue
        If _dct.TryGetValue(key, value) Then
            Dim expr = TryCast(value, String)
            If expr IsNot Nothing AndAlso expr.Length > 0 AndAlso expr(0) = "="c Then
                value = _ce.Evaluate(expr.Substring(1))
            End If
            Return True
        End If
        Return False
    End Function
    Default Public Property Item(ByVal key As String) As Object Implements System.Collections.Generic.IDictionary(Of String, Object).Item
        Get
            Dim value As Object
            If TryGetValue(key, value) Then
                Return value
            End If
            Throw New Exception("invalid index")
        End Get
        Set(ByVal value As Object)
            _dct(key) = value
        End Set
    End Property
    #End Region
    
    '---------------------------------------------------------------
    #Region "ICollection<KeyValuePair<string,object>> Members"
    
    Public Sub Add(ByVal item As KeyValuePair(Of String, Object)) Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, Object)).Add
        Dim d = TryCast(_dct, ICollection(Of KeyValuePair(Of String, Object)))
        d.Add(item)
    End Sub
    Public Sub Clear() Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, Object)).Clear
        _dct.Clear()
    End Sub
    Public Function Contains(ByVal item As KeyValuePair(Of String, Object)) As Boolean Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, Object)).Contains
        Dim d = TryCast(_dct, ICollection(Of KeyValuePair(Of String, Object)))
        Return d.Contains(item)
    End Function
    Public Sub CopyTo(ByVal array As KeyValuePair(Of String, Object)(), ByVal arrayIndex As Integer) Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, Object)).CopyTo
        Dim d = TryCast(_dct, ICollection(Of KeyValuePair(Of String, Object)))
        d.CopyTo(array, arrayIndex)
    End Sub
    Public ReadOnly Property Count() As Integer Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, Object)).Count
        Get
            Return _dct.Count
        End Get
    End Property
    Public ReadOnly Property IsReadOnly() As Boolean Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, Object)).IsReadOnly
        Get
            Return False
        End Get
    End Property
    Public Function Remove(ByVal item As KeyValuePair(Of String, Object)) As Boolean Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, Object)).Remove
        Dim d = TryCast(_dct, ICollection(Of KeyValuePair(Of String, Object)))
        Return d.Remove(item)
    End Function
    #End Region
    
    '---------------------------------------------------------------
    #Region "IEnumerable<KeyValuePair<string,object>> Members"
    
    Public Function GetEnumerator2() As IEnumerator(Of KeyValuePair(Of String, Object)) Implements IEnumerable(Of KeyValuePair(Of String, Object)).GetEnumerator
        Return TryCast(_dct.GetEnumerator(), IEnumerator(Of KeyValuePair(Of String, Object)))
    End Function
    
    #End Region
    
    '---------------------------------------------------------------
    #Region "IEnumerable Members"
    
    Private Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
        Return TryCast(_dct.GetEnumerator(), System.Collections.IEnumerator)
    End Function
    #End Region
    End Class
    

    【讨论】:

      猜你喜欢
      • 2016-03-10
      • 1970-01-01
      • 2021-09-07
      • 2013-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 2012-07-07
      相关资源
      最近更新 更多