VBA如何从C#调用返回类的构造函数

时间:2018-09-28 07:29:01

标签: c# excel vba excel-vba

我在C#中有一个带有构造函数的类

namespace CollectionHandler
{
    public class Dict
    {
        private Scripting.Dictionary dict = new Scripting.Dictionary();

        public Dict()
        {
            dict.Add(1, "Carl Johnson");
            dict.Add(2, "Leon");
            dict.Add(3, "Claire");
            dict.Add(4, "Jill");
            dict.Add(5, "Chris");
            dict.Add(6, "Vrad");
            dict.Add(7, "Aida");
            dict.Add(8, "Ryder");
            dict.Add(9, "Snake");
        }

        public Scripting.Dictionary ReturnDict()
        {
            return dict;
        }
    }
}

在VBA中,这些是我的代码。

Public Sub ShowDictionaries()
    Dim msg As String

    Dim pdict As Dictionary
    Set pdict = New Dictionary

    Dim x As CollectionHandler.Dict
    Set x = New CollectionHandler.Dict ' I thought this is the constructor

    Dim ctr As Long


    Dim key As Variant
    ctr = 0
    For Each key In x.ReturnDict().Keys
        ctr = ctr + 1
        pdict.Add key, x.ReturnDict()(key)
        Dim str1 As String
        str1 = "Key: " & ctr & " Value: " & pdict(ctr) & vbNewLine
        msg = msg & str1
    Next

    MsgBox msg, vbInformation, "We Are Items From C#"


End Sub

由于C#中的词典项列表在构造函数中,因此消息框显示为空白,如何调用CollectionHandler.Dict的构造函数?

0 个答案:

没有答案