如何在Excel VBA中使用字典Dictionary对象

如何在Excel VBA中使用字典Dictionary对象,第1张

在VBA中使用字典分为前期绑定和后期绑定两种方式,

一、前期绑定:打开VBE编辑器,按下图 *** 作,勾选相应选项就可以直接使用字典了。

二、后期绑定:如下代码即创建了一个名称为d的字典。

Set d = CreateObject("scripting.dictionary")

这是这个索引器:

public TValue this[TKey key]

{

get

{

int index = this.FindEntry(key)

if (index >= 0)

{

return this.entries[index].value

}

ThrowHelper.ThrowKeyNotFoundException()

return default(TValue)

}

set

{

this.Insert(key, value, false)

}

}

这是添加方法

public void Add(TKey key, TValue value)

{

this.Insert(key, value, true)

}

我不会发布整个insert方法,因为它很长,但是方法声明如下:

private void Insert(TKey key, TValue value, bool add)

在函数的更深处,会发生以下情况:

if ((this.entries[i].hashCode == num) &&this.comparer.Equals(this.entries[i].key, key))

{

if (add)

{

ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate)

}

IDictionary<string, string>strings = new Dictionary<string, string>()

strings["foo"] ="bar" //strings["foo"] =="bar"

strings["foo"] = string.Empty //strings["foo"] == string.empty

strings.Add("foo","bar") //throws


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11619785.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存