Thus, using the Item property with a non-existent key will not generate an error. Also, if you attempt to retrieve an item associated with a non-existent key, the Dictionary will add a blank item, associated with that key. If you use the Item property to attempt to set an item for a non-existent key, the Dictionary will implicitly add that item along with the indicated key. Item("SomeKey") = "foo" MsgBox "The value for 'SomeKey' is '" &. The Item property retrieves or sets an item associated with an indicated key: On Error Resume Next x = M圜ollection("foo") If Err = 0 Then MsgBox x Else Err.Clear MsgBox "There is no value associated with 'foo'" End If On Error GoTo 0 To do this, you must attempt to retrieve an item from the Collection using that key, and trap any error resulting from the key no existing: The Collection has no analogous method to check for existence of a specified key. For example, this snippet tests for existence of a key before adding a new item to the Dictionary: The Exists method checks for the existence of a specified key in the Dictionary, and returns boolean True if that key exists and False if not. MsgBox "There are " & MyDictionary.Count & " items" If there are no items, then it returns zero. The Count property returns a simple count of the items currently in the Dictionary. While if the CompareMode is equal to 1, the Dictionary would see the two key values as being the same, and thus generate an error. Thus, here the Dictionary will see the two prospective keys as distinct: MyDictionary.CompareMode = vbBinar圜ompare 'case sensitive MyDictionary.CompareMode = vbTextCompare 'case insensitive Because these values match those of certain built-in VBA constants, you can also use: The default value is zero, which is case sensitive use 1 to make the Dictionary case insensitive. The CompareMode indicates whether the Dictionary will be case-sensitive with respect to key strings. The key must be unique if you attempt to add an item using a duplicate key, you will get a runtime error.īy default, keys are case sensitive to change this, let the CompareMode equal 1. The key can be any data type, but cannot be an array. The item can be anything: any data type, an object (including another Dictionary), or even an array. The Add method adds an item to the Dictionary, and associates that item with a key: The Dictionary class has four properties and six methods, as discussed below. That said, even when the finished product will use late binding to enhance potential multi-version support, developers will often enable early binding during the development process to gain access to Intellisense, and then convert the code to late binding when preparing the code for release to end users. With late binding, you do not set an explicit reference to an external library, and thus when declaring your variables you would use the more generic Object type, and to instantiate a class from that library you would have to use CreateObject:ĭim MyDictionary As Object Set MyDictionary = CreateObject("Scripting.Dictionary")ĭevelopers typically use late binding when there is a possibility that the version of an external library on the development computer may be different from the version(s) installed on the various end-user computers in many cases the VBA runtime engine will gracefully manage the difference with early binding, but late binding tends to be more robust in that regard. Select Microsoft Scripting Runtime from the list of installed libraries, and click OK. To add the reference to your VBA project, go to the VB Editor, and select Tools -> References from the menu. For example, with early binding, you can declare and create your Dictionary this way:ĭim MyDictionary As Scripting.Dictionary Set MyDictionary = New Scripting.DictionaryĮarly binding also enables Intellisense, or auto-completion, as you type your code, and projects using early binding will usually have faster performance than projects using late binding. With early binding, you explicitly add a reference to the Microsoft Scripting Runtime library, which then allows you free access to the classes, constants, properties, methods, etc. However, even for relatively simple needs, a Dictionary may offer a significant performance advantage, as suggested by the analysis at the end of this article.įinally, if there is any possibility that your code will have to be ported to VBScript, you may want to consider using a Dictionary instead of a Collection: Collections do not exist in VBScript, so any code using Collections would have to have them converted to Dictionaries anyway.īecause the Dictionary class is not part of the VBA library, to use Dictionaries in your VBA projects you must either use early binding or late binding. Then using a Dictionary object offers a compelling alternative to a Collection. Be able to accommodate changes in items and/or keys
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |