|
-
Jul 22nd, 2009, 08:50 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Swapping Collection Members' Positions
This I'm almost certain is an easy problem for those with more experience than me. I just need a routine that trades the places of 2 members of a collection class.
For example: If there are 5 objects stored in a CList of() collection class, I want to swap item # 3 and #5 (or any pair of indexes), what would be the code?
This simple problem has my head spinning because once you remove a member all the indexes change.
Thanks.
Last edited by neef; Jul 22nd, 2009 at 09:19 AM.
Intermediate Level Programmer Extraordinaire 
-
Jul 22nd, 2009, 09:09 AM
#2
Lively Member
Re: Swapping Collection Members' Positions
the trick is not to use remove because it is a collection and automatically resizes. You need something like:
Code:
Sub swapObjects(index1, index2)
tempObj = collectionname(index1)
collectionname(index1) = collectionname(index2)
collectionname(index2) = tempObj
end Sub
-
Jul 22nd, 2009, 09:14 AM
#3
Re: Swapping Collection Members' Positions
You will have to write all the validation code:
vb.net Code:
Public Class Form1 Public Sub New() InitializeComponent() Dim list As New List(Of String) list.Add("item0") list.Add("item1") list.Add("item2") list.Add("item3") list = Me.Swap(list, 0, 2) End Sub Public Function Swap(Of T)(ByVal List As IList(Of T), ByVal FirstIndex As Integer, ByVal SecondIndex As Integer) As List(Of T) Dim rList As New List(Of T)(List) Dim FirstItem As T = rList(FirstIndex) Dim SecondItem As T = rList(SecondIndex) If FirstIndex > SecondIndex Then rList.RemoveAt(FirstIndex) rList.RemoveAt(SecondIndex) rList.Insert(SecondIndex, FirstItem) rList.Insert(FirstIndex, SecondItem) Else rList.RemoveAt(SecondIndex) rList.RemoveAt(FirstIndex) rList.Insert(FirstIndex, SecondItem) rList.Insert(SecondIndex, FirstItem) End If Return rList End Function End Class
-
Jul 22nd, 2009, 09:17 AM
#4
Thread Starter
Hyperactive Member
Re: Swapping Collection Members' Positions
That did the trick, thanks. You were right to approach the problem without using Remove.
Thanks again.
Intermediate Level Programmer Extraordinaire 
-
Jul 22nd, 2009, 09:37 AM
#5
Re: [RESOLVED] Swapping Collection Members' Positions
I'm pretty sure there must be a more elegant way, using the many extension methods like Join, Union, Intersection, etc. I can't think of one though... It'd be interesting to see if we can come up with something more elegant than a 'brute force' swap.
-
Jul 22nd, 2009, 09:54 AM
#6
Re: [RESOLVED] Swapping Collection Members' Positions
Actually, I don't think it's possible using existing extensions.
However, you can create your own!
Add a new Module (must be a module!) and define the following extension method:
vb.net Code:
Imports System.Runtime.CompilerServices
Module ListExtensions
<Extension()> _
Public Sub Swap(Of T)(ByVal lst As List(Of T), ByVal index1 As Integer, ByVal index2 As Integer)
Dim tmp As T = lst.Item(index1)
lst.Item(index1) = lst.Item(index2)
lst.Item(index2) = tmp
End Function
End Module
As you can see, it's a generic method as it takes items of any type T. The first argument of the function will be the List(Of T) you call the extension function on. The second and third arguments will then 'become' the first and second arguments in the extension function.
Example usage. Suppose you have a List(Of String) called list and you want to swap items 3 and 5, then you use:
As you can see, the extension created a method in the list object, which you can now call just as any other function. That is different from having a local method called Swap and calling that. You can call the Swap method on any list, which would not be possible if you just had a local method (unless you explicitly pass that list as an argument):
vb.net Code:
Dim intList As List(Of Integer)
Dim strList As List(Of String)
Dim objList As List(Of Object)
' fill lists
...
' Swap items
intList.Swap(2,4)
strList.Swap(2,8)
objList.Swap(9,29)
Last edited by NickThissen; Jul 22nd, 2009 at 10:00 AM.
-
Jul 22nd, 2009, 09:55 AM
#7
Thread Starter
Hyperactive Member
Re: [RESOLVED] Swapping Collection Members' Positions
No it's not an elegant method, I agree. I'mhappy to be able to move on in my project, however.
Intermediate Level Programmer Extraordinaire 
-
Jul 22nd, 2009, 09:56 AM
#8
Re: [RESOLVED] Swapping Collection Members' Positions
VS2005 Nick 
But yeah, mine was pretty sloppy
-
Jul 22nd, 2009, 10:02 AM
#9
Re: [RESOLVED] Swapping Collection Members' Positions
Oops, I see VS2005 now... heh. Oh well, still extension methods are pretty useful, at least I learned something new
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|