dcsimg
Results 1 to 5 of 5
  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    3

    Resolved [RESOLVED] How do I copy an object from one list to another?

    I want to make sure I do this right. How can I copy an object that is in a List to another List. What's more important, I want to make sure that the object is removed from the List it is copied from. Is there a way to do this in one step? or do I have to copy the object and then delete the previous object? This might get more confusing, since I might have the List of objects as a member in a Class. So having as few steps as possible would be what I want. Could I perhaps see an example of an efficient way to do this?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,635

    Re: How do I copy an object from one list to another?

    Firstly, it's not copying. The idea of copying is that you end up with the original and a copy. What you're talking about is moving.

    There is no way to do this in one step. What you want is to remove the item from one list and add it to the other, and that is exactly what you have to do, e.g.
    vb.net Code:
    1. Dim item = sourceList(index)
    2.  
    3. sourceList.Remove(item)
    4. destinationList.Add(item)
    You could write a method - even an extension method - that incorporated that code and then you could do it whenever you need with one line of code, e.g.
    vb.net Code:
    1. Public Sub MoveItem(Of T)(sourceList As IList(Of T),
    2.                           destinationList As IList(Of T),
    3.                           item As T)
    4.     Dim item = sourceList(index)
    5.  
    6.     sourceList.Remove(item)
    7.     destinationList.Add(item)
    8. End Sub
    vb.net Code:
    1. MoveItem(someList, someOtherList, someItem)
    By the way, I should point out that, assuming that the list contained reference type objects, i.e. instances of a class, then even if you did "copy" then it would only be copying the reference and not copying the object. There would still only be one object, although it would be in both lists. Consider a case where you were in a class and you had to stay back for detention because you misbehaved. You would be on the list of students in the class and you would also be on the list of students staying for detention but that doesn't mean that there are two of you. Programming objects are supposed to work just like real-life objects in that regard.
    Last edited by jmcilhinney; Jan 12th, 2018 at 09:45 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    3

    Re: How do I copy an object from one list to another?

    Can you recommend a link on msdn library so I can look up your example. It's exactly what I am looking for, but the MoveItem Sub looks a little complicated. I have never seen a sub ...(Of T), so I am not sure how to understand that.

    ...ahh extension method.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    3

    Re: How do I copy an object from one list to another?

    Actually I did not find it yet. Where can I find a definition of how you structured the Sub Procedure as an (Of T) followed by a parameter list?

    update: OK, I found it. "Generic Procedure"

    Thank you for the help. It's exactly what I needed.
    Last edited by electricimage; Jan 12th, 2018 at 10:00 PM.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,635

    Re: How do I copy an object from one list to another?

    You don't necessarily need that '(Of T)' part. It would also work like this:
    vb.net Code:
    1. Public Sub MoveItem(sourceList As IList,
    2.                     destinationList As IList,
    3.                     item As Object)
    4.     Dim item = sourceList(index)
    5.  
    6.     sourceList.Remove(item)
    7.     destinationList.Add(item)
    8. End Sub
    The thing is, if you did do it that way then it would allow you to call that method in code in ways that would fail at run time.

    If you want to read about this topic then look up generics. A generic method or type is one that is written in a generic way for all types and then used for a specific type at run time. For instance, the most common generic type is the List(Of T). That type represents a list of items where all items are the same type T. T can be any type you like but once you fix it to a specific type, any use of T within that generic type becomes that type. That probably sounds confusing but an example or two will hopefully elucidate the matter. The two most commonly-used members of the List(Of T) class are the Item property, which is type T, and the Add method, which has a parameter of type T. As I said, T can be any type and you fix it in your code for each particular instance. The code below fixes T to String for one instance and Integer for another:
    vb.net Code:
    1. Dim stringList As New List(Of String)
    2. Dim integerList As New List(Of Integer)
    As I said, once you fix T for a particular instance, any use of T within the class becomes that type. That means that, for 'stringList', the Item property is now type String and the Add method has a parameter of type String. Likewise, 'integerList' now has an Item property of type Integer and an Add method with a parameter of type Integer. Basically, that means that only Strings can go into or come out of a List(Of String) and only Integers can go into or come out of a List(Of Integer). You can create a List(Of Form), List(Of Control), List(Of DataTable) or any other type you like without anyone having to write any extra code beyond what is in the generic List(Of T) class.

    In the method I wrote earlier, T can be any type but each use of T within the method must be the same type. The 'sourceList' parameter is type IList(Of T) and so is 'destinationList', which means that those two lists can contain any type of items but they must both contain the same type. Also, the 'item' parameter is type T so it must be the same type as the items in the lists. That all means that this would be OK:
    vb.net Code:
    1. Dim list1 As New List(Of String)
    2. Dim list2 As New List(Of String)
    3.  
    4. '...
    5.  
    6. Dim str = "Hello World"
    7.  
    8. MoveItem(list1, list2, item)
    because that means that each usage of T in the method definition becomes String. This, on the other hand, is not OK:
    vb.net Code:
    1. Dim list1 As New List(Of String)
    2. Dim list2 As New List(Of Date)
    3.  
    4. '...
    5.  
    6. Dim n = 100
    7.  
    8. MoveItem(list1, list2, item)
    That would throw a compilation error because it does not match the declaration of the method. T can only represent one type so you can replace it with String in one place, Date in another and Integer in another.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width