无法分配给从VBA使用的C#类中的哈希表项

无法分配给从VBA使用的C#类中的哈希表项,c#,vba,com,com-interop,C#,Vba,Com,Com Interop,我在VBA中编写了一个C#类(KvpHash),它提供了有关哈希表的其他有用功能。在我的VBA代码中,我为KvpHash类提供了一个扩展的RuberDuck测试套件,该套件显示了该类的所有功能均按预期工作,但我无法更改项的值这一事实除外 从VBA我得到了错误消息 424“需要对象” 在C#类中,接口代码是 dynamic this[dynamic Key] { get; set; } 而且实现是非常简单的 public dynamic this[dynamic Key] { get

我在VBA中编写了一个C#类(KvpHash),它提供了有关哈希表的其他有用功能。在我的VBA代码中,我为KvpHash类提供了一个扩展的RuberDuck测试套件,该套件显示了该类的所有功能均按预期工作,但我无法更改项的值这一事实除外

从VBA我得到了错误消息

424“需要对象”

在C#类中,接口代码是

dynamic this[dynamic Key] { get; set; }
而且实现是非常简单的

public dynamic this[dynamic Key]
{
    get
    {
        return MyKvpHash[Key];
    }
    set
    {
        MyKvpHash[Key] = value;
    }
}
其中MYKVPHAH定义为

private Hashtable MyKvpHash = new Hashtable();
如果我将mscorelib引用添加到VBA,我可以直接在VBA中创建一个哈希表,在这里完全可以更改哈希表中某个项的值

我希望有人能指点我在C#代码中犯了什么错误,这会导致所需的对象错误

已编辑:添加示例VBA代码

使用本机哈希表

Public Sub TestHashtable()
' requires reference to mscorlib.dll

Dim myHt As Hashtable

    Set myHt = New Hashtable
    myHt.Add 5, "Hello5"
    myHt.Add 10, "Hello10"

    Debug.Print myHt.Item(10)
    Debug.Print myHt.Item(5)

    ' the next line works as expected
    myHt.Item(10) = "A new world"
    Debug.Print myHt.Item(10)

End Sub
Public Sub TestKvpHash()

Dim myHt As VBAExtensions.KvpHash
' KvpHash is a C# wrapper for a System.Collections.HashTable

    Set myHt = New KvpHash
    myHt.AddByKey 5, "Hello5"
    myHt.AddByKey 10, "Hello10"

    Debug.Print myHt.Item(10)
    Debug.Print myHt.Item(5)

    ' The next line produces error 424
    myHt.Item(10) = "A new world"
    Debug.Print myHt.Item(10)

End Sub
给出输出

Hello10
Hello5
A new world
Hello10
Hello5
使用我的KvpHash类(哈希表的包装器)

给出输出

Hello10
Hello5
A new world
Hello10
Hello5
然后以424错误停止

按要求编辑以添加完整的C#代码

似乎没有文件托管,我也没有其他方法提供链接,所以我在下面插入了相关代码。该代码最初基于字典,但当我第一次发现无法分配到某个项时,我将其更新为哈希表。那个开关并没有改变我代码的行为。请注意,我不是一名专业程序员,所提供的代码基本上是我第一次进入C。通常,我编写Word VBA宏供自己使用

// VBAExtensions
//
// C# Library module for VBA

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;


namespace VBAExtensions
{
    /// <summary>
    /// Enum for accessing the kvphash structure returned by Method Cohorts
    /// </summary>

    public enum CohortType 
    {
        /// <summary>1 = the keys in A plus keys in B that are not shared</summary>
        KeysInAAndOnlyB = 1,
        /// <summary>2 = the Keys from B in A where B has a different value to A</summary>
        KeysInAandBWithDifferentValues,
        /// <summary>3 = the keys that are only in A and only in B</summary>
        KeysNotInAandB,
        /// <summary>4 = the keys that are inA and  B </summary>
        KeysInAandB,
        /// <summary>5 = the keys in A only   </summary>
        KeysInAOnly,
        /// <summary>6 = the keys in B only</summary>
        KeysInBOnly
    }

/// <summary>
    /// KvpHash is a C# class for VBA which implements a Key/Value HashTable
    /// The object is a morer flexible version of the Scripting.Dictionary
    /// </summary>
    [Guid("30F9294B-11B4-4D91-9D7C-7FF02ADB3F11")]
    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IKvpHash
    {


        /// <summary>
        /// Returns/Sets the "Value" specified by "Key" (i) of a Key/Value Pair
        /// </summary>
        /// <param name="Key"></param>
        /// <returns>Type used in Set statement (C# dynamic)</returns> 
        dynamic this[dynamic Key] { get; set; }

        /// <summary>
        /// Adds "Value" to the KvpHash using an integer (VBA Long) Key.
        /// The integer key is based on the first available integer greater than or
        /// equal to the Count of the KvpHash
        /// </summary>
        /// <param name="Value"></param>
        void AddByIndex(dynamic Value);

        /// <summary>
        /// Populates this KvpHash using AddByIndex for each character in the string
        /// </summary>
        /// <param name="this_string"></param>
        void AddByIndexAsChars(string this_string);

        /// <summary>
        /// Pupulates this KvpHash using AddByIndex for each substring in this_string delineated by this_seperator
        /// </summary>
        /// <param name="this_string"></param>
        /// <param name="this_seperator"></param>
        void AddByIndexAsSubStr(string this_string, string this_seperator = ",");

        /// <summary>
        /// Pupulates a KvpHash using AddByIndex for each array item
        /// </summary>
        /// <param name="this_array"></param>
        void AddByIndexFromArray(dynamic this_array);

        /// <summary>
        /// Adds "Value" to the KvpHash with a key pf "Key"
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="Value"></param>
        void AddByKey(dynamic Key, dynamic Value);

        /// <summary>
        /// Groups the keys of the two KvpHash
        /// </summary>
        /// <param name="ArgKvpHash"></param>
        /// <returns>An array of 6 KvpHash
        /// keys in a {1,2,3,4,5,6}
        /// keys in b {1,2,3,6,7,8}
        /// 1 = the keys in A plus keys in B that are not shared            {1,2,3( from A),4,5,6,7,8}
        /// 2 = the Keys from B in A where B has a different value to A     {3( from B) if value is different}
        /// 3 = the keys that are only in A and only in B                   {4,5,7,8}
        /// 4 = the keys that are in A and  B                               {1,2,3,6}
        /// 5 = the keys in A only                                          {4,5}
        /// 6 = the keys in B only                                          {7,8}
        /// </returns>
        KvpHash Cohorts(KvpHash ArgKvpHash);

        /// <summary>
        /// The number of key/vaue pairs in the KvpHash
        /// </summary>
        /// <returns>Long</returns>
        int Count();

        ///// <summary>
        ///// Return the IEnumerator interface for KvpHash
        ///// </summary>
        ///// <returns>IEnumerator</returns>
        //IEnumerator GetEnumerator();

        /// <summary>
        /// Gets the "Key" for the first ocurrence of "Value" in the KvpHash.
        /// </summary>
        /// <param name="Value"></param>
        /// <returns>Key</returns>
        dynamic GetKey(dynamic Value);

        /// <summary>
        /// Returns a variant array of the Keys of the KvpHash
        /// </summary>
        /// /// <returns>Variant Array</returns>
        dynamic[] GetKeys();

        /// <summary>
        /// Returns a variant array of the values of the KvpHash
        /// </summary>
        /// <returns>Variant Array</returns>
        dynamic[] GetValues();

        /// <summary>
        /// True if the "Key" exists in the keys of the KvpHash
        /// </summary>
        /// <param name="Key"></param>
        /// <returns>Boolean</returns>
        bool HoldsKey(dynamic Key);

        /// <summary>
        /// True if the "Value" exists in the values of the KvpHash
        /// </summary>
        /// <param name="Value"></param>
        /// <returns>Boolean</returns>
        bool HoldsValue(dynamic Value);

        /// <summary>
        /// True if the KvpHash holds 0 key/value pairs
        /// </summary>
        /// <returns>Boolean</returns>
        bool IsEmpty();

        /// <summary>
        /// True if the KvpHash holds one or more key/value pairs
        /// </summary>
        /// <returns>Boolean</returns>
        bool IsNotEmpty();

        /// <summary>
        /// True is the "Key" is not found in the keys of the KvpHash
        /// </summary>
        /// <param name="Key"></param>
        /// <returns>Boolean</returns>
        bool LacksKey(dynamic Key);

        /// <summary>
        /// True if the "Value" is not found in the values of the KvpHash
        /// </summary>
        /// <param name="Value"></param>
        /// <returns>Boolean</returns>
        bool LacksValue(dynamic Value);

        /// <summary>
        /// Reverses the Key/Value pairs in a KvpHash
        /// </summary>
        /// <returns>New KvpHash where: 
        ///     KvpHash.Value(1) = KvpHash Unique values as Value/Key pairs 
        ///     KvpHash.Value(2) = KvpHash Non unique values as Key/Value pairs</returns>
        KvpHash Mirror();

        /// <summary>
        /// Removes the Key/Value pair spacified by "Key" from the KvpHash
        /// </summary>
        /// <param name="Key"></param>
        void Remove(dynamic Key);

        /// <summary>
        /// Removes all Key/Value pairs from the KvpHash
        /// </summary>
        void RemoveAll();

        /// <summary>
        /// Returns true if the Values in KvpHash are unique.
        /// </summary>
        /// <returns>Boolean</returns>
        bool ValuesAreUnique();

        /// <summary>
        /// Returns true if the Values in KvpHash are not unique.
        /// </summary>
        /// <returns>Boolean</returns>
        bool ValuesAreNotUnique();
    }

    [Guid("87E5A539-FDB3-40D0-9CCD-C817F9893C08")]
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class KvpHash : IKvpHash, IEnumerable
    {

        private Hashtable MyKvpHash = new Hashtable();

        public dynamic this[dynamic Key]
        {
            get
            {
                return MyKvpHash[Key];
            }
            set
            {
                MyKvpHash[Key] = value;
            }
        }

        public void AddByIndex(dynamic Value)
        {
            int my_index = MyKvpHash.Count + 1;
            while (MyKvpHash.ContainsKey(my_index))
            {
                my_index++;
            }
            MyKvpHash.Add(my_index, Value);
        }

        public void AddByIndexAsChars(string this_string)
        {
            int my_index = MyKvpHash.Count + 1;
            while (MyKvpHash.ContainsKey(my_index))
            {
                my_index++;
            }
            char[] MyArray = this_string.ToCharArray();
            MyKvpHash.Clear();
            for (int i = 0; i <= MyArray.GetUpperBound(0); i++)
            {
                //KvpHash uses ordinal indexes
                MyKvpHash.Add(i + 1, MyArray[i].ToString());
            }
        }

        public void AddByIndexAsSubStr(string this_string, string this_seperator = ",")
        {
            int my_index = MyKvpHash.Count + 1;
            while (MyKvpHash.ContainsKey(my_index))
            {
                my_index++;
            }
            string[] MyArray = this_string.Split(this_seperator.ToArray());
            for (int i = 0; i <= MyArray.GetUpperBound(0); i++)
            {
                //KvpHash uses ordinal indexes
                MyKvpHash.Add(i + 1, MyArray[i]);
            }
        }

        public void AddByIndexFromArray(dynamic this_array)
        {
            int my_index = MyKvpHash.Count + 1;
            while (MyKvpHash.ContainsKey(my_index))
            {
                my_index++;
            }
            for (int i = 0; i <= this_array.GetUpperBound(0); i++)
            {
                //KvpHash uses ordinal indexes
                MyKvpHash.Add(i + 1, this_array[i]);
            }
        }

        public void AddByKey(dynamic Key, dynamic Value)
        {
            MyKvpHash.Add(Key, Value);
        }

        public KvpHash Cohorts(KvpHash ArgKvpHash)
        {
            KvpHash ResultKvpHash = new KvpHash();
            // VBA reports object not set error if the resuly KvpHash are not newed
            for (int i = 1; i < 7; i++)
            {
                ResultKvpHash.AddByKey(i, new KvpHash());
            }

            foreach (DictionaryEntry MyKvpHashPair in MyKvpHash)
            {
                // A plus unique in B
                ResultKvpHash[1].AddByKey(MyKvpHashPair.Key, MyKvpHashPair.Value);

                if (ArgKvpHash.LacksKey(MyKvpHashPair.Key))  // problem is here
                {
                    // In A only or in B only
                    ResultKvpHash[3].AddByKey(MyKvpHashPair.Key, MyKvpHashPair.Value);
                    // In A only
                    ResultKvpHash[5].AddByKey(MyKvpHashPair.Key, MyKvpHashPair.Value);
                }
                else
                {
                    // In A and In B
                    ResultKvpHash[4].AddByKey(MyKvpHashPair.Key, MyKvpHashPair.Value);
                }
            }

            foreach (dynamic MyKey in ArgKvpHash.GetKeys())
            {
                // B in A with different value
                if (ResultKvpHash[1].LacksKey(MyKey))  // Result 0 will contain all of A
                {
                    ResultKvpHash[1].AddByKey(MyKey, ArgKvpHash[MyKey]);
                    ResultKvpHash[3].AddByKey(MyKey, ArgKvpHash[MyKey]);
                    ResultKvpHash[6].AddByKey(MyKey, ArgKvpHash[MyKey]);
                }
                else
                {
                    if (ResultKvpHash[1][MyKey] != ArgKvpHash[MyKey])
                    {
                        ResultKvpHash[2].AddByKey(MyKey, ArgKvpHash[MyKey]);
                    }
                }
            }
            return ResultKvpHash;
        }

        public Int32 Count()
        {
            return MyKvpHash.Count;
        }

        public bool IsEmpty()
        {
            return MyKvpHash.Count == 0;
        }

        public bool IsNotEmpty()
        {
            return !IsEmpty();
        }

        public IEnumerator GetEnumerator()
        {
            foreach (DictionaryEntry my_pair in MyKvpHash)
            {
                yield return my_pair.Value;
            }
        }

        public dynamic GetKey(dynamic Value)
        {
            return this.Mirror()[1][Value];
        }

        public dynamic[] GetKeys()

        {
            return (dynamic[]) MyKvpHash.Keys;
        }

        public dynamic[] GetValues()
        {
            return (dynamic[]) MyKvpHash.Values;
        }

        public bool HoldsKey(dynamic Key)
        {
            return MyKvpHash.ContainsKey(Key);
        }

        public bool HoldsValue(dynamic Value)
        {
            return MyKvpHash.ContainsValue(Value);
        }

        public bool LacksKey(dynamic Key)
        {
            return !HoldsKey(Key);
        }

        public bool LacksValue(dynamic Value)
        {
            return !HoldsValue(Value);
        }

        public KvpHash Mirror()
        {
            KvpHash MyResult = new KvpHash();
            MyResult.AddByIndex(new KvpHash());
            MyResult.AddByIndex(new KvpHash());
            foreach (DictionaryEntry my_pair in MyKvpHash)
            {
                if (MyResult[1].LacksKey(my_pair.Value))
                {
                    MyResult[1].AddByKey(my_pair.Value, my_pair.Key);
                }
                else
                {
                    MyResult[2].AddByKey(my_pair.Key, my_pair.Value);
                }
            }
            return MyResult;
        }

        public void Remove(dynamic Key)
        {
            MyKvpHash.Remove(Key);
        }


        public void RemoveAll()
        {
            MyKvpHash.Clear();
        }

        public bool ValuesAreUnique()
        {
            return MyKvpHash.Count == ((dynamic[]) MyKvpHash.Values).Distinct().Count();
        }

        public bool ValuesAreNotUnique()
        {
            return !ValuesAreUnique();
        }
    }    
}   
//VBAExtensions
//
//VBA的C#库模块
使用制度;
使用系统集合;
使用System.Collections.Generic;
使用System.Runtime.InteropServices;
使用System.Linq;
命名空间VBAExtensions
{
/// 
///用于访问方法队列返回的kvphash结构的枚举
/// 
公共枚举共生类型
{
///1=A中的键加上B中未共享的键
keysinandonlyb=1,
///2=A中B的键,其中B与A的值不同
具有不同值的键ina和b,
///3=仅在A和B中的钥匙
基诺蒂纳和B,
///4=inA和B键
凯西南德,
///5=仅A中的钥匙
仅限Keysina,
///6=仅B中的键
按键输入
}
/// 
///KvpHash是VBA的一个C#类,它实现了一个键/值哈希表
///该对象是Scripting.Dictionary的一个更灵活的版本
/// 
[Guid(“30F9294B-11B4-4D91-9D7C-7FF02ADB3F11”)]
[ComVisible(true)]
[接口类型(ComInterfaceType.InterfaceIsDual)]
公共接口IKvpHash
{
/// 
///返回/设置由键/值对的“键”(i)指定的“值”
/// 
/// 
///Set语句中使用的类型(C#动态)
动态此[动态键]{get;set;}
/// 
///使用整数(VBA Long)键向KvpHash添加“值”。
///整数键基于第一个大于或的可用整数
///等于KvpHash的计数
/// 
/// 
void AddByIndex(动态值);
/// 
///使用AddByIndex为字符串中的每个字符填充此KvpHash
/// 
/// 
void addbyindexashars(字符串此_字符串);
/// 
///使用AddByIndex为该\u分隔符所描绘的该\u字符串中的每个子字符串生成该KvpHash
/// 
/// 
/// 
void AddByIndexAsSubStr(字符串this_string,字符串this_separator=“,”);
/// 
///使用AddByIndex为每个数组项生成KvpHash
/// 
/// 
void AddByIndexFromArray(动态此_数组);
/// 
///使用键pf“key”向KvpHash添加“值”
/// 
/// 
/// 
void AddByKey(动态键、动态值);
/// 
///将两个KvpHash的键分组
/// 
/// 
///6 KvpHash的阵列
///a{1,2,3,4,5,6}中的键
///b{1,2,3,6,7,8}中的键
///1=A中的键加上B中未共享的键{1,2,3(来自A),4,5,6,7,8}
///2=A中B的键,其中B与A的值不同{3(来自B),如果值不同}
///3=仅在A中且仅在B{4,5,7,8}中的键
///4=A和B{1,2,3,6}中的键
///5=仅{4,5}中的键
///6=仅B{7,8}中的键
/// 
KvpHash队列(KvpHash ArgKvpHash);
/// 
///KvpHash中的键/值对数
/// 
///长的
int Count();
///// 
/////返回KvpHash的IEnumerator接口
///// 
/////电子计算器
//IEnumerator GetEnumerator();
/// 
///获取KvpHash中第一次出现的“Value”的“Key”。
/// 
/// 
///钥匙
动态GetKey(动态值);
/// 
///返回KvpHash键的变体数组
/// 
//////变量数组
动态[]GetKeys();
/// 
///返回KvpHash值的变量数组
/// 
///变量数组
动态[]GetValues();
/// 
///如果KvpHash的键中存在“键”,则为True
/// 
/// 
///布尔值
bool-HoldsKey(动态键);
/// 
///如果KvpHash的值中存在“值”,则为True
/// 
/// 
///布尔值
布尔保持值(动态值);
/// 
///如果KvpHash包含0个键/值对,则为True
/// 
///布尔值
博