Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Sort a collection of classes by class properties

Posted on 2004-11-10
7
Medium Priority
?
221 Views
Last Modified: 2010-04-24
I have a class with a bunch of properties (mostly strings, one date time)

I have a class which inherits the collectionBase that holds the above mentioned classes.

I want to be able to create a sort function that sorts the collection by the properties of the classes it contains.

Here is the code i have so far:

Public Class MediaObject
    Private myFilename As String
    Private myTitle As String
    Private myArtist As String
    Private myAlbum As String
    Private myDuration As DateTime

    Public Property filename() As String
        Get
            Return myFilename
        End Get
        Set(ByVal Value As String)
            myFilename = Value
        End Set
    End Property
    Public Property title() As String
        Get
            Return myTitle
        End Get
        Set(ByVal Value As String)
            myTitle = Value
        End Set
    End Property
    Public Property artist() As String
        Get
            Return myArtist
        End Get
        Set(ByVal Value As String)
            myArtist = Value
        End Set
    End Property
    Public Property album() As String
        Get
            Return myAlbum
        End Get
        Set(ByVal Value As String)
            myAlbum = Value
        End Set
    End Property
    Public Property duration() As DateTime
        Get
            Return myDuration
        End Get
        Set(ByVal Value As DateTime)
            myDuration = Value
        End Set
    End Property
End Class

Public Class MediaObjectCollection
    Inherits System.Collections.CollectionBase

    Public ReadOnly Property Item(ByVal index As Integer) As MediaObject
        Get
            ' The appropriate item is retrieved from the List object and
            ' explicitly cast to the MediaObject type, then returned to the
            ' caller.
            Return CType(List.Item(index), MediaObject)
        End Get
    End Property

    Public Sub Add(ByVal MediaObjectToAdd As MediaObject)
        list.Add(MediaObjectToAdd)
    End Sub

    Public Sub Remove(ByVal index As Integer)
        ' Check to see if there is a MediaObject at the supplied index.
        If index > Count - 1 Or index < 0 Then
            ' If no MediaObject exists, a messagebox is shown and the operation is
            ' cancelled.
            System.Windows.Forms.MessageBox.Show("Index not valid!")
        Else
            ' Invokes the RemoveAt method of the List object.
            List.RemoveAt(index)
        End If
    End Sub
End Class


So I want to be able to something like this:

dim mc as new mediaobjectcollection

mc.add(mediaObject1)
mc.add(mediaObject2)
mc.add(mediaObject3)
mc.add(mediaObject4)

mc.Sort(duration)
mc.Sort(title)
...and so on...

And if it is possible, can I write the Sort function so that it dynamically figures out what properties are in the mediaObject so if I add a new property or delete a property from the mediaObject class I don't have to rewrite the mediaObjectCollection's sort function?

Thank you for any help!
0
Comment
Question by:majnun
  • 4
5 Comments
 
LVL 18

Expert Comment

by:armoghan
ID: 12552450
You need to use Reflection for this.
see this link for getting properties and then getting and setting there values

Hope it helps
0
 
LVL 18

Accepted Solution

by:
armoghan earned 2000 total points
ID: 12552452
0
 

Author Comment

by:majnun
ID: 12858435
armoghan gave me useful info, but I'm still not sure how to sort them. The biggest delima is writing the function for the mediaobjectcollection for sorting it's collection based on the properties of the items in its collection.

I don't feel my question has been fully answered. I'd give armoghan assisted answer points, but since no one has really answered my question I can't close the question with an accepted answer.

I've moved on with a different approach in the particular project, so if you want to close this question, my suggestion is to find a way of giving armoghan some points (it doesn't matter to me how much, 100-500) but close the question without making his answer "accepted" since it doesn't offer the complete answer. It would also be nice to see if someone can figure out the sorting problem, so if you want to keep the question open for a while that's fine too (though it's been open and no one has taken a crack at it).

Either way, i'm fine with whatever.

Thanks!
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12864325
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12864332
This is in VB.NET
http://aspalliance.com/461
0

Featured Post

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!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

810 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