<?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>haslo.ch - Guido's Blog &#187; snippets</title>
	<atom:link href="http://www.haslo.ch/blog/tag/snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.haslo.ch/blog</link>
	<description>We believe that people with passion can change the world for the better.</description>
	<lastBuildDate>Sun, 07 Mar 2010 17:58:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SetProperty and GetProperty with C# Reflection</title>
		<link>http://www.haslo.ch/blog/setproperty-and-getproperty-with-c-reflection/</link>
		<comments>http://www.haslo.ch/blog/setproperty-and-getproperty-with-c-reflection/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 12:32:29 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=2863</guid>
		<description><![CDATA[I&#8217;ll make a post soon, comparing Ruby on Rails to C# with ASP.NET MVC, but this here is atomic and might be of help for one or two readers.
If you want to set and get properties in C# in a type-agnostic way, these are the functions you want to put into a library class somewhere [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll make a post soon, comparing Ruby on Rails to C# with ASP.NET MVC, but this here is atomic and might be of help for one or two readers.</p>
<p>If you want to set and get properties in C# in a type-agnostic way, these are the functions you want to put into a library class somewhere (or, as I did, into the class that uses them):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #FF0000;">object</span> getProperty<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> containingObject, <span style="color: #FF0000;">string</span> propertyName<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> containingObject.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">InvokeMember</span><span style="color: #000000;">&#40;</span>propertyName, BindingFlags.<span style="color: #0000FF;">GetProperty</span>, <span style="color: #0600FF;">null</span>, containingObject, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> setProperty<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> containingObject, <span style="color: #FF0000;">string</span> propertyName, <span style="color: #FF0000;">object</span> newValue<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    containingObject.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">InvokeMember</span><span style="color: #000000;">&#40;</span>propertyName, BindingFlags.<span style="color: #0000FF;">SetProperty</span>, <span style="color: #0600FF;">null</span>, containingObject, <span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> newValue <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>That&#8217;s all I will say on this subject.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/setproperty-and-getproperty-with-c-reflection/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Being No One, First Main Argument</title>
		<link>http://www.haslo.ch/blog/being-no-one-first-main-argument/</link>
		<comments>http://www.haslo.ch/blog/being-no-one-first-main-argument/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 14:52:48 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Philosophy]]></category>
		<category><![CDATA[awesome]]></category>
		<category><![CDATA[haslo]]></category>
		<category><![CDATA[phenomenology]]></category>
		<category><![CDATA[Science]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[studies]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=2213</guid>
		<description><![CDATA[As you may know, I&#8217;m busy reading for my diploma thesis. If I manage to pull myself to it, that is, which is often not until noon, but regularly lasts to midnight and beyond. I happen to like reading at the office then, as that&#8217;s a way for me to switch context &#8211; &#8220;now I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>As you may know, I&#8217;m busy reading for my diploma thesis. If I manage to pull myself to it, that is, which is often not until noon, but regularly lasts to midnight and beyond. I happen to like reading at the office then, as that&#8217;s a way for me to switch context &#8211; &#8220;now I&#8217;m at home, doing whatever people do at home&#8221; and &#8220;now I&#8217;m at the office, learning and reading&#8221;. Fairly often, it works.</p>
<p>Anyway, enough &#8220;dear journal&#8221;, I really have to quote these few sentences from Thomas Metzinger&#8217;s book, <a href="http://www.amazon.com/Being-No-One-Self-Model-Subjectivity/dp/0262134179">Being No One</a>. While they do build on a lot of what he has written up to that point (starting with definitions of &#8220;transparency&#8221;, said constraints, &#8220;representation&#8221; and &#8220;phenomenal experience&#8221;), they&#8217;re utterly brilliant on their own as well, I think, and a big stepping stone for tackling the problem known as the <a href="http://en.wikipedia.org/wiki/Hard_problem_of_consciousness">hard problem of consciousness</a>.</p>
<blockquote><p>[...] If all other necessary and sufficient constraints for the emergence of phenomenal experience are satisfied by a given representational system, the addition of a transparent self-model will by necessity lead to the emergence of a phenomenal self. Phenomenal selfhood results from autoepistemic closure in a self-representing system; it is a lack of information. The prereflexive, preattentive experience of <em>being someone</em> results directly from the contents of the currently active self-model being transparent. [...] Under a general principle of ontological parsimony it is not necessary (or rational) to assume the existence of selves, because as theoretical entities they fulfill no indispensable explanatory function. What exists are information-processing systems engaged in the transparent process of phenomenal self-modelling. All that can be explained by the phenomenological notion of a &#8220;self&#8221; can also be explained using the representationalist notion of a transparent self-<em>model</em>.</p></blockquote>
<p>Really, just brilliant.</p>
<p>(I do hope that this falls under fair use in the context of scientific research, if the quotation is too expansive please notify me and I will remove it, or narrow it down further, immediately.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/being-no-one-first-main-argument/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stack Overflow: Solutions for Coders</title>
		<link>http://www.haslo.ch/blog/stack-overflow-solutions-for-coders/</link>
		<comments>http://www.haslo.ch/blog/stack-overflow-solutions-for-coders/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 12:56:35 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[awesome]]></category>
		<category><![CDATA[bubble 2.0]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[news]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=1225</guid>
		<description><![CDATA[If you&#8217;re a programmer, you know the problem of solution googlability. It&#8217;s particularly prominent in mainstream languages (and one of the things I decried in my What&#8217;s Wrong with VBA series, as VBA is particularly bad in that respect); you have a problem, you google for it, unless you&#8217;re coding in a good awesome well-designed [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_1252" class="wp-caption alignright" style="width: 160px"><a href="http://www.haslo.ch/blog/wp-content/uploads/2008/09/stack_overflow.jpg"><img class="size-thumbnail wp-image-1252" title="Stack Overflow in Firefox 3" src="http://www.haslo.ch/blog/wp-content/uploads/2008/09/stack_overflow-150x150.jpg" alt="Stack Overflow" width="150" height="150" /></a><p class="wp-caption-text">Stack Overflow</p></div>
<p>If you&#8217;re a programmer, you know the problem of solution googlability. It&#8217;s particularly prominent in mainstream languages (and one of the things <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/#community">I decried</a> in my <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">What&#8217;s Wrong with VBA series</a>, as VBA is particularly bad in that respect); you have a problem, you google for it, unless you&#8217;re coding in a <span style="text-decoration: line-through;">good</span> <span style="text-decoration: line-through;">awesome</span> <span style="text-decoration: line-through;">well-designed</span> freak language like Ruby, Python, or Haskell, all you get back is trash and spam.</p>
<p>So, since you&#8217;re reading blogs and a programmer (or you&#8217;d have stopped reading by now, I guess), you know <a href="http://www.joelonsoftware.com/">Joel on Software</a>. Now this guy and his team have done something: They took the Wiki approach of &#8220;everybody can edit&#8221;, added Digg&#8217;s &#8220;let&#8217;s vote stuff up&#8221;, and made the whole thing a generic programming Q&amp;A site.</p>
<p style="text-align: center;"><strong>Behold, and witness, the rise of</strong> <a href="http://stackoverflow.com/"><strong>Stack Overflow</strong></a>.</p>
<p>From <a href="http://www.joelonsoftware.com/items/2008/09/15.html">his launch post</a>:</p>
<blockquote><p>Here’s how it’s supposed to work. This is a community project, so I’m being careful to avoid saying this is how it will work… that’s up to the community. But this is roughly what I have in mind.</p>
<p>Every question in Stack Overflow is like the Wikipedia article for some extremely narrow, specific programming question. <em>How do I enlarge a fizzbar without overwriting the user’s snibbit?</em> [...]</p>
<p>Some people propose answers. Others vote on those answers. If you see the right answer, vote it up. If an answer is obviously wrong (or inferior in some way), you vote it down. Very quickly, the best answers bubble to the top. The person who asked the question in the first place also has the ability to designate one answer as the “accepted” answer, but this isn’t required. The accepted answer floats above all the other answers.</p></blockquote>
<p>Sounds great! It has tags, it&#8217;s not restricted to one programming language, it could provide the framework for thousands of programmers collaborating. And the best thing: They plan to &#8220;keep it ad-free and open to the public forever&#8221;.</p>
<p>You&#8217;ll need an <a href="http://en.wikipedia.org/wiki/Openid">OpenID</a> to log in &#8211; I got one <a href="https://www.myopenid.com/">at myOpenID</a> myself. Too bad it&#8217;s not possible to log in without, but whatever &#8211; I guess this approach has more promise than the closed one that Microsoft tried to force upon us with that whole <a href="http://en.wikipedia.org/wiki/Windows_Live_ID">Live ID</a> crap, but still shares the same weaknesses (which are, mainly, another party involved and a central point of failure).</p>
<p>Nevertheless, I do think this could be awesome. Let&#8217;s see where it goes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/stack-overflow-solutions-for-coders/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>VBA Vector Classes</title>
		<link>http://www.haslo.ch/blog/vba-vector-classes/</link>
		<comments>http://www.haslo.ch/blog/vba-vector-classes/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 15:36:25 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[downloads]]></category>
		<category><![CDATA[haslo]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[vb]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=416</guid>
		<description><![CDATA[If you&#8217;re anything like me and ever had to code anything in Visual Basic for Applications, you must have felt the need for a proper API with some very basic common ADTs. The most basic of them being, in my opinion, the dynamic array (Vector, ArrayList, List and Collection are often used more or rather [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re anything like me and ever had to code anything in <a href="http://en.wikipedia.org/wiki/Visual_Basic_for_Applications">Visual Basic for Applications</a>, you must have felt the need for a proper API with some very basic common <a href="http://en.wikipedia.org/wiki/Abstract_data_type">ADTs</a>. The most basic of them being, in my opinion, the <a href="http://en.wikipedia.org/wiki/Dynamic_array">dynamic array</a> (Vector, ArrayList, List and Collection are often used more or rather less synonymous). So what would it feel like to be able to write this and have it work?</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Dim</span> primitiveVector <span style="color: #000080;">As</span> clsVector
<span style="color: #000080;">Set</span> primitiveVector = <span style="color: #000080;">New</span> clsVector
<span style="color: #000080;">Dim</span> myInt <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
myInt = 3
&nbsp;
primitiveVector.AddItem myInt</pre></div></div>

<p>There are of course other people who have made such a thing, and there&#8217;s the VBA <a href="http://msdn.microsoft.com/en-us/library/a1y8b3b3(VS.80).aspx">Collection</a>, but none of them quite fit what I wanted. I wanted a Vector that had random access, was able to handle <a href="http://en.wikipedia.org/wiki/LIFO">LIFO</a> and <a href="http://en.wikipedia.org/wiki/FIFO">FIFO</a> (and thus act as stack and queue), addition and insertion and deletion, could check for item existence, and had dynamic resizing. So I went and coded my own. I did this while working at <a href="http://promino.ch/">Promino AG</a> and thus the code is technically theirs, so thanks to <a href="https://www.xing.com/profile/PaulB_Moser/">Paul Moser</a> for the nod for publishing this under the GNU <a href="http://www.gnu.org/licenses/lgpl.html">Lesser General Public License</a>.</p>
<p><a href="http://www.haslo.ch/media/Vectors.zip"><img src="/media/floppy.png" style="border: none; float: left" title="VBA Vector Classes" alt="floppy VBA Vector Classes" /></a>You can download both classes (two? more below), together with a tiny trivial testing framework, <a href="http://www.haslo.ch/media/Vectors.zip">as Access 2002-2003 .mdb</a>.<br />
<span id="more-416"></span><br />
This here is the vector class for Variants. It goes into a new class module, I called mine &#8220;clsVector&#8221;:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Option</span> Compare Database
<span style="color: #000080;">Option</span> <span style="color: #000080;">Explicit</span>
&nbsp;
<span style="color: #008000;">' Copyright 2007, 2008 by Promino AG
</span><span style="color: #008000;">' Author: Guido Gloor
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' This program is free software: you can redistribute it and/or modify
</span><span style="color: #008000;">' it under the terms of the GNU Lesser General Public License as published by
</span><span style="color: #008000;">' the Free Software Foundation, either version 3 of the License, or
</span><span style="color: #008000;">' (at your option) any later version.
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' This program is distributed in the hope that it will be useful,
</span><span style="color: #008000;">' but WITHOUT ANY WARRANTY; without even the implied warranty of
</span><span style="color: #008000;">' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
</span><span style="color: #008000;">' GNU Lesser General Public License for more details.
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' You should have received a copy of the GNU Lesser General Public License
</span><span style="color: #008000;">' along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
</span>
<span style="color: #000080;">Private</span> itemCount <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
<span style="color: #000080;">Private</span> itemBufferSize <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
<span style="color: #000080;">Private</span> itemBuffer() <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> Class_Initialize()
    itemCount = 0
    itemBufferSize = 100
    NewDimensions
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> NewDimensions()
    <span style="color: #000080;">ReDim</span> <span style="color: #000080;">Preserve</span> itemBuffer(itemBufferSize) <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> CheckDimensions()
    <span style="color: #000080;">If</span> itemCount &gt; itemBufferSize - 2 <span style="color: #000080;">Then</span>
        itemBufferSize = itemBufferSize * 2
        NewDimensions
    <span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> ShrinkDimensions()
    itemCount = itemBufferSize + 10
    NewDimensions
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetSize() <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
    GetSize = itemCount
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> AddItem(item <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>)
    CheckDimensions
    itemBuffer(itemCount) = item
    itemCount = itemCount + 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
    GetItem = itemBuffer(index)
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> PopItem() <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
    PopItem = itemBuffer(itemCount - 1)
    DeleteItem itemCount - 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetNextQueueItem() <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
    GetNextQueueItem = itemBuffer(0)
    DeleteItem 0
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> ContainsItem(item <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span>
    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
    ContainsItem = <span style="color: #000080;">False</span>
&nbsp;
    <span style="color: #000080;">For</span> i = 0 <span style="color: #000080;">To</span> itemCount - 1
        <span style="color: #000080;">If</span> itemBuffer(i) = item <span style="color: #000080;">Then</span> ContainsItem = <span style="color: #000080;">True</span>
    <span style="color: #000080;">Next</span> i
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> InsertItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>, item <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>)
    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
&nbsp;
    CheckDimensions
    <span style="color: #000080;">For</span> i = itemCount - 1 <span style="color: #000080;">To</span> index <span style="color: #000080;">Step</span> -1
        itemBuffer(i + 1) = itemBuffer(i)
    <span style="color: #000080;">Next</span> i
    itemBuffer(index) = item
    itemCount = itemCount + 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> DeleteItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>)
    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
&nbsp;
    <span style="color: #000080;">For</span> i = index + 1 <span style="color: #000080;">To</span> itemCount - 1
        itemBuffer(i - 1) = itemBuffer(i)
    <span style="color: #000080;">Next</span> i
    itemCount = itemCount - 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> IsEmpty() <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span>
    IsEmpty = itemCount &lt;= 0
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span></pre></td></tr></table></div>

<p>Now, it would be nice if we would be done. But, we&#8217;re not done yet, obviously, after all there&#8217;s plenty more code below this break. The reason for that is simple: In VBA, a Variant is not an Object, and an Object is not a Variant, consequently functions that take Objects don&#8217;t take Variants and vice versa (although you can technically store an Object in a Variant, this <em>will</em> lead to problems). Objects also happen to have &#8220;Set&#8221; mandatory in places where it isn&#8217;t allowed for Variants.</p>
<p>What this means is that there is no way to have the same Vector class for both your custom ADTs and the primitive data types. So basically, we have another class that has exactly the same functions and exactly the same algorithms, but takes Objects instead of Variants. I called mine &#8220;clsObjectVector&#8221;. Here is the source for the class module:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Option</span> Compare Database
<span style="color: #000080;">Option</span> <span style="color: #000080;">Explicit</span>
&nbsp;
<span style="color: #008000;">' Copyright 2007, 2008 by Promino AG
</span><span style="color: #008000;">' Author: Guido Gloor
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' This program is free software: you can redistribute it and/or modify
</span><span style="color: #008000;">' it under the terms of the GNU Lesser General Public License as published by
</span><span style="color: #008000;">' the Free Software Foundation, either version 3 of the License, or
</span><span style="color: #008000;">' (at your option) any later version.
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' This program is distributed in the hope that it will be useful,
</span><span style="color: #008000;">' but WITHOUT ANY WARRANTY; without even the implied warranty of
</span><span style="color: #008000;">' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
</span><span style="color: #008000;">' GNU Lesser General Public License for more details.
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' You should have received a copy of the GNU Lesser General Public License
</span><span style="color: #008000;">' along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
</span>
<span style="color: #000080;">Private</span> itemCount <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
<span style="color: #000080;">Private</span> itemBufferSize <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
<span style="color: #000080;">Private</span> itemBuffer() <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> Class_Initialize()
    itemCount = 0
    itemBufferSize = 100
    NewDimensions
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> NewDimensions()
    <span style="color: #000080;">ReDim</span> <span style="color: #000080;">Preserve</span> itemBuffer(itemBufferSize) <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> CheckDimensions()
    <span style="color: #000080;">If</span> itemCount &gt; itemBufferSize - 2 <span style="color: #000080;">Then</span>
        itemBufferSize = itemBufferSize * 2
        NewDimensions
    <span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> ShrinkDimensions()
    itemCount = itemBufferSize + 10
    NewDimensions
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetSize() <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
    GetSize = itemCount
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> AddItem(item <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>)
    CheckDimensions
    <span style="color: #000080;">Set</span> itemBuffer(itemCount) = item
    itemCount = itemCount + 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>
    <span style="color: #000080;">Set</span> GetItem = itemBuffer(index)
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> PopItem() <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>
    <span style="color: #000080;">Set</span> PopItem = itemBuffer(itemCount - 1)
    DeleteItem itemCount - 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetNextQueueItem() <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>
    <span style="color: #000080;">Set</span> GetNextQueueItem = itemBuffer(0)
    DeleteItem 0
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> ContainsItem(item <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span>
    <span style="color: #008000;">' The used ADTs need to have an Equals function for this to work
</span>    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
    ContainsItem = <span style="color: #000080;">False</span>
&nbsp;
    <span style="color: #000080;">For</span> i = 0 <span style="color: #000080;">To</span> itemCount - 1
        <span style="color: #000080;">If</span> itemBuffer(i).Equals(item) <span style="color: #000080;">Then</span> ContainsItem = <span style="color: #000080;">True</span>
    <span style="color: #000080;">Next</span> i
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> InsertItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>, item <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>)
    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
&nbsp;
    CheckDimensions
    <span style="color: #000080;">For</span> i = itemCount - 1 <span style="color: #000080;">To</span> index <span style="color: #000080;">Step</span> -1
        <span style="color: #000080;">Set</span> itemBuffer(i + 1) = itemBuffer(i)
    <span style="color: #000080;">Next</span> i
    <span style="color: #000080;">Set</span> itemBuffer(index) = item
    itemCount = itemCount + 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> DeleteItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>)
    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
&nbsp;
    <span style="color: #000080;">For</span> i = index + 1 <span style="color: #000080;">To</span> itemCount - 1
        <span style="color: #000080;">Set</span> itemBuffer(i - 1) = itemBuffer(i)
    <span style="color: #000080;">Next</span> i
    itemCount = itemCount - 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> IsEmpty() <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span>
    IsEmpty = itemCount &lt;= 0
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span></pre></td></tr></table></div>

<p>This is it, have fun <img src='http://www.haslo.ch/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' title="VBA Vector Classes" />  As much as you can have if you&#8217;re forced to work with VBA, that is.</p>
<p>Oh, and if you can use it &#8211; comments are appreciated <img src='http://www.haslo.ch/blog/wp-includes/images/smilies/icon_razz.gif' alt=':-P' class='wp-smiley' title="VBA Vector Classes" /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/vba-vector-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
