?
Solved

Sorting arraylist using COM objects

Posted on 2004-09-01
12
Medium Priority
?
229 Views
Last Modified: 2010-05-18
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
-----------------------------------------------------------------------

0
Comment
Question by:tinchos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11957534
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
 
LVL 9

Author Comment

by:tinchos
ID: 11957795
Sorry bob,

but what would Project be?

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

Thanks

Tincho
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11957812
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Author Comment

by:tinchos
ID: 11957847
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
 
LVL 9

Author Comment

by:tinchos
ID: 11957871
Will that work if the object is defined using VB6?

Tincho
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11957895
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
 
LVL 9

Author Comment

by:tinchos
ID: 11957959
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11957968
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
 
LVL 9

Author Comment

by:tinchos
ID: 11966122
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11966139
I didn't mean that, and I'm not sure that what would do, and whether it would even work.

Bob
0
 
LVL 9

Author Comment

by:tinchos
ID: 11966219
What did you mean then, I guess that I didn't got the idea

Tincho
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 11966257
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

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question