在 VB.NET 中使用 KeyValuePair 进行反向查找字典(C# 中的示例提示需要转换)

发布于 2024-08-08 20:36:06 字数 578 浏览 5 评论 0原文

我目前正在进行 VB.NET 项目,希望使用 KeyValuePair 来促进反向查找。

我在这里找到了一个很好的 C# 示例: http://www.dreamincode.net/forums /showtopic78080.htm,但是我在转换为 VB.NET 时遇到了一个小问题(手动和使用翻译器(在线 carlosag))。例如,我在 Add 方法中期望的语法如下:

    Public Sub Add(ByVal key As TKey, ByVal value As TValue)
        Me.Add(New KeyValuePair(Of Tkey(key, value))
    End Sub

然而这告诉我“‘System.Collections.Generic.KeyValuePair(Of TKey, TValue)’的类型参数太少”

任何帮助肯定会有所帮助(事实上,包括匿名方法的示例的完整翻译也会有所帮助:D。

I'm currently on a VB.NET project and wish to use a KeyValuePair to facilitate a reverse lookup.

I've found a great example in C# here: http://www.dreamincode.net/forums/showtopic78080.htm, however I am having a small problem converting to VB.NET (both manually and using a translator (online carlosag)). For example the syntax I would expect in the Add method is as follows:

    Public Sub Add(ByVal key As TKey, ByVal value As TValue)
        Me.Add(New KeyValuePair(Of Tkey(key, value))
    End Sub

Whereas this tells me "Too few type arguments to 'System.Collections.Generic.KeyValuePair(Of TKey, TValue)'"

Any assistance would sure be helpful (indeed as would a full translation of the example including anon methods :D.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑饮青盏花 2024-08-15 20:36:06

我通过我通常用来将 C# 转换为 VB.NET 的工具运行了您提到的示例,网址为 www.developerfusion.co.uk/tools

Imports System 
Imports System.Collections.Generic 
Imports System.Text 
Namespace ConsoleApplication1 
    Class PairCollection(Of TKey, TValue) 
        Inherits List(Of KeyValuePair(Of TKey, TValue)) 
        Public Sub Add(ByVal key As TKey, ByVal value As TValue) 
            Me.Add(New KeyValuePair(Of TKey, TValue)(key, value)) 
        End Sub 
        Public Function FindByKey(ByVal key As TKey) As List(Of KeyValuePair(Of TKey, TValue)) 
            Return Me.FindAll(Function(ByVal item As KeyValuePair(Of TKey, TValue)) (item.Key.Equals(key))) 
        End Function 
        Public Function FindByValue(ByVal value As TValue) As List(Of KeyValuePair(Of TKey, TValue)) 
            Return Me.FindAll(Function(ByVal item As KeyValuePair(Of TKey, TValue)) (item.Value.Equals(value))) 
        End Function 
    End Class 
    Class Program 
        Private Shared Sub Main(ByVal args As String()) 
            Dim menu As New PairCollection(Of String, Double)() 
            menu.Add("Burger", 3.5R) 
            menu.Add("Hot Dog", 2.25) 
            menu.Add("Fries", 1.75) 
            Console.WriteLine(menu.FindByKey("Fries")(0)) 
            Console.ReadLine() 
        End Sub 
    End Class 
End Namespace 

正如您所看到的,Add() 方法的结果与您的方法略有不同。

I ran the example you referred to through the tool I usually use to convert C# to VB.NET at www.developerfusion.co.uk/tools

Imports System 
Imports System.Collections.Generic 
Imports System.Text 
Namespace ConsoleApplication1 
    Class PairCollection(Of TKey, TValue) 
        Inherits List(Of KeyValuePair(Of TKey, TValue)) 
        Public Sub Add(ByVal key As TKey, ByVal value As TValue) 
            Me.Add(New KeyValuePair(Of TKey, TValue)(key, value)) 
        End Sub 
        Public Function FindByKey(ByVal key As TKey) As List(Of KeyValuePair(Of TKey, TValue)) 
            Return Me.FindAll(Function(ByVal item As KeyValuePair(Of TKey, TValue)) (item.Key.Equals(key))) 
        End Function 
        Public Function FindByValue(ByVal value As TValue) As List(Of KeyValuePair(Of TKey, TValue)) 
            Return Me.FindAll(Function(ByVal item As KeyValuePair(Of TKey, TValue)) (item.Value.Equals(value))) 
        End Function 
    End Class 
    Class Program 
        Private Shared Sub Main(ByVal args As String()) 
            Dim menu As New PairCollection(Of String, Double)() 
            menu.Add("Burger", 3.5R) 
            menu.Add("Hot Dog", 2.25) 
            menu.Add("Fries", 1.75) 
            Console.WriteLine(menu.FindByKey("Fries")(0)) 
            Console.ReadLine() 
        End Sub 
    End Class 
End Namespace 

As you can see, the Add() method comes out very slightly differently to yours.

冰之心 2024-08-15 20:36:06

1-正如kevinw指出的,你原来的代码是错误的,它应该是:

Me.Add(New KeyValuePair(Of TKey, TValue)(key, value))

2-上面的代码应该在继承自PairCollection中插入一个新的KeyValuePair >列表(KeyValuePair)。如果它不起作用,那么您的 Inherits 行很可能是错误的。根据错误,Add 方法需要一个键,而不是 KeyValuePair...也许您是从 Dictionary 而不是 继承的>列表?

1- As kevinw pointed out, your original code is just wrong, it should be:

Me.Add(New KeyValuePair(Of TKey, TValue)(key, value))

2- The code above should insert a new KeyValuePair in a PairCollection which inherits from List(Of KeyValuePair). If it doesn't work, there's a good chance that your Inherits line is wrong. According to the error, the Add method expects a key, not a KeyValuePair... maybe your inherited from a Dictionary instead of a List?

ζ澈沫 2024-08-15 20:36:06

通过使 Add 方法重载来解决:

Public Overloads Sub Add(ByVal key As TKey, ByVal value As TValue)
   Me.Add(New KeyValuePair(Of TKey, TValue)(key, value))
End Sub

奇怪的是,尽管 MyBase.Add 可以工作(通过 Reflector 中的反编译发现),但我却没有(尽管“this”在 C# 中工作)。我想我将此归因于 VB.NET 中的一个怪癖?

感谢 Kevinw 和 Meta-Knight。原来的代码是错误的,但我一直都是从List继承的。

Resolved by making the Add method Overloads as such:

Public Overloads Sub Add(ByVal key As TKey, ByVal value As TValue)
   Me.Add(New KeyValuePair(Of TKey, TValue)(key, value))
End Sub

Strange that although MyBase.Add works (found via a decompile in Reflector) Me didn't (despite "this" working in C#). I guess I put this down to a quirk in VB.NET?

Thanks Kevinw and Meta-Knight. Original code was wrong, but I was inheriting from List all along.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文