Results 1 to 9 of 9

Thread: [RESOLVED] Swapping Collection Members' Positions

  1. #1

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    [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

  2. #2
    Lively Member KTech's Avatar
    Join Date
    Jun 2008
    Location
    Pittsburgh
    Posts
    117

    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

  3. #3
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Swapping Collection Members' Positions

    You will have to write all the validation code:

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Public Sub New()
    4.         InitializeComponent()
    5.         Dim list As New List(Of String)
    6.         list.Add("item0")
    7.         list.Add("item1")
    8.         list.Add("item2")
    9.         list.Add("item3")
    10.         list = Me.Swap(list, 0, 2)
    11.     End Sub
    12.  
    13.     Public Function Swap(Of T)(ByVal List As IList(Of T), ByVal FirstIndex As Integer, ByVal SecondIndex As Integer) As List(Of T)
    14.                 Dim rList As New List(Of T)(List)
    15.         Dim FirstItem As T = rList(FirstIndex)
    16.         Dim SecondItem As T = rList(SecondIndex)
    17.         If FirstIndex > SecondIndex Then
    18.             rList.RemoveAt(FirstIndex)
    19.             rList.RemoveAt(SecondIndex)
    20.             rList.Insert(SecondIndex, FirstItem)
    21.             rList.Insert(FirstIndex, SecondItem)
    22.         Else
    23.             rList.RemoveAt(SecondIndex)
    24.             rList.RemoveAt(FirstIndex)
    25.             rList.Insert(FirstIndex, SecondItem)
    26.             rList.Insert(SecondIndex, FirstItem)
    27.         End If
    28.         Return rList
    29.     End Function
    30.  
    31. End Class

  4. #4

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    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

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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.

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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:
    1. Imports System.Runtime.CompilerServices
    2.  
    3. Module ListExtensions
    4.  
    5.     <Extension()> _
    6.     Public Sub Swap(Of T)(ByVal lst As List(Of T), ByVal index1 As Integer, ByVal index2 As Integer)
    7.         Dim tmp As T = lst.Item(index1)
    8.         lst.Item(index1) = lst.Item(index2)
    9.         lst.Item(index2) = tmp
    10.     End Function
    11.  
    12. 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:
    vb.net Code:
    1. list.Swap(3, 5)

    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:
    1. Dim intList As List(Of Integer)
    2. Dim strList As List(Of String)
    3. Dim objList As List(Of Object)
    4.  
    5. ' fill lists
    6. ...
    7.  
    8. ' Swap items
    9. intList.Swap(2,4)
    10. strList.Swap(2,8)
    11. objList.Swap(9,29)

  7. #7

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    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

  8. #8
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [RESOLVED] Swapping Collection Members' Positions

    VS2005 Nick

    But yeah, mine was pretty sloppy

  9. #9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width