无法分配给从VBA使用的C#类中的Hashtable项目

问题描述 投票:2回答:1

我已经编写了一个C#类(KvpHash)以便在VBA中使用,它提供了围绕Hashtable的其他有用功能。在我的VBA代码中,我为KvpHash类提供了一个扩展的Rubberduck测试套件,该套件按预期显示了类工作的所有功能,但事实是我无法更改项目的值。

从VBA收到错误消息

424'必需的对象'

在C#类中,接口代码为

dynamic this[dynamic Key] { get; set; }

实现是

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

其中MyKvpHash定义为

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

给出输出

Hello10
Hello5
A new world

使用我的KvpHash类(HashTable的包装器)

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

然后停止并显示424错误。

根据要求编辑以添加完整的C#代码。

似乎没有文件托管,并且我没有提供链接的另一种方法,所以我在下面插入了相关代码。该代码最初是基于Dictionary的,但是当我第一次发现我无法分配给某项时,我将其更新为Hashtable。该开关并没有改变我的代码的行为。请注意,我不是专业程序员,提供的代码本质上是我第一次涉足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();
        }
    }    
}   
c# vba com com-interop
1个回答
0
投票

@@ Freeflow,如果您在Word模块中更改myHt定义,可以,它将正常运行。

Public Sub TestKvpHash()

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

    Set myHt = New VBAExtensions.KvpHash
    ' Rest of code
© www.soinside.com 2019 - 2024. All rights reserved.