Sorting arraylist using COM objects

Hi

My problem is the following.....

I've developed an ObjectVector class, which is COM Visible, whichs wraps the ArrayList functionality so it can be used for all languages via COM.

So far, it is used from Visual Basic 6 and the data inserted are objects of a type defined in VB.

The problem comes when you want to sort the vector.
I can't make it work, I mean I don't know how to make a comparer that compares this kind of objects.

Any ideas?

Thanks in advance

Tincho

Example

PointVB Class
-----------------------------------------------------------------------
Option Explicit
Dim x As Integer
Dim y As Integer

Public Sub setX(xx As Integer)
x = xx
End Sub

Public Sub setY(yy As Integer)
y = yy
End Sub


Public Function getSum() As Integer
getSum = x + y
End Function
-----------------------------------------------------------------------


Usage
-----------------------------------------------------------------------
Private Sub Command1_Click()

Dim vector As New UtilityCOMClasses.ObjectVector

'p As New PointVB
Dim p1 As New PointVB
p1.setX 1
p1.setY 2

Dim p2 As New PointVB
p2.setX 2
p2.setY 3

Dim p3 As New PointVB
p3.setX 3
p3.setY 4

vector.Add p1
vector.Add p2
vector.Add p3

///// Here comes the problem
vector.Sort

MsgBox "Punto 1: " & map.Item(1).getSum & vbCrLf & _
        "Punto 2: " & map.Item(2).getSum & vbCrLf & _
        "Punto 3: " & map.Item(3).getSum
End Sub
-----------------------------------------------------------------------

LVL 9
tinchosAsked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
I was talking about putting the Comparer in the .NET class, since I assumed that you meant that your class was in .NET, and exposed to the COM world.  Whenever you implement the IComparer interface, and you need to Sort, then the Compare method is used to perform the sort.  So you need to provide the functionality to determine what a sort order would be for whatever object that you are going to pass to the .NET class.

Bob
0
 
Bob LearnedCommented:
Example:

 Public Class ManagerUserNameComparer : Implements IComparer
         
         Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
            Dim first As Project = CType(x, Project)
            Dim second As Project = CType(y, Project)
            Return first.ManagerUserName.CompareTo(second.ManagerUserName)
         End Function 'Compare
      End Class 'ManagerUserNameComparer

Bob
0
 
tinchosAuthor Commented:
Sorry bob,

but what would Project be?

            Dim first As Project = CType(x, Project)
            Dim second As Project = CType(y, Project)

Thanks

Tincho
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Bob LearnedCommented:
Just the object that needs to be compared.  The parameters for the Compare are objects, so it is better to cast them to specific types to do the comparison.

Bob
0
 
tinchosAuthor Commented:
I've found a solution for it.

I've defined an ObjectComparable COM interface in C#.
All classes to be sorted must implement this interface.

so, in VB 6 I would have

PointVB Class
-----------------------------------------------------------------------
Option Explicit
Implements UtilityCOMClasses.ObjectComparable

Dim x As Integer
Dim y As Integer

Public Function ObjectComparable_CompareTo(ByVal o) As Long

Dim point As PointVB
Set point = o

If Me.getSum < point.getSum Then
    ObjectComparable_CompareTo = -1
ElseIf Me.getSum = point.getSum Then
    ObjectComparable_CompareTo = 0
Else
    ObjectComparable_CompareTo = 1
End If
   
End Function


Public Sub setX(xx As Integer)
x = xx
End Sub

Public Sub setY(yy As Integer)
y = yy
End Sub


Public Function getSum() As Integer
getSum = x + y
End Function


After this I've implemented a comparer class which implements .NET IComparer which looks like.....

using System;
using System.Collections;                                    // IComparer
using System.Runtime.InteropServices;                  // COMVisible

namespace UtilityCOMClasses
{
      public class ObjectComparer : IComparer
      {
            public ObjectComparer()
            {}

            public int Compare( object x, object y      )
            {
                  ObjectComparable p1 = (ObjectComparable)x;

                  return p1.CompareTo( y );
            }
      }
}


So in my wrapper I would sort the vector using ObjectComparer

public class MyCOMVector
{
             // code

            public void Sort()
            {
                  this.objects.Sort( new ObjectComparer() );
            }

            private ArrayList objects;
}

and all objects added in my vector should implement ObjectComparable

This is just the solution I've found. If bob brings me another one, I'll accept his answer, otherwise I'll asked this q to be paqed

Tincho
0
 
tinchosAuthor Commented:
Will that work if the object is defined using VB6?

Tincho
0
 
Bob LearnedCommented:
I am not sure.  Objects are a generic type in both C# and VB6.  I would have to say that you should try it and let me know.  

BTW, if you would like to PAQ this question, because you found your own solution, and didn't feel that I was helpful, then I have no problem.  My only concern is for finding a solution, and even learning something new in the process :)

Bob
0
 
tinchosAuthor Commented:
I mean,

While you posted your comment, I found the solution, and even though it hasn't helped me, I dont want to disregard your comment, and that's why I said I would accept your comment as an answer if you give me a full solution, so as to give credit for the time you invested.

Right now, the solution I posted is working and I don't have much time to go on trying the other one.
If you want I would leave the question open and I'll close it when I check it.

Tincho
0
 
Bob LearnedCommented:
That sounds good to me.  Just test it out, and let me know what happened.

No offense taken.  You handle the question any way that you see fit.

Bob
0
 
tinchosAuthor Commented:
Hi Bob

just a question

when you said

>> Public Class ManagerUserNameComparer : Implements IComparer
>>         
>>         Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
>>            Dim first As Project = CType(x, Project)
>>            Dim second As Project = CType(y, Project)
>>            Return first.ManagerUserName.CompareTo(second.ManagerUserName)
>>         End Function 'Compare
>>      End Class 'ManagerUserNameComparer

you meant that I should implement this in my VB6 dll, (the same that uses my container), didn't you?

Tincho
0
 
Bob LearnedCommented:
I didn't mean that, and I'm not sure that what would do, and whether it would even work.

Bob
0
 
tinchosAuthor Commented:
What did you mean then, I guess that I didn't got the idea

Tincho
0
All Courses

From novice to tech pro — start learning today.