Solved

Sorting arraylist using COM objects

Posted on 2004-09-01
12
216 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
Technology Partners: 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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