Solved

Sorting arraylist using COM objects

Posted on 2004-09-01
12
202 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 9

Author Comment

by:tinchos
Comment Utility
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
Comment Utility
Will that work if the object is defined using VB6?

Tincho
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Author Comment

by:tinchos
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now