Binding (copy)

  • Thread starter Thread starter csharpula csharp
  • Start date Start date
C

csharpula csharp

Hello,
I would like to know how can I copy from BindingList<obj> to some other
BindingList<obj>?
I tried to pass one binding list to other via constractor but this is
copying it by reference and I want by value.
How can I do it?
Thank u very much!
 
Hello,
I would like to know how can I copy from BindingList<obj> to some other
BindingList<obj>?
I tried to pass one binding list to other via constractor but this is
copying it by reference and I want by value.
How can I do it?
Thank u very much!

*** Sent via Developersdexhttp://www.developersdex.com***

If you are passing any list(List<T>, BindingList<T> etc) of reference
types the elements will not be created and both list elements point to
the same object in heap. Still there will be two instances of list for
List<T> i.e.
if you use List<obj> list2 = new List<obj>(list1) and
list1.RemoveAt(0) won't remove 0th item from list2.

But this is not the case with BindingList<T>:

BindingList<obj> list1 = new BindingList<obj>;
BindingList list2 = new BindingList<obj>(list1);

now, list1.RemoveAt(0) will remove 0th item from list1 also.

A workaround could be using an intermediate list in BindingList
constructor.

BindingList<Person> list2 = new BindingList<Person>(new
List<Person>(list));

This would enable independent operations for two instances of
BindingList. But I'd reiterate - we'll be referring to the same list
memers. Changing any item in list1 will be have effect on list2 also.
 
Peter said:
Passing one instance into the constructor of a new instance is
copying the BindingList. So you don't seem to be asking the
question that you really want the answer to.

It sounds like what you really mean is that you want to clone each of
the objects in the BindingList when making the new BindingList.
This may or may not be possible, depending on the actual type of the
object. If it implements ICloneable, then you need to enumerate the
original list and call Clone() on each object to make a new instance
to add to your new BindingList.

If the type doesn't implement ICloneable, then there's not
necessarily a reliable way to do this.

You can try serializing each object to a MemoryStream, and then
deserializing it back as a new object. But whether this works for
any given type depends on whether it's serializable, and whether you
really get an exact copy of the original when it's deserialized
(many types will fail to meet one or both of those criteria).

Using reflection, you could manually do a deep copy, but this isn't a
very good general-purpose solution, as many kinds of types just
aren't meant to be duplicated. Reflection is powerful, but you run
the risk of copying something that really shouldn't have been copied
if you use it.

The best solution is simply to make sure that the type of the object
in your BindingList implements ICloneable, and then clone each
object manually while creating a new BindingList.

Pete

Man, what an excellent answer.

I'd just add that there is one other option that may or may not be
suitable depending on your obj. Object provides a shallow cloning
method called MemberwiseClone(), this would be fine if you were happy
to copy value types but leave reference types pointing to the original
referenced thing. (I would however re-itterate what Peter said, if in
doubt implement ICloneable, then you are in complete control)

Cheers Tim.
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top