简体   繁体   English

将vb.net词典转换为vba词典

[英]Converting a vb.net dictionary to a vba dictionary

I was pleased to find that I could call structs that I had set up in vb.net straight into excel vba - using COM visible and registering using regasm.exe. 我很高兴发现我可以将在vb.net中设置的结构直接调用到excel vba中-使用可见的COM并使用regasm.exe进行注册。

I am struggling to do the same with a dictionary created in vb.net. 我正在努力对在vb.net中创建的字典执行相同的操作。

I found this link which suggested that the dictionary in vb.net was not the same as the runtime.scripting dictionary found in vba. 我发现此链接表明vb.net中的词典与vba中的runtime.scripting词典不同。

I was unable to have much luck with the links suggested in the comments though. 不过,我对评论中建议的链接不太满意。

Here is the vb.net code: 这是vb.net代码:

Public Function ReturnDict() As Dictionary(Of String, Integer)
    Dim dict As New Dictionary(Of String, Integer)
    dict.Add("a", 10)
    dict.Add("b", 11)
    Return dict
End Function

Here is the vba code: 这是vba代码:

Function MyReturnDict()
   Dim classLib As New MyVBClass.Class1
   Set MyDict = classLib.ReturnDict()
     \\do stuff with dictionary
   MyReturnDict = Result
End Function

Any help/advice would be much appreciated! 任何帮助/建议将不胜感激!

Hans Passant's solutions in the comments above perfectly solved the problem: 上面评论中的Hans Passant的解决方案完美地解决了这个问题:

In VB.net, either use: 在VB.net中,可以使用:

Public Function ReturnDict() As System.Collections.IDictionary

or reference scrrun.dll and: 或引用scrrun.dll和:

Public Function ReturnDict() As Scripting.Dictionary
    Dim dict As New Scripting.Dictionary

The latter solution provides a VBA dictionary that can be used as one would like. 后一种解决方案提供了VBA词典,可以按需使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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