Steve Ford
Steve Ford

Reputation: 7763

VBA Unable to implement IComparable.CompareTo

I am trying to implement IComparable.CompareTo in Outlook VBA to enable me to sort an ArrayList of a custom class. I am getting the following error:

Compile Error: Procedure declaration does not match description of event or procedure having the same name.

This is a shortened version of my class(clsEmail):

Option Explicit
Implements IComparable

Dim Sender As String
Dim Recipient As String

Function IComparable_CompareTo(ByVal obj As Object) As Integer
    'To do add logic to compare Recipients
    CompareTo = 0
End Function

I have tried various formats for the Function declaration such as IComparable_CompareTo(obj as Object) As Long etc. to no avail.

Any pointers on what I am doing wrong.

Upvotes: 2

Views: 354

Answers (1)

Mathieu Guindon
Mathieu Guindon

Reputation: 71207

As with any interface implementation or event provider, you should use the dropdowns at the top of the code pane, so that the VBIDE automatically creates the method stubs for you, instead of typing up signatures by hand.

VBIDE code pane dropdowns

This is the correct signature:

Private Function IComparable_CompareTo(ByVal obj As Variant) As Long

End Function

The differences with yours are due to the different type system in .NET; Long being a 32-bit integer (vs VBA's Integer being 16-bit), and Variant being able to wrap anything COM has no clue about (e.g. IUnknown). Object in VBA isn't Object in .NET.

Upvotes: 3

Related Questions