Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting arraylist using COM objects

Posted on 2004-09-01
12
Medium Priority
?
233 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
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

783 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