CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2011
    Posts
    4

    How to copy one element of a Generic Collection?

    I need to copy one element of a generic collection and add it to the list. Something similar to this:

    private List<CalculationResult> cantileverResults = new List<CalculationResult>();
    cantileverResults.Add(cantileverResults[previousIndex]);

    The problem with this solution is that when I modify the new element, the previousIndex element changes as well. I believe this is because they are reference-type, not value-type. How can I just copy (clone?) the information from one element to another without affecting each other any further?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to copy one element of a Generic Collection?

    One approach is to write a copy constructor

    http://msdn.microsoft.com/en-us/libr...16(VS.80).aspx

    Then, use it.

    Code:
    private List<CalculationResult> cantileverResults = new List<CalculationResult>();
    cantileverResults.Add( new CalculationResult ( cantileverResults[previousIndex] ) );
    Another approach is to implement ICloneable

    http://msdn.microsoft.com/en-us/libr...cloneable.aspx

    Code:
    private List<CalculationResult> cantileverResults = new List<CalculationResult>();
    cantileverResults.Add( cantileverResults[previousIndex].Clone( ) as CalculationResult ) );

  3. #3
    Join Date
    Feb 2011
    Posts
    4

    Re: How to copy one element of a Generic Collection?

    Thank you. Good answer. At the end there's an extra paranthesis, but the solution works fine.

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: How to copy one element of a Generic Collection?

    There is some dispute about whether IClonable is an advisable interface to implement. Here is a sampling of arguments for and against. Mostly it boils down to IClonable not specifying whether a deep or a shallow copy is to be made (which some regard as bad).

    Against: http://blogs.msdn.com/b/brada/archiv...03/125427.aspx
    For: http://channel9.msdn.com/Forums/Tech...-best-practise

    I'm not suggesting either is correct, but I was influenced by this when I was looking into this only a few days ago and wanted to bring it to wider attention.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: How to copy one element of a Generic Collection?

    Quote Originally Posted by Arjay View Post

    Code:
    cantileverResults.Add( cantileverResults[previousIndex].Clone( ) as CalculationResult ) );
    Actually, another question. I note you use the 'as' keyword instead of just casting it. I typically do not use the 'as' keyword for any purpose. Is there a reason you preferred it here over a cast? (The difference appearing to be behavior on conversion failure; exception with a cast, return null with 'as'). If there is a performance benefit, that would be useful information. Just curious.

    Thanks.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to copy one element of a Generic Collection?

    in this I think it would be preferable to use a C style cast if only because it makes things blow up if the object is of the wrong type, and since you expect the type to be correct at all times in this case, it is better to crash instantly than in the future with a NullReferenceException.

    Quote Originally Posted by BioPhysEngr View Post
    I typically do not use the 'as' keyword for any purpose.
    The 'as' keyword is best for cases in which the cast may fail, but you expect it to happen occasionally and it is not a bug in your program. It compiles to a single CLR instruction, so even though it is probably slightly less efficient it isn't going to be a bottleneck. There is a good discussion of the implementation at Eric Lippert's blog.
    Last edited by BigEd781; February 7th, 2011 at 05:54 AM.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to copy one element of a Generic Collection?

    Let's not over think this.

    Actually I wouldn't use the ICloneable interface at all because I would prefer to return a typesafe object. That way I wouldn't need to use a cast or 'as'.

Tags for this Thread

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

Featured