Click here to Skip to main content
15,885,278 members
Home / Discussions / C#
   

C#

 
GeneralRe: Opening file with in its application Pin
Hum Dum11-Nov-09 20:17
Hum Dum11-Nov-09 20:17 
QuestionProblem with BCP in C# Pin
sudhirBirlapur10-Nov-09 18:02
sudhirBirlapur10-Nov-09 18:02 
AnswerRe: Problem with BCP in C# Pin
Christian Graus10-Nov-09 18:17
protectorChristian Graus10-Nov-09 18:17 
QuestionList element copying problem Pin
Charlesh310-Nov-09 15:35
Charlesh310-Nov-09 15:35 
AnswerRe: List element copying problem Pin
Luc Pattyn10-Nov-09 15:41
sitebuilderLuc Pattyn10-Nov-09 15:41 
AnswerRe: List element copying problem Pin
Gerry Schmitz10-Nov-09 17:32
mveGerry Schmitz10-Nov-09 17:32 
GeneralRe: List element copying problem Pin
Charlesh311-Nov-09 8:09
Charlesh311-Nov-09 8:09 
GeneralRe: List element copying problem [modified] Pin
Gerry Schmitz11-Nov-09 11:38
mveGerry Schmitz11-Nov-09 11:38 
Chuck,

I had to make a few assumptions from your original post, assuming you wanted to "clone" a "member" in the list, and not the list itself.

Anyway, cloning a "list" would clone "references" for a "shallow" clone; but would create new objects for a "deep" clone. Same with cloning a single object; shallow cloning would create references for "fields" in the oject that referenced other objects, but would create new objects referenced for by those fields for a deep clone.

Anyway, here is an example of how I understood your problem in the first place; cloning an "element" (ie. object) from one list to another where that element / object does not reference other objects:

(Note that strings, while being reference types, are "deep" cloned in this case ... )

<pre>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1 {

   class Program {

         static List<Widget> List1 = new List<Widget>();
         static List<Widget> List2 = new List<Widget>();

         static void Main( string[] args ) {

            List1.Add( new Widget() {
                  Description = "I am a Widget",
                  OtherData = "12345"
            } );

            List2.Add( List1[ 0 ] );

            Display( "@1" );

            List1[ 0 ].Description = "I was changed";
            List2[ 0 ].OtherData = "abcde";

            Display( "@2" );

            List2[ 0 ] = List1[ 0 ].MemberwiseClone();
            List1[ 0 ].Description = "I was cloned";
            List2[ 0 ].Description = "I am the clone";

            Display( "@3" );
         }

         public static void Display( string location ) {
            Console.WriteLine( location + " List1 = {0}; List2 = {1}",
                  List1[ 0 ].ToString(), List2[ 0 ].ToString() );
         }
   }

   class Widget {
         public string Description;
         public string OtherData;

         public new Widget MemberwiseClone() {

            return ( Widget ) base.MemberwiseClone();
         }

         public override string ToString() {
            return Description + "(" + OtherData + ")";
         }
   }
}

</pre>

modified on Wednesday, November 11, 2009 5:46 PM

GeneralRe: List element copying problem Pin
Charlesh315-Nov-09 9:17
Charlesh315-Nov-09 9:17 
GeneralRe: List element copying problem Pin
Gerry Schmitz15-Nov-09 12:36
mveGerry Schmitz15-Nov-09 12:36 
QuestionUnable to refresh dataset in (rdlc) report Pin
Crapaw4510-Nov-09 10:07
Crapaw4510-Nov-09 10:07 
AnswerRe: Unable to refresh dataset in (rdlc) report Pin
Abhishek Sur10-Nov-09 11:09
professionalAbhishek Sur10-Nov-09 11:09 
GeneralRe: Unable to refresh dataset in (rdlc) report [solved] Pin
Crapaw4511-Nov-09 4:06
Crapaw4511-Nov-09 4:06 
GeneralRe: Unable to refresh dataset in (rdlc) report [solved] Pin
Abhishek Sur11-Nov-09 21:35
professionalAbhishek Sur11-Nov-09 21:35 
Questionre: deserialisation Pin
jdneul10-Nov-09 10:04
jdneul10-Nov-09 10:04 
AnswerRe: re: deserialisation Pin
Christian Graus10-Nov-09 10:14
protectorChristian Graus10-Nov-09 10:14 
GeneralRe: re: deserialisation Pin
jdneul10-Nov-09 10:26
jdneul10-Nov-09 10:26 
GeneralRe: re: deserialisation Pin
Abhishek Sur10-Nov-09 11:14
professionalAbhishek Sur10-Nov-09 11:14 
QuestionQuick Code Question Pin
DarkKitten10-Nov-09 9:17
DarkKitten10-Nov-09 9:17 
AnswerRe: Quick Code Question Pin
Christian Graus10-Nov-09 10:15
protectorChristian Graus10-Nov-09 10:15 
GeneralRe: Quick Code Question Pin
DarkKitten10-Nov-09 10:21
DarkKitten10-Nov-09 10:21 
GeneralRe: Quick Code Question Pin
EliottA10-Nov-09 10:53
EliottA10-Nov-09 10:53 
GeneralRe: Quick Code Question Pin
Christian Graus10-Nov-09 11:25
protectorChristian Graus10-Nov-09 11:25 
Questioncreate form Pin
farokhian10-Nov-09 9:05
farokhian10-Nov-09 9:05 
AnswerRe: create form Pin
Not Active10-Nov-09 9:14
mentorNot Active10-Nov-09 9:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.