在 VB.NET 中使用 KeyValuePair 进行反向查找字典(C# 中的示例提示需要转换)
我目前正在进行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过我通常用来将 C# 转换为 VB.NET 的工具运行了您提到的示例,网址为 www.developerfusion.co.uk/tools
正如您所看到的,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
As you can see, the Add() method comes out very slightly differently to yours.
1-正如kevinw指出的,你原来的代码是错误的,它应该是:
2-上面的代码应该在继承自
PairCollection
中插入一个新的KeyValuePair
>列表(KeyValuePair)。如果它不起作用,那么您的 Inherits 行很可能是错误的。根据错误,Add
方法需要一个键,而不是KeyValuePair
...也许您是从Dictionary
而不是继承的>列表?
1- As kevinw pointed out, your original code is just wrong, it should be:
2- The code above should insert a new
KeyValuePair
in aPairCollection
which inherits fromList(Of KeyValuePair)
. If it doesn't work, there's a good chance that your Inherits line is wrong. According to the error, theAdd
method expects a key, not aKeyValuePair
... maybe your inherited from aDictionary
instead of aList
?通过使 Add 方法重载来解决:
奇怪的是,尽管 MyBase.Add 可以工作(通过 Reflector 中的反编译发现),但我却没有(尽管“this”在 C# 中工作)。我想我将此归因于 VB.NET 中的一个怪癖?
感谢 Kevinw 和 Meta-Knight。原来的代码是错误的,但我一直都是从List继承的。
Resolved by making the Add method Overloads as such:
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.