Click here to Skip to main content
15,898,373 members
Home / Discussions / C#
   

C#

 
QuestionError-Object reference not set to an instance of an object Pin
T4AMD29-Nov-07 7:30
T4AMD29-Nov-07 7:30 
AnswerRe: Error-Object reference not set to an instance of an object Pin
Alaric_29-Nov-07 8:19
professionalAlaric_29-Nov-07 8:19 
QuestionRe: Error-Object reference not set to an instance of an object Pin
T4AMD29-Nov-07 8:33
T4AMD29-Nov-07 8:33 
AnswerRe: Error-Object reference not set to an instance of an object Pin
Luc Pattyn29-Nov-07 8:46
sitebuilderLuc Pattyn29-Nov-07 8:46 
GeneralRe: Error-Object reference not set to an instance of an object Pin
T4AMD29-Nov-07 8:55
T4AMD29-Nov-07 8:55 
Questionremoving comments from xml strings Pin
AndrusM29-Nov-07 7:29
AndrusM29-Nov-07 7:29 
AnswerRe: removing comments from xml strings Pin
Dave Kreskowiak29-Nov-07 9:27
mveDave Kreskowiak29-Nov-07 9:27 
GeneralRe: removing comments from xml strings Pin
AndrusM29-Nov-07 22:13
AndrusM29-Nov-07 22:13 
I did'nt understand your reply. You wrote that this code modifies same collection inside foreach loop which is not allowed.
.NET does not throw exception for this code.
So it is .NET bug, isn't it ?

Chris Shepherd explains:

A cloned IEnumerable instance is *not* the same IEnumerable, by nature. It may have references to the same data, but it
does not necessarily contain the same list of references. If you remove
items from the newly cloned instance, it won't affect its progenitor,
and vice versa. Kind of the point of clone, really.

This code is functionally working with two different IEnumerable objects. Below is an example of effectively what
original code is doing -- removing a subset of items in one list from
another list.

class General<br />
{<br />
     class ClassA<br />
     {<br />
         public string name;<br />
<br />
         public ClassA(string nm) { this.name = nm; }<br />
     }<br />
<br />
     static void Main(string[] args)<br />
     {<br />
         List<ClassA> listOne = new List<ClassA>();<br />
         List<ClassA> listTwo = new List<ClassA>();<br />
<br />
         ClassA a = new ClassA("A");<br />
         ClassA b = new ClassA("B");<br />
         ClassA c = new ClassA("C");<br />
         ClassA d = new ClassA("D");<br />
         ClassA e = new ClassA("E");<br />
<br />
         listOne.AddRange(new ClassA[] { a, b, c, d, e });<br />
         listTwo.AddRange(new ClassA[] { a, c, d });<br />
<br />
         string msg = "Start Length of listOne: " +<br />
             listOne.Count.ToString() +<br />
             "\nStart Length of listTwo: " +<br />
             listTwo.Count.ToString() +<br />
             "\nWhat is removed:\n";<br />
         foreach (ClassA z in listTwo)<br />
         {<br />
             msg += z.name + "\t";<br />
             listOne.Remove(z);<br />
         }<br />
<br />
         Console.WriteLine(msg + "\nLength of listOne: " +<br />
             listOne.Count.ToString() +<br />
             "\nLength of listTwo: " +<br />
             listTwo.Count.ToString());<br />
     }<br />
}<br />


Andrus

GeneralRe: removing comments from xml strings Pin
Dave Kreskowiak30-Nov-07 2:32
mveDave Kreskowiak30-Nov-07 2:32 
GeneralRe: removing comments from xml strings Pin
AndrusM30-Nov-07 3:02
AndrusM30-Nov-07 3:02 
GeneralRe: removing comments from xml strings Pin
Dave Kreskowiak30-Nov-07 16:12
mveDave Kreskowiak30-Nov-07 16:12 
GeneralRe: removing comments from xml strings Pin
AndrusM1-Dec-07 6:32
AndrusM1-Dec-07 6:32 
GeneralRe: removing comments from xml strings Pin
PIEBALDconsult1-Dec-07 8:31
mvePIEBALDconsult1-Dec-07 8:31 
GeneralRe: removing comments from xml strings Pin
Dave Kreskowiak1-Dec-07 12:41
mveDave Kreskowiak1-Dec-07 12:41 
QuestionStrange frequency readings Pin
Guybrush8629-Nov-07 7:19
Guybrush8629-Nov-07 7:19 
AnswerApply a frequency filter Pin
Ennis Ray Lynch, Jr.29-Nov-07 8:01
Ennis Ray Lynch, Jr.29-Nov-07 8:01 
AnswerRe: Strange frequency readings Pin
Luc Pattyn29-Nov-07 9:12
sitebuilderLuc Pattyn29-Nov-07 9:12 
AnswerRe: Strange frequency readings Pin
leppie29-Nov-07 9:54
leppie29-Nov-07 9:54 
GeneralRe: Strange frequency readings Pin
Luc Pattyn29-Nov-07 9:59
sitebuilderLuc Pattyn29-Nov-07 9:59 
GeneralRe: Strange frequency readings Pin
leppie29-Nov-07 10:17
leppie29-Nov-07 10:17 
GeneralRe: Strange frequency readings Pin
Luc Pattyn29-Nov-07 11:16
sitebuilderLuc Pattyn29-Nov-07 11:16 
GeneralRe: Strange frequency readings Pin
Big Daddy Farang29-Nov-07 12:26
Big Daddy Farang29-Nov-07 12:26 
Questionclickonce error Pin
arkiboys29-Nov-07 6:58
arkiboys29-Nov-07 6:58 
QuestionLINQ to SQL Question Pin
Philip Laureano29-Nov-07 5:38
Philip Laureano29-Nov-07 5:38 
GeneralRe: LINQ to SQL Question Pin
Sam Xavier18-Jan-08 0:02
Sam Xavier18-Jan-08 0:02 

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.