<?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>Cogniza &#187; COM</title>
	<atom:link href="http://cogniza.com/wordpress/tag/com/feed/" rel="self" type="application/rss+xml" />
	<link>http://cogniza.com/wordpress</link>
	<description>Business-Intelligence Specialists</description>
	<lastBuildDate>Mon, 15 Mar 2010 20:22:31 +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>Visual Basic: Create a Sorted Collection</title>
		<link>http://cogniza.com/wordpress/2007/01/30/visual-basic-create-a-sorted-collection/</link>
		<comments>http://cogniza.com/wordpress/2007/01/30/visual-basic-create-a-sorted-collection/#comments</comments>
		<pubDate>Tue, 30 Jan 2007 21:51:00 +0000</pubDate>
		<dc:creator>Craig Buchanan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Visual Basic]]></category>
		<category><![CDATA[Collections]]></category>
		<category><![CDATA[COM]]></category>

		<guid isPermaLink="false">http://www.cogniza.com/blog/?p=92</guid>
		<description><![CDATA[Create a sorted Visual Basic 6 collection that is similar to sorting an ArrayList in Visual Basic .Net.]]></description>
			<content:encoded><![CDATA[<p>Create a sorted Visual Basic 6 collection that is similar to sorting an ArrayList in Visual Basic .Net.</p>
<p><span id="more-92"></span></p>
<p>This example will create a sorted collection of Person objects; the class will be named PersonCollection.</p>
<p>The first step is to create two classes to be used as interfaces: IComparable and IComparer.  This classes resemble their Visual Basic.Net interface equivalents.</p>
<h2>IComparable Class</h2>
<pre>'compare the current object to the referenced object</pre>
<pre>Public Function CompareTo(obj As Object)
End Function</pre>
<p>The Person&#8217;s IComparable interface will be used by the PersonCollection&#8217;s Sort() method to provide the default sorting.</p>
<h2>IComparer Class</h2>
<pre>'compare to referenced objects</pre>
<pre>Public Function Compare(x As Object, y As Object)
End Function</pre>
<p>The PersonSorter&#8217;s IComparable interface will be used by the PersonCollection&#8217;s Sort() method to provide additional sorting options.</p>
<h2>Person Class</h2>
<p>The Person class has two relevant properties:</p>
<pre>'--------------------------------------------------------------------------------
'Public Properties
'--------------------------------------------------------------------------------
'FileAs will be used to provide a default-sorting order.
Public Property Get FileAs() As String
FileAs = Me.LastName &amp; ", " &amp; Me.FirstName
End Property

'The Key property is used to provide the class' unique key.
Public Property Get Key As String
Key = "#" &amp; Me.Id
End Property
'--------------------------------------------------------------------------------
'IComparable Methods
'--------------------------------------------------------------------------------
Private Function IComparable_CompareTo(obj As Object)

If Not TypeOf obj Is Person Then _
Err.Raise vbObjectError + 1, Err.Source, "The object is not a Person."

'convert the object to a Person
Dim Item As Person: Set Item = obj

'compare the FileAs field between the two instances.
If Me.FileAs &gt; Item.FileAs Then
IComparable_CompareTo = 1
ElseIf Me.FileAs &lt; Item.FileAs Then
IComparable_CompareTo = -1
Else
IComparable_CompareTo = 0
End If

End Function</pre>
<p>The Person class exposes a CompareTo function to complete the interface inheritance:</p>
<pre>'--------------------------------------------------------------------------------
'Public Methods
'--------------------------------------------------------------------------------
Public Function CompareTo(obj As Object)
CompareTo = IComparable_CompareTo(obj)
End Function</pre>
<h2>PersonSorter Class</h2>
<p>The PersonSorter class implements the IComparer interface to provide a more-flexible set of sorting option. It uses the CallByName() function in combination with the ProcedureName property to do the actual work of comparison.  In addition, the PersonSorter provides a means to change the direction of the sort using the Direction property.</p>
<pre>'--------------------------------------------------------------------------------
'Public Properties
'--------------------------------------------------------------------------------
Public Direction As DirectionEnum
Public ProcedureName As String</pre>
<pre>'--------------------------------------------------------------------------------
'Public Methods
'--------------------------------------------------------------------------------
Public Function Compare(x As Object, y As Object) As Integer
Compare = IComparer_Compare(x, y)
End Function

Public Function ToString() As String
ToString = IObject_ToString
End Function
'--------------------------------------------------------------------------------
'IComparer Methods
'--------------------------------------------------------------------------------
Private Function IComparer_Compare(x As Object, y As Object) As Integer

If CallByName(x, ProcedureName, VbGet) &gt; CallByName(y, ProcedureName, VbGet) Then
IComparer_Compare = 1 * Direction
ElseIf CallByName(x, ProcedureName, VbGet) &lt; CallByName(y, ProcedureName, VbGet) Then
IComparer_Compare = -1 * Direction
Else
IComparer_Compare = 0
End If

End Function</pre>
<p>The next challenge is to provide a means to swap two items in a collection.  This is done via the Swap method:</p>
<h2>Swap() Function</h2>
<pre>Private Sub Swap(ByRef Items As Collection, x As Long, y As Long)

'quality control
If x = y Then Exit Sub
If x &lt; 0 Or y &lt; 0 Then Exit Sub
If x &gt; Items.Count Or y &gt; Items.Count Then Exit Sub

'normalize positions
If x &gt; y Then
Dim t As Long
t = x
x = y
y = t
End If

'store items temporarily
Dim ItemX As Object: Set ItemX = Items(x)
Dim ItemY As Object: Set ItemY = Items(y)

'remove items
Items.Remove y
Items.Remove x

'For the swap to work, the Key property must be created.  While this could be implemented as an interface, I chose to simplify matters and simply add it to the Person class.
'add y to x's position
If x &gt; Items.Count Then
Items.Add ItemY, ItemY.Key
Else
Items.Add ItemY, ItemY.Key, x
End If

'add x to y's position
If y &gt; Items.Count Then
Items.Add ItemX, ItemX.Key
Else
Items.Add ItemX, ItemX.Key, y
End If

'finalize
Set ItemX = Nothing
Set ItemY = Nothing

End Sub</pre>
<p>Next, a mechanism is needed to sort the collection&#8211;the <a href="http://en.wikipedia.org/wiki/Bubblesort" target="_blank" title="BubbleSort">BubbleSort</a> algorithm was adapted for for this purpose.</p>
<h2>BubbleSort() Function</h2>
<pre>'Items - collection to be sorted 'Comparer - alternate sorting method Public Sub BubbleSort(ByRef Items As Collection, Optional Comparer As IComparer)

Dim i As Long, Sorted As Boolean

If Comparer Is Nothing Then

Do While Not Sorted
Sorted = True
For i = 1 To Items.Count - 1
If Items(i).CompareTo(Items(i + 1)) &gt; 0 Then
Swap Items, i, i + 1
Sorted = False
End If
Next
Loop

Else

Do While Not Sorted
Sorted = True
For i = 1 To Items.Count - 1
If Comparer.Compare(Items(i), Items(i + 1)) &gt; 0 Then
Swap Items, i, i + 1
Sorted = False
End If
Next
Loop

End If

End Sub</pre>
<h2>Usage</h2>
<p>For basic usage, add items to the PersonCollection, the call the Sort() method:</p>
<pre>Dim PC As New PersonCollection
With PC
.Add 1, "chris", "sellers", "6/13/1937"
.Add 5, "john", "gage", "10/10/1974"
.Add 3, "craig", "buchanan", "3/29/1966"
.Add 4, "jane", "buchanan", "12/26/1968"
.Add 2, "bill", "richards", "01/01/1900"
.Sort
End With

Enumerate PC</pre>
<p>For more control, add a PersonSorter class:</p>
<pre>Dim PS As New PersonSorter
With PS
.ProcedureName = "Age"
.Direction = DirectionEnum.Desc
End With

Dim PC As New PersonCollection
With PC
.Add 1, "chris", "sellers", "6/13/1937"
.Add 5, "john", "gage", "10/10/1974"
.Add 3, "craig", "buchanan", "3/29/1966"
.Add 4, "jane", "buchanan", "12/26/1968"
.Add 2, "bill", "richards", "01/01/1900"
.Sort PS
End With

Sub Enumerate(obj As Object)

Dim Item As Person
Debug.Print "--------------------------------------------------"
Dim o As IObject: Set o = obj.Comparer
Debug.Print o.tostring
Debug.Print "--------------------------------------------------"
For Each Item In obj
Debug.Print Item.Id &amp; "; " &amp; Item.FileAs &amp; "; " &amp; Item.Age
Next

End Sub</pre>
<h2>SortableCollection Project</h2>
<p><a href="/wordpress/wp-content/uploads/2006/12/sortablecollection.zip" id="p91">Sortable Collection Project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://cogniza.com/wordpress/2007/01/30/visual-basic-create-a-sorted-collection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Outlook: Export Contacts to vCards</title>
		<link>http://cogniza.com/wordpress/2006/11/09/outlook-export-contacts-to-vcards/</link>
		<comments>http://cogniza.com/wordpress/2006/11/09/outlook-export-contacts-to-vcards/#comments</comments>
		<pubDate>Thu, 09 Nov 2006 15:22:53 +0000</pubDate>
		<dc:creator>Craig Buchanan</dc:creator>
				<category><![CDATA[Outlook]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[Contacts]]></category>
		<category><![CDATA[vCard]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.cogniza.com/blog/?p=85</guid>
		<description><![CDATA[This script will export all Outlook Contacts as vCards.]]></description>
			<content:encoded><![CDATA[<p>This script will export all Outlook Contacts as vCards.<span id="more-85"></span></p>
<p>Usage:</p>
<ul>
<li>Download the <a href="/wordpress/wp-content/uploads/2006/11/Contacts.bas.txt" id="p86">Contacts module</a></li>
<li>Rename to Contacts.bas</li>
<li>Open Outlook</li>
<li>Press Alt+F11 to open the Visual-Basic editor</li>
<li>Import Contacts.bas into Outlook</li>
<li>Include a reference (Tools | References&#8230;) to &#8216;Microsoft Scripting Runtime&#8217;</li>
<li>Change the destination&#8217;s path as desired</li>
<li>Position cursor in Main</li>
<li>Press F5</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://cogniza.com/wordpress/2006/11/09/outlook-export-contacts-to-vcards/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Visual Basic: NNTP Message Threader</title>
		<link>http://cogniza.com/wordpress/2006/05/05/visual-basic-nntp-message-threader/</link>
		<comments>http://cogniza.com/wordpress/2006/05/05/visual-basic-nntp-message-threader/#comments</comments>
		<pubDate>Fri, 05 May 2006 13:13:37 +0000</pubDate>
		<dc:creator>Craig Buchanan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Visual Basic]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[NNTP]]></category>

		<guid isPermaLink="false">http://www.cogniza.com/blog/?p=41</guid>
		<description><![CDATA[This application demonstrates how to use the Microsoft Xml component thread NNTP messages by id or subject.  In addition, the application uses a plugin architecture to wrap the Nntp components from IpWorks! and ServerObjects.
Download NNTP Message Threader
]]></description>
			<content:encoded><![CDATA[<p>This application demonstrates how to use the Microsoft Xml component thread NNTP messages by id or subject.  In addition, the application uses a plugin architecture to wrap the Nntp components from IpWorks! and ServerObjects.<br />
<span id="more-41"></span><a href="http://www.cogniza.com/blog/wp-content/uploads/2006/05/Nntp.zip" id="p48">Download NNTP Message Threader</a></p>
]]></content:encoded>
			<wfw:commentRss>http://cogniza.com/wordpress/2006/05/05/visual-basic-nntp-message-threader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
