<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Internet World &#187; dictionary</title>
	<atom:link href="http://www.net-func.com/tag/dictionary/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.net-func.com</link>
	<description>Information &#38; Communication Technology</description>
	<lastBuildDate>Tue, 07 Feb 2012 09:35:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<image>
<link>http://www.net-func.com</link>
<url>http://www.net-func.com/wp-content/mbp-favicon/Internet-icon.png</url>
<title>Internet World</title>
</image>
		<item>
		<title>Create_the Dictionary Object&#8230;. Please Consider</title>
		<link>http://www.net-func.com/how-to/create_the-dictionary-object-please-consider/</link>
		<comments>http://www.net-func.com/how-to/create_the-dictionary-object-please-consider/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 06:31:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[dictionary]]></category>
		<category><![CDATA[vbscript]]></category>

		<guid isPermaLink="false">http://net-func.com/?p=437</guid>
		<description><![CDATA[Creating the dictionary Create a dictionary using VBscript Okay, before we&#8217;re talk about the dictionary, I&#8217;ll admit I&#8217;m a bit of a fan of the Array. You either love or hate an Array. People who dislike the Array will often opt for a Collection instead. Other languages do provide a really cool object called a [...]]]></description>
			<content:encoded><![CDATA[<h2>Creating the dictionary</h2>
<h3>Create a dictionary using VBscript</h3>
<p>Okay, before we&#8217;re talk about the <strong>dictionary</strong>, I&#8217;ll admit I&#8217;m a bit of a fan of the Array. You either love or hate an Array. People who dislike the Array will often opt for a Collection instead. Other languages do provide a really cool object called a Hash Table. This is like a Collection that behaves like a Collection combined with an Array with some extra handy methods. VBA does not have this but VBScript does provide an object, which is cool, and we can make use of this object within our VBA <a href="http://www.net-func.com/computer/computer-security-encryption-becoming-more-vital/">environment</a>. To build a the object do the following:</p>
<p>Dim my_dictionary as Object<br />
Set my_dictionary = CreateObject (&#8220;Scripting.Dictionary&#8221;)</p>
<p>Voila! We have a dicti-o-nary. What can we do with it? We can add items, check for the existence of items, return an array of keys, return an array of items, set how a dicti-o-nary compares keys and get the count and so on. An example:</p>
<p>&#8216;First create the Object<br />
Dim my_dictionary as Object<br />
Set my_dictionary = CreateObject (&#8220;Scripting.Dictionary&#8221;)</p>
<p><img class="alignleft size-full wp-image-1657" title="dictionary" src="http://www.net-func.com/wp-content/uploads/dictnry.gif" alt="dictionary" width="155" height="200" /></p>
<p><span id="more-437"></span></p>
<p>&#8216;When adding an object or value to a this you put the key first and the actual value or object second. The key is mandatory and you cannot add items without it.<br />
my_dicti onary.Add &#8220;Key 1&#8243;, &#8220;Value 1&#8243;<br />
my_dicti onary.Add &#8220;Key 2&#8243;, &#8220;Value 2&#8243;<br />
my_dicti onary.Add &#8220;Key 3&#8243;, &#8220;Value 3&#8243;<br />
my_dicti onary.Add &#8220;Key 4&#8243;, &#8220;Value 4&#8243;</p>
<p>&nbsp;</p>
<p>You couldn&#8217;t do that with a collection! In a collection you would have to remove one item and add another, thus losing the order or your items. A dictionary behaves like an Array in this respect. What if we were not sure there was a key called &#8220;Key 3&#8243; within the object and wanted to avoid an error. Again, easy, we just use the Exists method of the object:</p>
<p>if my_dictionary.Exists(&#8220;Key 3&#8243;) then<br />
my_dictionary.Item(&#8220;Key 3&#8243;) = &#8220;Zebra&#8221;<br />
else<br />
my_dictionary.Add &#8220;Key 3&#8243;, &#8220;Zebra&#8221;<br />
end if</p>
<p>If you want to iterate through the items in a dicti-o-nary, you can&#8217;t use an integer counter as you would an Array or Collection but you can use a method to do so:</p>
<p>&#8216;Iterate through the array of items. These items can include objects as well.<br />
Dim separate_item as Variant<br />
For Each separate_item in items<br />
MsgBox separate_item<br />
Next separate_item</p>
<p>These are the basics. I&#8217;ll look at the CompareMode method in un minuto. The object is a real advantage when we need to build a Collection of Collections or a Class Collection. For example; say we had to collect data on spies and their current missions. Usually we would have to create a Class Object called Spy and hold a Private or Public Collection within the class to which we would add their missions. One class too many (A Collection is a Class)! Let&#8217;s use a an object instead&#8230;</p>
<p>Dim my_dictionary As Object Dim missions As Collection Dim spy_name As String Dim keys, key As Variant Set my_dictionary = CreateObject(&#8220;Scripting.Dictionary&#8221;)</p>
<p>&#8216;Add three lots of spies.<br />
Set missions = New Collection<br />
spy_name = &#8220;Alexander Poligraphovich&#8221;<br />
missions.Add &#8220;Vladivostok&#8221;<br />
missions.Add &#8220;Ukraine&#8221;<br />
missions.Add &#8220;Beijing&#8221;<br />
my_dictionary.Add spy_name, missions</p>
<p>spy_name = &#8220;Mohammed Ramadan&#8221;<br />
Set missions = New Collection<br />
missions.Add &#8220;Munich&#8221;<br />
missions.Add &#8220;Tehran&#8221;<br />
missions.Add &#8220;Sydney&#8221;<br />
my_dictionary.Add spy_name, missions</p>
<p>spy_name = &#8220;Sri FitzPatrick&#8221;<br />
Set missions = New Collection<br />
missions.Add &#8220;Dublin&#8221;<br />
missions.Add &#8220;San Francisco&#8221;<br />
my_dictionary.Add spy_name, missions</p>
<p>keys = my_dictionary.Keys<br />
For Each key In Keys<br />
MsgBox key &amp; vbCrLf &amp; _<br />
my_dictionary(key).item(1) &amp; vbCrLf &amp; _<br />
my_dictionary(key).item(2) &amp; vbCrLf &amp; _<br />
my_dictionary(key).item(3)<br />
Next key</p>
<p>On this example we don&#8217;t even get to the Msgbox, instead we get an error stating &#8220;This Key is already associated with an element of this collection.&#8221;. This stops two keys being added that have the same name. vbBinaryCompare behaves the same way as the first example does (it is the default) and vbDatabaseCompare.Well I read what it did once and never had to remember it again! You can find explanations for these, albeit very succinct, within the MS Help in Access, or better still Google it.</p>
<p>Hopefully this gives you an extra tool alongside the Collection or Array and some ideas on future use. A Dictionary makes code structure cleaner and more humanly understandable and this VBScript tool will really pay dividends.</p>
<div class="ce4-share" style="margin:10px 0"><script type="text/javascript"><!--
		document.write('<div class="g-plusone" href="http://www.net-func.com/how-to/create_the-dictionary-object-please-consider/" data-size="medium" data-count="true"></div>');
		-->
		</script><a class="twitter-share-button" data-count="horizontal" data-text="Create_the Dictionary Object&#8230;. Please Consider" data-url="http://www.net-func.com/how-to/create_the-dictionary-object-please-consider/" data-via="Internet World" href="http://twitter.com/share"></a><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.net-func.com/how-to/create_the-dictionary-object-please-consider/&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;send=false&amp;colorscheme=light" style="border:none; overflow:hidden; width:90px; height:21px;"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://www.net-func.com/how-to/create_the-dictionary-object-please-consider/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

