<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Collection Add and Functions</title>
	<atom:link href="http://www.dailydoseofexcel.com/archives/2007/08/22/collection-add-and-functions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dailydoseofexcel.com/archives/2007/08/22/collection-add-and-functions/</link>
	<description>Daily posts of Excel tips…and other stuff</description>
	<lastBuildDate>Thu, 09 Feb 2012 23:42:03 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Andy Pope</title>
		<link>http://www.dailydoseofexcel.com/archives/2007/08/22/collection-add-and-functions/#comment-27123</link>
		<dc:creator>Andy Pope</dc:creator>
		<pubDate>Thu, 06 Sep 2007 17:14:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1719#comment-27123</guid>
		<description>&lt;p&gt;Ads,&lt;/p&gt;
&lt;p&gt;You need to export the Group class and edit the bas file, as KeepItCool mentions&lt;br&gt;
Quote:&quot;Group has Enumeration to enable for/each.&lt;br&gt;
(needs editing on exported module to set the attributes)&quot;&lt;/p&gt;
&lt;p&gt;See Chip&#039;s page on the subject.&lt;br&gt;
&lt;a href=&quot;http://www.cpearson.com/excel/DefaultProperty.htm&quot; rel=&quot;nofollow&quot;&gt;http://www.cpearson.com/excel/DefaultProperty.htm&lt;/a&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Ads,</p>
<p>You need to export the Group class and edit the bas file, as KeepItCool mentions<br />
Quote:&#8221;Group has Enumeration to enable for/each.<br />
(needs editing on exported module to set the attributes)&#8221;</p>
<p>See Chip&#8217;s page on the subject.<br />
<a href="http://www.cpearson.com/excel/DefaultProperty.htm" rel="nofollow">http://www.cpearson.com/excel/DefaultProperty.htm</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam</title>
		<link>http://www.dailydoseofexcel.com/archives/2007/08/22/collection-add-and-functions/#comment-27093</link>
		<dc:creator>Adam</dc:creator>
		<pubDate>Thu, 06 Sep 2007 00:44:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1719#comment-27093</guid>
		<description>&lt;p&gt;Hi there,&lt;br&gt;
I tried the above slice of cade in excel and it stops at the following line with a runtime error &#039;438&#039; not being able to supportthe Contect property.&lt;/p&gt;
&lt;p&gt;&quot;For Each Contact In Group&quot;&lt;/p&gt;
&lt;p&gt;Any idea why this is stopping here?&lt;/p&gt;
&lt;p&gt;Cheers&lt;/p&gt;
&lt;p&gt;Ads&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hi there,<br />
I tried the above slice of cade in excel and it stops at the following line with a runtime error &#8217;438&#8242; not being able to supportthe Contect property.</p>
<p>&#8220;For Each Contact In Group&#8221;</p>
<p>Any idea why this is stopping here?</p>
<p>Cheers</p>
<p>Ads</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Woodhouse</title>
		<link>http://www.dailydoseofexcel.com/archives/2007/08/22/collection-add-and-functions/#comment-26720</link>
		<dc:creator>Mike Woodhouse</dc:creator>
		<pubDate>Thu, 23 Aug 2007 12:01:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1719#comment-26720</guid>
		<description>&lt;p&gt;I&#039;d say the create-in-a-function idea is an instance of a &quot;Factory Method&quot;. Or the VBA equivalent. It&#039;s about the best we can do in a COM-influenced world where we don&#039;t get parameterised constructors.&lt;/p&gt;
&lt;p&gt;I do like - and often use - the idea from keepItCool: building a custom collection class, even though it needs a little external hacking to get enumeration working (I&#039;m sure someone like Rob Bovey could extend VBE to export, hack and reimport the class...)&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I&#8217;d say the create-in-a-function idea is an instance of a &#8220;Factory Method&#8221;. Or the VBA equivalent. It&#8217;s about the best we can do in a COM-influenced world where we don&#8217;t get parameterised constructors.</p>
<p>I do like &#8211; and often use &#8211; the idea from keepItCool: building a custom collection class, even though it needs a little external hacking to get enumeration working (I&#8217;m sure someone like Rob Bovey could extend VBE to export, hack and reimport the class&#8230;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: keepITcool</title>
		<link>http://www.dailydoseofexcel.com/archives/2007/08/22/collection-add-and-functions/#comment-26717</link>
		<dc:creator>keepITcool</dc:creator>
		<pubDate>Thu, 23 Aug 2007 10:24:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1719#comment-26717</guid>
		<description>&lt;p&gt;A variation:&lt;/p&gt;
&lt;p&gt;Contact is the simplest class, defacto a user defined type.&lt;br&gt;
Group does the handling of creating the contact objects.&lt;br&gt;
Group has Enumeration to enable for/each.&lt;br&gt;
(needs editing on exported module to set the attributes)&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;co1&quot;&gt;&#039;Module1 &lt;br&gt;
&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;Option&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Explicit&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt; Test()&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; Groups &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;&lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Collection&lt;/span&gt;&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; Group &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; CGroup&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; Contact &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; CContact&lt;br&gt;
&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; Groups = GetGroups&lt;br&gt;
&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;For&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Each&lt;/span&gt; Group &lt;span class=&quot;kw1&quot;&gt;In&lt;/span&gt; Groups&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;For&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Each&lt;/span&gt; Contact &lt;span class=&quot;kw1&quot;&gt;In&lt;/span&gt; Group&lt;br&gt;
&#160; &#160; &#160; &lt;span class=&quot;kw1&quot;&gt;If&lt;/span&gt; Contact.State = &lt;span class=&quot;st0&quot;&gt;&quot;NE&quot;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Then&lt;/span&gt;&lt;br&gt;
&#160; &#160; &#160; &#160; &lt;span class=&quot;kw1&quot;&gt;With&lt;/span&gt; Contact&lt;br&gt;
&#160; &#160; &#160; &#160; &#160; Debug.&lt;span class=&quot;kw1&quot;&gt;Print&lt;/span&gt; Group.Name, .State, .Name, .Company&lt;br&gt;
&#160; &#160; &#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;With&lt;/span&gt;&lt;br&gt;
&#160; &#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;If&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Next&lt;/span&gt;&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;Next&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt; GetGroups() &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Collection&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; Group &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; CGroup&lt;br&gt;
&#160; &#160; &lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; GetGroups = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Collection&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; Group = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; CGroup&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;With&lt;/span&gt; Group&lt;br&gt;
&#160; &#160; &#160; .Name = &lt;span class=&quot;st0&quot;&gt;&quot;Group1&quot;&lt;/span&gt;&lt;br&gt;
&#160; &#160; &#160; .AddContact &lt;span class=&quot;st0&quot;&gt;&quot;Dick Kusleika&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;Company1&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;NE&quot;&lt;/span&gt;&lt;br&gt;
&#160; &#160; &#160; .AddContact &lt;span class=&quot;st0&quot;&gt;&quot;John Doe&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;Company2&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;NE&quot;&lt;/span&gt;&lt;br&gt;
&#160; &#160; &#160; .AddContact &lt;span class=&quot;st0&quot;&gt;&quot;Jane Doe&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;Company2&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;MA&quot;&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;With&lt;/span&gt;&lt;br&gt;
&#160; &#160; GetGroups.Add Group, Group.Name&lt;br&gt;
&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; Group = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; CGroup&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;With&lt;/span&gt; Group&lt;br&gt;
&#160; &#160; .Name = &lt;span class=&quot;st0&quot;&gt;&quot;Group2&quot;&lt;/span&gt;&lt;br&gt;
&#160; &#160; .AddContact &lt;span class=&quot;st0&quot;&gt;&quot;Ed Poe&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;Company3&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;AZ&quot;&lt;/span&gt;&lt;br&gt;
&#160; &#160; .AddContact &lt;span class=&quot;st0&quot;&gt;&quot;Bill Shakes&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;Company2&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;OR&quot;&lt;/span&gt;&lt;br&gt;
&#160; &#160; .AddContact &lt;span class=&quot;st0&quot;&gt;&quot;Emily Bronte&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;Company4&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;FL&quot;&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;With&lt;/span&gt;&lt;br&gt;
&#160; &#160; GetGroups.Add Group, Group.Name&lt;br&gt;
&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; Group = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; CGroup&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;With&lt;/span&gt; Group&lt;br&gt;
&#160; &#160; .Name = &lt;span class=&quot;st0&quot;&gt;&quot;Group3&quot;&lt;/span&gt;&lt;br&gt;
&#160; &#160; .AddContact &lt;span class=&quot;st0&quot;&gt;&quot;Henry Ford&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;Company5&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;MI&quot;&lt;/span&gt;&lt;br&gt;
&#160; &#160; .AddContact &lt;span class=&quot;st0&quot;&gt;&quot;Andy Carnegie&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;Company5&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;NE&quot;&lt;/span&gt;&lt;br&gt;
&#160; &#160; .AddContact &lt;span class=&quot;st0&quot;&gt;&quot;Johnny Rocky&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;Company4&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;NY&quot;&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;With&lt;/span&gt;&lt;br&gt;
&#160; &#160; GetGroups.Add Group, Group.Name&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;co1&quot;&gt;&#039;PublicNotCreatable Class CContact&lt;br&gt;
&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;Public&lt;/span&gt; Name &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Public&lt;/span&gt; Company &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Public&lt;/span&gt; State &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;co1&quot;&gt;&#039;PublicNotCreatable Class CGroup&lt;br&gt;
&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;Option&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Explicit&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; myContacts &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;&lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Collection&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Public&lt;/span&gt; Name &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt; AddContact(Name &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;, Company &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;, State &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;)&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; Contact &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; CContact&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; Contact = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; CContact&lt;br&gt;
&#160; Contact.Name = Name&lt;br&gt;
&#160; Contact.Company = Company&lt;br&gt;
&#160; Contact.State = State&lt;br&gt;
&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;If&lt;/span&gt; Len(Name) = 0 &lt;span class=&quot;kw1&quot;&gt;Then&lt;/span&gt; &lt;br&gt;
&#160; &#160; &#160;Err.Raise -1, , &lt;span class=&quot;st0&quot;&gt;&quot;Contact Name must be supplied&quot;&lt;/span&gt;&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;Else&lt;/span&gt;&lt;br&gt;
&#160; &#160; &#160;myContacts.Add Contact, Contact.Name&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;If&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt; Contact(Index &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Variant&lt;/span&gt;) &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; CContact&lt;br&gt;
&lt;span class=&quot;co1&quot;&gt;&#039;Attribute Contact.VB_UserMemId = 0&lt;br&gt;
&lt;/span&gt; &#160;&lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; Contact = myContacts.Item(Index)&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt; NewEnum() &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; IUnknown&lt;br&gt;
&lt;span class=&quot;co1&quot;&gt;&#039;Attribute NewEnum.VB_UserMemId = -4&lt;br&gt;
&lt;/span&gt;&lt;span class=&quot;co1&quot;&gt;&#039;Attribute NewEnum.VB_MemberFlags = &quot;40&quot;&lt;br&gt;
&lt;/span&gt; &#160;&lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; NewEnum = myContacts.[_NewEnum]&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Private&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt; Class_Initialize()&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; myContacts = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Collection&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
</description>
		<content:encoded><![CDATA[<p>A variation:</p>
<p>Contact is the simplest class, defacto a user defined type.<br />
Group does the handling of creating the contact objects.<br />
Group has Enumeration to enable for/each.<br />
(needs editing on exported module to set the attributes)</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="co1">&#8216;Module1 <br />
</span><span class="kw1">Option</span> <span class="kw1">Explicit</span></p>
<p><span class="kw1">Sub</span> Test()<br />
&nbsp; <span class="kw1">Dim</span> Groups &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">As</span> <span class="kw1">Collection</span><br />
&nbsp; <span class="kw1">Dim</span> Group &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">As</span> CGroup<br />
&nbsp; <span class="kw1">Dim</span> Contact &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">As</span> CContact</p>
<p>&nbsp; <span class="kw1">Set</span> Groups = GetGroups</p>
<p>&nbsp; <span class="kw1">For</span> <span class="kw1">Each</span> Group <span class="kw1">In</span> Groups<br />
&nbsp; &nbsp; <span class="kw1">For</span> <span class="kw1">Each</span> Contact <span class="kw1">In</span> Group<br />
&nbsp; &nbsp; &nbsp; <span class="kw1">If</span> Contact.State = <span class="st0">&#8220;NE&#8221;</span> <span class="kw1">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">With</span> Contact<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.<span class="kw1">Print</span> Group.Name, .State, .Name, .Company<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">With</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">If</span><br />
&nbsp; &nbsp; <span class="kw1">Next</span><br />
&nbsp; <span class="kw1">Next</span><br />
<span class="kw1">End</span> <span class="kw1">Sub</span></p>
<p><span class="kw1">Function</span> GetGroups() <span class="kw1">As</span> <span class="kw1">Collection</span><br />
&nbsp; &nbsp; <span class="kw1">Dim</span> Group <span class="kw1">As</span> CGroup<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">Set</span> GetGroups = <span class="kw1">New</span> <span class="kw1">Collection</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">Set</span> Group = <span class="kw1">New</span> CGroup<br />
&nbsp; &nbsp; <span class="kw1">With</span> Group<br />
&nbsp; &nbsp; &nbsp; .Name = <span class="st0">&#8220;Group1&#8243;</span><br />
&nbsp; &nbsp; &nbsp; .AddContact <span class="st0">&#8220;Dick Kusleika&#8221;</span>, <span class="st0">&#8220;Company1&#8243;</span>, <span class="st0">&#8220;NE&#8221;</span><br />
&nbsp; &nbsp; &nbsp; .AddContact <span class="st0">&#8220;John Doe&#8221;</span>, <span class="st0">&#8220;Company2&#8243;</span>, <span class="st0">&#8220;NE&#8221;</span><br />
&nbsp; &nbsp; &nbsp; .AddContact <span class="st0">&#8220;Jane Doe&#8221;</span>, <span class="st0">&#8220;Company2&#8243;</span>, <span class="st0">&#8220;MA&#8221;</span><br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">With</span><br />
&nbsp; &nbsp; GetGroups.Add Group, Group.Name</p>
<p>&nbsp; &nbsp; <span class="kw1">Set</span> Group = <span class="kw1">New</span> CGroup<br />
&nbsp; &nbsp; <span class="kw1">With</span> Group<br />
&nbsp; &nbsp; .Name = <span class="st0">&#8220;Group2&#8243;</span><br />
&nbsp; &nbsp; .AddContact <span class="st0">&#8220;Ed Poe&#8221;</span>, <span class="st0">&#8220;Company3&#8243;</span>, <span class="st0">&#8220;AZ&#8221;</span><br />
&nbsp; &nbsp; .AddContact <span class="st0">&#8220;Bill Shakes&#8221;</span>, <span class="st0">&#8220;Company2&#8243;</span>, <span class="st0">&#8220;OR&#8221;</span><br />
&nbsp; &nbsp; .AddContact <span class="st0">&#8220;Emily Bronte&#8221;</span>, <span class="st0">&#8220;Company4&#8243;</span>, <span class="st0">&#8220;FL&#8221;</span><br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">With</span><br />
&nbsp; &nbsp; GetGroups.Add Group, Group.Name</p>
<p>&nbsp; &nbsp; <span class="kw1">Set</span> Group = <span class="kw1">New</span> CGroup<br />
&nbsp; &nbsp; <span class="kw1">With</span> Group<br />
&nbsp; &nbsp; .Name = <span class="st0">&#8220;Group3&#8243;</span><br />
&nbsp; &nbsp; .AddContact <span class="st0">&#8220;Henry Ford&#8221;</span>, <span class="st0">&#8220;Company5&#8243;</span>, <span class="st0">&#8220;MI&#8221;</span><br />
&nbsp; &nbsp; .AddContact <span class="st0">&#8220;Andy Carnegie&#8221;</span>, <span class="st0">&#8220;Company5&#8243;</span>, <span class="st0">&#8220;NE&#8221;</span><br />
&nbsp; &nbsp; .AddContact <span class="st0">&#8220;Johnny Rocky&#8221;</span>, <span class="st0">&#8220;Company4&#8243;</span>, <span class="st0">&#8220;NY&#8221;</span><br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">With</span><br />
&nbsp; &nbsp; GetGroups.Add Group, Group.Name</p>
<p><span class="kw1">End</span> <span class="kw1">Function</span></p>
<p><span class="co1">&#8216;PublicNotCreatable Class CContact<br />
</span><span class="kw1">Public</span> Name <span class="kw1">As</span> <span class="kw1">String</span><br />
<span class="kw1">Public</span> Company <span class="kw1">As</span> <span class="kw1">String</span><br />
<span class="kw1">Public</span> State <span class="kw1">As</span> <span class="kw1">String</span></p>
<p><span class="co1">&#8216;PublicNotCreatable Class CGroup<br />
</span><span class="kw1">Option</span> <span class="kw1">Explicit</span></p>
<p><span class="kw1">Dim</span> myContacts &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">As</span> <span class="kw1">Collection</span><br />
<span class="kw1">Public</span> Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">As</span> <span class="kw1">String</span></p>
<p><span class="kw1">Sub</span> AddContact(Name <span class="kw1">As</span> <span class="kw1">String</span>, Company <span class="kw1">As</span> <span class="kw1">String</span>, State <span class="kw1">As</span> <span class="kw1">String</span>)<br />
&nbsp; <span class="kw1">Dim</span> Contact &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">As</span> CContact<br />
&nbsp; <span class="kw1">Set</span> Contact = <span class="kw1">New</span> CContact<br />
&nbsp; Contact.Name = Name<br />
&nbsp; Contact.Company = Company<br />
&nbsp; Contact.State = State</p>
<p>&nbsp; <span class="kw1">If</span> Len(Name) = 0 <span class="kw1">Then</span> <br />
&nbsp; &nbsp; &nbsp;Err.Raise -1, , <span class="st0">&#8220;Contact Name must be supplied&#8221;</span><br />
&nbsp; <span class="kw1">Else</span><br />
&nbsp; &nbsp; &nbsp;myContacts.Add Contact, Contact.Name<br />
&nbsp; <span class="kw1">End</span> <span class="kw1">If</span></p>
<p><span class="kw1">End</span> <span class="kw1">Sub</span></p>
<p><span class="kw1">Function</span> Contact(Index <span class="kw1">As</span> <span class="kw1">Variant</span>) <span class="kw1">As</span> CContact<br />
<span class="co1">&#8216;Attribute Contact.VB_UserMemId = 0<br />
</span> &nbsp;<span class="kw1">Set</span> Contact = myContacts.Item(Index)<br />
<span class="kw1">End</span> <span class="kw1">Function</span></p>
<p><span class="kw1">Function</span> NewEnum() <span class="kw1">As</span> IUnknown<br />
<span class="co1">&#8216;Attribute NewEnum.VB_UserMemId = -4<br />
</span><span class="co1">&#8216;Attribute NewEnum.VB_MemberFlags = &#8220;40&#8243;<br />
</span> &nbsp;<span class="kw1">Set</span> NewEnum = myContacts.[_NewEnum]<br />
<span class="kw1">End</span> <span class="kw1">Function</span></p>
<p><span class="kw1">Private</span> <span class="kw1">Sub</span> Class_Initialize()<br />
&nbsp; <span class="kw1">Set</span> myContacts = <span class="kw1">New</span> <span class="kw1">Collection</span><br />
<span class="kw1">End</span> <span class="kw1">Sub</span></div>
</div>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tushar Mehta</title>
		<link>http://www.dailydoseofexcel.com/archives/2007/08/22/collection-add-and-functions/#comment-26714</link>
		<dc:creator>Tushar Mehta</dc:creator>
		<pubDate>Thu, 23 Aug 2007 06:22:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1719#comment-26714</guid>
		<description>&lt;p&gt;Hi Dick:&lt;/p&gt;
&lt;p&gt;When creating a class consider putting as much of the functionality into the class rather than relying on the client doing things correctly.  For example, I would have the parent class add a single element object not an entire collection since one doesn&#039;t know what is in the collection.&lt;/p&gt;
&lt;p&gt;So, clsParent would contain:&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;Option&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Explicit&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; AllElements &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Collection&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt; addElement(Ele &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; clsElement)&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;On&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Resume&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Next&lt;/span&gt;&lt;br&gt;
&#160; &#160; AllElements.Add Ele, Ele.Key&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Private&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt; Class_Initialize()&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; AllElements = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Collection&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;For the sake of simplicity I assumed that clsElement has a Key.  But, PED shows how to generalize the idea.&lt;/p&gt;
&lt;p&gt;I would even put the responsibility of listing all the elements into the class with&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;Public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt; ListAll() &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; vEle &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Variant&lt;/span&gt;, Ele &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; clsElement&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;For&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Each&lt;/span&gt; vEle &lt;span class=&quot;kw1&quot;&gt;In&lt;/span&gt; AllElements&lt;br&gt;
&#160; &#160; &#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; Ele = vEle&lt;br&gt;
&#160; &#160; &#160; &#160; ListAll = ListAll &amp; Ele.Key &amp; &lt;span class=&quot;st0&quot;&gt;&quot;=&quot;&lt;/span&gt; &amp; Ele.Val &amp; vbNewLine&lt;br&gt;
&#160; &#160; &#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Next&lt;/span&gt; vEle&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now, if we have an element class named clsElement containing&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;Option&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Explicit&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; sKey &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;, sVal &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Property&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Let&lt;/span&gt; Key(uKey &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;): sKey = uKey: &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Property&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Property&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Get&lt;/span&gt; Key() &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;: Key = sKey: &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Property&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Property&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Let&lt;/span&gt; Val(uVal &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;): sVal = uVal: &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Property&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Property&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Get&lt;/span&gt; Val() &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;: Val = sVal: &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Property&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;we would use the above with code in a standard module as in&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;Option&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Explicit&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; aParent &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; clsParent&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt; addEle(&lt;span class=&quot;kw1&quot;&gt;ByVal&lt;/span&gt; Key &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;kw1&quot;&gt;ByVal&lt;/span&gt; Val &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;) &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; clsElement&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; Ele &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; clsElement&lt;br&gt;
&#160; &#160; Ele.Key = Key&lt;br&gt;
&#160; &#160; Ele.Val = Val&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; addEle = Ele&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt; demoClasses()&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; aParent = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; clsParent&lt;br&gt;
&#160; &#160; aParent.addElement addEle(&lt;span class=&quot;st0&quot;&gt;&quot;1&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;One&quot;&lt;/span&gt;)&lt;br&gt;
&#160; &#160; aParent.addElement addEle(&lt;span class=&quot;st0&quot;&gt;&quot;2&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;Two&quot;&lt;/span&gt;)&lt;br&gt;
&#160; &#160; Debug.&lt;span class=&quot;kw1&quot;&gt;Print&lt;/span&gt; aParent.ListAll&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; aParent = &lt;span class=&quot;kw1&quot;&gt;Nothing&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;One thing that VB doesn&#039;t support is overloading functions.  That is why we cannot use something like&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;dim&lt;/span&gt; anEle &lt;span class=&quot;kw1&quot;&gt;as&lt;/span&gt; clsElement&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;set&lt;/span&gt; anEle = &lt;span class=&quot;kw1&quot;&gt;new&lt;/span&gt; clsElement (&lt;span class=&quot;st0&quot;&gt;&quot;1&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;One&quot;&lt;/span&gt;)&lt;br&gt;
aParent.addelement anEle&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;or even better&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;aParent.addelement &lt;span class=&quot;kw1&quot;&gt;new&lt;/span&gt; clsElement (&lt;span class=&quot;st0&quot;&gt;&quot;1&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;One&quot;&lt;/span&gt;)&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;But, we can do something similar with the following:&lt;/p&gt;
&lt;p&gt;Add to clsElement:&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;Public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt; Init(&lt;span class=&quot;kw1&quot;&gt;ByVal&lt;/span&gt; Key &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;, Val &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;)&lt;br&gt;
&#160; &#160; Me.Key = Key: Me.Val = Val&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now, the standard module code could be shortened to&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;Option&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Explicit&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; aParent &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; clsParent&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt; addEle(&lt;span class=&quot;kw1&quot;&gt;ByVal&lt;/span&gt; Key &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;kw1&quot;&gt;ByVal&lt;/span&gt; Val &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;) &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; clsElement&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; addEle = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; clsElement: addEle.Init Key, Val&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt; demoClasses()&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; aParent = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; clsParent&lt;br&gt;
&#160; &#160; aParent.addElement addEle(&lt;span class=&quot;st0&quot;&gt;&quot;1&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;One&quot;&lt;/span&gt;)&lt;br&gt;
&#160; &#160; aParent.addElement addEle(&lt;span class=&quot;st0&quot;&gt;&quot;2&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;Two&quot;&lt;/span&gt;)&lt;br&gt;
&#160; &#160; Debug.&lt;span class=&quot;kw1&quot;&gt;Print&lt;/span&gt; aParent.ListAll&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; aParent = &lt;span class=&quot;kw1&quot;&gt;Nothing&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;We can take it a step further approaching&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;aParent.addelement &lt;span class=&quot;kw1&quot;&gt;new&lt;/span&gt; clsElement (&lt;span class=&quot;st0&quot;&gt;&quot;1&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;One&quot;&lt;/span&gt;)&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;with the following:&lt;/p&gt;
&lt;p&gt;In the clsElement class replace the Init sub with&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;Public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt; Init(&lt;span class=&quot;kw1&quot;&gt;ByVal&lt;/span&gt; Key &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;, Val &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;) &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; clsElement&lt;br&gt;
&#160; &#160; Me.Key = Key: Me.Val = Val&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; Init = Me&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now with a function that creates an empty object, the code in the standard module becomes&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;Option&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Explicit&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; aParent &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; clsParent&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt; newClsElement() &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; clsElement&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; newClsElement = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; clsElement&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt; demoClasses()&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; aParent = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; clsParent&lt;br&gt;
&#160; &#160; aParent.addElement newClsElement.Init(&lt;span class=&quot;st0&quot;&gt;&quot;1&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;One&quot;&lt;/span&gt;)&lt;br&gt;
&#160; &#160; aParent.addElement newClsElement.Init(&lt;span class=&quot;st0&quot;&gt;&quot;2&quot;&lt;/span&gt;, &lt;span class=&quot;st0&quot;&gt;&quot;Two&quot;&lt;/span&gt;)&lt;br&gt;
&#160; &#160; Debug.&lt;span class=&quot;kw1&quot;&gt;Print&lt;/span&gt; aParent.ListAll&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; aParent = &lt;span class=&quot;kw1&quot;&gt;Nothing&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;This, the use of a function to create an empty object, is how one can also use a class declared in another workbook/addin.  See&lt;br&gt;
How to use a class (object) from outside of the VBA project in which it is declared&lt;br&gt;
&lt;a href=&quot;http://support.microsoft.com/kb/555159&quot; rel=&quot;nofollow&quot;&gt;http://support.microsoft.com/kb/555159&lt;/a&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hi Dick:</p>
<p>When creating a class consider putting as much of the functionality into the class rather than relying on the client doing things correctly.  For example, I would have the parent class add a single element object not an entire collection since one doesn&#8217;t know what is in the collection.</p>
<p>So, clsParent would contain:</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="kw1">Option</span> <span class="kw1">Explicit</span></p>
<p><span class="kw1">Dim</span> AllElements <span class="kw1">As</span> <span class="kw1">Collection</span></p>
<p><span class="kw1">Public</span> <span class="kw1">Sub</span> addElement(Ele <span class="kw1">As</span> clsElement)<br />
&nbsp; &nbsp; <span class="kw1">On</span> <span class="kw1">Error</span> <span class="kw1">Resume</span> <span class="kw1">Next</span><br />
&nbsp; &nbsp; AllElements.Add Ele, Ele.Key<br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Sub</span></p>
<p><span class="kw1">Private</span> <span class="kw1">Sub</span> Class_Initialize()<br />
&nbsp; &nbsp; <span class="kw1">Set</span> AllElements = <span class="kw1">New</span> <span class="kw1">Collection</span><br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Sub</span></div>
</div>
<p>For the sake of simplicity I assumed that clsElement has a Key.  But, PED shows how to generalize the idea.</p>
<p>I would even put the responsibility of listing all the elements into the class with</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="kw1">Public</span> <span class="kw1">Function</span> ListAll() <span class="kw1">As</span> <span class="kw1">String</span><br />
&nbsp; &nbsp; <span class="kw1">Dim</span> vEle <span class="kw1">As</span> <span class="kw1">Variant</span>, Ele <span class="kw1">As</span> clsElement<br />
&nbsp; &nbsp; <span class="kw1">For</span> <span class="kw1">Each</span> vEle <span class="kw1">In</span> AllElements<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">Set</span> Ele = vEle<br />
&nbsp; &nbsp; &nbsp; &nbsp; ListAll = ListAll &amp; Ele.Key &amp; <span class="st0">&#8220;=&#8221;</span> &amp; Ele.Val &amp; vbNewLine<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">Next</span> vEle<br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Function</span></div>
</div>
<p>Now, if we have an element class named clsElement containing</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="kw1">Option</span> <span class="kw1">Explicit</span></p>
<p><span class="kw1">Dim</span> sKey <span class="kw1">As</span> <span class="kw1">String</span>, sVal <span class="kw1">As</span> <span class="kw1">String</span></p>
<p><span class="kw1">Property</span> <span class="kw1">Let</span> Key(uKey <span class="kw1">As</span> <span class="kw1">String</span>): sKey = uKey: <span class="kw1">End</span> <span class="kw1">Property</span><br />
<span class="kw1">Property</span> <span class="kw1">Get</span> Key() <span class="kw1">As</span> <span class="kw1">String</span>: Key = sKey: <span class="kw1">End</span> <span class="kw1">Property</span></p>
<p><span class="kw1">Property</span> <span class="kw1">Let</span> Val(uVal <span class="kw1">As</span> <span class="kw1">String</span>): sVal = uVal: <span class="kw1">End</span> <span class="kw1">Property</span><br />
<span class="kw1">Property</span> <span class="kw1">Get</span> Val() <span class="kw1">As</span> <span class="kw1">String</span>: Val = sVal: <span class="kw1">End</span> <span class="kw1">Property</span></div>
</div>
<p>we would use the above with code in a standard module as in</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="kw1">Option</span> <span class="kw1">Explicit</span></p>
<p><span class="kw1">Dim</span> aParent <span class="kw1">As</span> clsParent</p>
<p><span class="kw1">Function</span> addEle(<span class="kw1">ByVal</span> Key <span class="kw1">As</span> <span class="kw1">String</span>, <span class="kw1">ByVal</span> Val <span class="kw1">As</span> <span class="kw1">String</span>) <span class="kw1">As</span> clsElement<br />
&nbsp; &nbsp; <span class="kw1">Dim</span> Ele <span class="kw1">As</span> <span class="kw1">New</span> clsElement<br />
&nbsp; &nbsp; Ele.Key = Key<br />
&nbsp; &nbsp; Ele.Val = Val<br />
&nbsp; &nbsp; <span class="kw1">Set</span> addEle = Ele<br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Function</span><br />
<span class="kw1">Sub</span> demoClasses()<br />
&nbsp; &nbsp; <span class="kw1">Set</span> aParent = <span class="kw1">New</span> clsParent<br />
&nbsp; &nbsp; aParent.addElement addEle(<span class="st0">&#8220;1&#8243;</span>, <span class="st0">&#8220;One&#8221;</span>)<br />
&nbsp; &nbsp; aParent.addElement addEle(<span class="st0">&#8220;2&#8243;</span>, <span class="st0">&#8220;Two&#8221;</span>)<br />
&nbsp; &nbsp; Debug.<span class="kw1">Print</span> aParent.ListAll<br />
&nbsp; &nbsp; <span class="kw1">Set</span> aParent = <span class="kw1">Nothing</span><br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Sub</span></div>
</div>
<p>One thing that VB doesn&#8217;t support is overloading functions.  That is why we cannot use something like</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="kw1">dim</span> anEle <span class="kw1">as</span> clsElement<br />
<span class="kw1">set</span> anEle = <span class="kw1">new</span> clsElement (<span class="st0">&#8220;1&#8243;</span>, <span class="st0">&#8220;One&#8221;</span>)<br />
aParent.addelement anEle</div>
</div>
<p>or even better</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer">aParent.addelement <span class="kw1">new</span> clsElement (<span class="st0">&#8220;1&#8243;</span>, <span class="st0">&#8220;One&#8221;</span>)</div>
</div>
<p>But, we can do something similar with the following:</p>
<p>Add to clsElement:</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="kw1">Public</span> <span class="kw1">Sub</span> Init(<span class="kw1">ByVal</span> Key <span class="kw1">As</span> <span class="kw1">String</span>, Val <span class="kw1">As</span> <span class="kw1">String</span>)<br />
&nbsp; &nbsp; Me.Key = Key: Me.Val = Val<br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Sub</span></div>
</div>
<p>Now, the standard module code could be shortened to</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="kw1">Option</span> <span class="kw1">Explicit</span></p>
<p><span class="kw1">Dim</span> aParent <span class="kw1">As</span> clsParent</p>
<p><span class="kw1">Function</span> addEle(<span class="kw1">ByVal</span> Key <span class="kw1">As</span> <span class="kw1">String</span>, <span class="kw1">ByVal</span> Val <span class="kw1">As</span> <span class="kw1">String</span>) <span class="kw1">As</span> clsElement<br />
&nbsp; &nbsp; <span class="kw1">Set</span> addEle = <span class="kw1">New</span> clsElement: addEle.Init Key, Val<br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Function</span><br />
<span class="kw1">Sub</span> demoClasses()<br />
&nbsp; &nbsp; <span class="kw1">Set</span> aParent = <span class="kw1">New</span> clsParent<br />
&nbsp; &nbsp; aParent.addElement addEle(<span class="st0">&#8220;1&#8243;</span>, <span class="st0">&#8220;One&#8221;</span>)<br />
&nbsp; &nbsp; aParent.addElement addEle(<span class="st0">&#8220;2&#8243;</span>, <span class="st0">&#8220;Two&#8221;</span>)<br />
&nbsp; &nbsp; Debug.<span class="kw1">Print</span> aParent.ListAll<br />
&nbsp; &nbsp; <span class="kw1">Set</span> aParent = <span class="kw1">Nothing</span><br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Sub</span></div>
</div>
<p>We can take it a step further approaching</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer">aParent.addelement <span class="kw1">new</span> clsElement (<span class="st0">&#8220;1&#8243;</span>, <span class="st0">&#8220;One&#8221;</span>)</div>
</div>
<p>with the following:</p>
<p>In the clsElement class replace the Init sub with</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="kw1">Public</span> <span class="kw1">Function</span> Init(<span class="kw1">ByVal</span> Key <span class="kw1">As</span> <span class="kw1">String</span>, Val <span class="kw1">As</span> <span class="kw1">String</span>) <span class="kw1">As</span> clsElement<br />
&nbsp; &nbsp; Me.Key = Key: Me.Val = Val<br />
&nbsp; &nbsp; <span class="kw1">Set</span> Init = Me<br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Function</span></div>
</div>
<p>Now with a function that creates an empty object, the code in the standard module becomes</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="kw1">Option</span> <span class="kw1">Explicit</span></p>
<p><span class="kw1">Dim</span> aParent <span class="kw1">As</span> clsParent</p>
<p><span class="kw1">Function</span> newClsElement() <span class="kw1">As</span> clsElement<br />
&nbsp; &nbsp; <span class="kw1">Set</span> newClsElement = <span class="kw1">New</span> clsElement<br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Function</span><br />
<span class="kw1">Sub</span> demoClasses()<br />
&nbsp; &nbsp; <span class="kw1">Set</span> aParent = <span class="kw1">New</span> clsParent<br />
&nbsp; &nbsp; aParent.addElement newClsElement.Init(<span class="st0">&#8220;1&#8243;</span>, <span class="st0">&#8220;One&#8221;</span>)<br />
&nbsp; &nbsp; aParent.addElement newClsElement.Init(<span class="st0">&#8220;2&#8243;</span>, <span class="st0">&#8220;Two&#8221;</span>)<br />
&nbsp; &nbsp; Debug.<span class="kw1">Print</span> aParent.ListAll<br />
&nbsp; &nbsp; <span class="kw1">Set</span> aParent = <span class="kw1">Nothing</span><br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Sub</span></div>
</div>
<p>This, the use of a function to create an empty object, is how one can also use a class declared in another workbook/addin.  See<br />
How to use a class (object) from outside of the VBA project in which it is declared<br />
<a href="http://support.microsoft.com/kb/555159" rel="nofollow">http://support.microsoft.com/kb/555159</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>

