PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET Copy List(Of type) to another List(Of type)

Reply

Copy List(Of type) to another List(Of type)

 
Thread Tools Rate Thread
Old 04-04-2006, 10:00 AM   #1
Muskito
Guest
 
Posts: n/a
Default Copy List(Of type) to another List(Of type)


Hi,

Is it possible to copy the entire List(Of type) object to another
List(Of type) object
(they are both of the same type ofcourse) - without keeping the
reference?

There's the CopyTo method, but it only copies the data to a flat array.

I Need this for Backup in my program, when i update the master list and
want to rollback the entire list to the Pre-Update state...

Right now i'm considering creating a specific function to loop on the
entire list and copy field by field...

Better suggestions will be much appreciated...

  Reply With Quote
Old 04-04-2006, 10:17 AM   #2
Larry Lard
Guest
 
Posts: n/a
Default Re: Copy List(Of type) to another List(Of type)


Muskito wrote:
> Hi,
>
> Is it possible to copy the entire List(Of type) object to another
> List(Of type) object
> (they are both of the same type ofcourse) - without keeping the
> reference?
>
> There's the CopyTo method, but it only copies the data to a flat array.


One of the contructors for List(Of T) takes an IEnumerable(Of T) from
which the new list is constructed. List(Of T) implements IEnumerable(Of
T) so we can do:


Dim listA, listB As List(Of Integer)

listA = New List(Of Integer)
listA.Add(1)
listA.Add(2)
listA.Add(5)

listB = New List(Of Integer)(listA)


This is just the same as the way all the old non-generic collections
generally have a constructor that takes an IList or an ICollection to
populate the new collection from.

--
Larry Lard
Replies to group please

  Reply With Quote
Old 04-04-2006, 10:31 AM   #3
Muskito
Guest
 
Posts: n/a
Default Re: Copy List(Of type) to another List(Of type)

Hi,

Thanks for the answer - but it doesn't work...
It does keep the reference.. when i update something on the first list,
it is also updated on the backup list...

  Reply With Quote
Old 04-04-2006, 10:43 AM   #4
Larry Lard
Guest
 
Posts: n/a
Default Re: Copy List(Of type) to another List(Of type)


Muskito wrote:
> Hi,
>
> Thanks for the answer - but it doesn't work...
> It does keep the reference.. when i update something on the first list,
> it is also updated on the backup list...


Yes, this is the default behvaiour of reference types. There is no
general 'copy object' method, since 'copying' means a different thing
to every classs. List(Of T) can't possibly know how to copy a T, so
you're going to have to do it yourself.

--
Larry Lard
Replies to group please

  Reply With Quote
Old 04-04-2006, 10:50 AM   #5
Muskito
Guest
 
Posts: n/a
Default Re: Copy List(Of type) to another List(Of type)

Meanwhile on the other side of town...

I Thought this would be the final answer.... so i already made my
HomeMade copy function...

Thanks!

  Reply With Quote
Old 10-04-2006, 12:49 PM   #6
Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
Default Re: Copy List(Of type) to another List(Of type)

Larry,
| This is just the same as the way all the old non-generic collections
| generally have a constructor that takes an IList or an ICollection to
| populate the new collection from.

Be mindful: System.Collections.ObjectModel.Collection(Of T) & its
derivatives, such as BindingList(Of T), wraps the IList(Of T) passed to the
contractor, it does not copy the contents.

Collection(Of T) wrapping the IList(Of T) is very useful in that it allows
BindingList(Of T) to mediate the binding between one your collections & a
Windows Form. It also allows Collection(Of T) to be based on other
collection types, such as LinkedList(Of T) or an Array.

NOTE: To use LinkedList(Of T) with Collection(Of T) you need a version of
LinkedList(Of T) that implements IList(Of T)...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"Larry Lard" <larrylard@hotmail.com> wrote in message
news:1144142262.627507.190340@g10g2000cwb.googlegroups.com...
|
| Muskito wrote:
| > Hi,
| >
| > Is it possible to copy the entire List(Of type) object to another
| > List(Of type) object
| > (they are both of the same type ofcourse) - without keeping the
| > reference?
| >
| > There's the CopyTo method, but it only copies the data to a flat array.
|
| One of the contructors for List(Of T) takes an IEnumerable(Of T) from
| which the new list is constructed. List(Of T) implements IEnumerable(Of
| T) so we can do:
|
|
| Dim listA, listB As List(Of Integer)
|
| listA = New List(Of Integer)
| listA.Add(1)
| listA.Add(2)
| listA.Add(5)
|
| listB = New List(Of Integer)(listA)
|
|
| This is just the same as the way all the old non-generic collections
| generally have a constructor that takes an IList or an ICollection to
| populate the new collection from.
|
| --
| Larry Lard
| Replies to group please
|


  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off