Binding (copy)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • csharpula csharp

    Binding (copy)

    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 Developersdex http://www.developersdex.com ***
  • Peter Duniho

    #2
    Re: Binding (copy)

    On Mon, 26 May 2008 07:36:21 -0700, csharpula csharp <csharpula@yaho o.com>
    wrote:
    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.
    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

    Comment

    • Aneesh Pulukkul [http://dotnet-revolutions.blogspo

      #3
      Re: Binding (copy)

      On May 26, 7:36 pm, csharpula csharp <csharp...@yaho o.comwrote:
      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 Developersdexht tp://www.developersd ex.com***
      If you are passing any list(List<T>, BindingList<Tet c) 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<Ti.e.
      if you use List<objlist2 = 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<Per sonlist2 = new BindingList<Per son>(new
      List<Person>(li st));

      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.

      Comment

      • Tim Jarvis

        #4
        Re: Binding (copy)

        Peter Duniho wrote:
        On Mon, 26 May 2008 07:36:21 -0700, csharpula csharp
        <csharpula@yaho o.com wrote:
        >
        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.
        >
        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.
        --

        Comment

        • Peter Duniho

          #5
          Re: Binding (copy)

          On Mon, 26 May 2008 18:20:39 -0700, Tim Jarvis <tim@jarvis.com .auwrote:
          Man, what an excellent answer.
          <blush>
          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)
          Well, MemberwiseClone () is "protected" anyway. You can't use it except
          from within the class, and in that case you might as well implement
          ICloneable. If the class has non-cloneable reference types, this just
          pushes the problem down a layer, but who knows? Maybe that's not an issue
          here (either because a shallow clone is okay, or because the class has
          only cloneable members). :)

          Pete

          Comment

          Working...