|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
by reference and by valueWhy do i get the following error? In other projects I have done (what i thought was) the same thing, without compiler complaint :-/ 1: List<MyClass> myClasses = new List<MyClass>(); 2: myClasses.Add(new MyClass(20)); 3: myClasses[0].value = 0; Line 3: Cannot modify the return value of System.Collections.Generic.List<OPS.MyClass>.this[int]' because it is not a variable And why does this print 20, and not 0? Again I thought i had done this in other applications without this happening List<MyClass> myClasses= new List<MyClass>(); myClasses.Add(new MyClass(20)); MyClass m = myClasses[0]; m.value = 0; // prints "20" Console.WriteLine(myClasses[0].value); Thanks Andrew Andrew Bullock <andrewREMOVEbullockT***@ANDntlworldTHIS.com> wrote:
> Why do i get the following error? In other projects I have done (what i My guess is that despite its name, "MyClass" is a struct. Fields in a > thought was) the same thing, without compiler complaint :-/ > > 1: List<MyClass> myClasses = new List<MyClass>(); > 2: myClasses.Add(new MyClass(20)); > 3: myClasses[0].value = 0; > > Line 3: Cannot modify the return value of > System.Collections.Generic.List<OPS.MyClass>.this[int]' because it is > not a variable struct which are referenced via a non-variable expression (a method call, property or indexer) aren't classed as variables. > And why does this print 20, and not 0? Again I thought i had done this Again, that would be consistent with MyClass being a value type rather > in other applications without this happening > > List<MyClass> myClasses= new List<MyClass>(); > myClasses.Add(new MyClass(20)); > MyClass m = myClasses[0]; > m.value = 0; > > // prints "20" > Console.WriteLine(myClasses[0].value); than a reference type. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too Jon Skeet [C# MVP] wrote:
Show quoteHide quote > Andrew Bullock <andrewREMOVEbullockT***@ANDntlworldTHIS.com> wrote: Excellent,>> Why do i get the following error? In other projects I have done (what i >> thought was) the same thing, without compiler complaint :-/ >> >> 1: List<MyClass> myClasses = new List<MyClass>(); >> 2: myClasses.Add(new MyClass(20)); >> 3: myClasses[0].value = 0; >> >> Line 3: Cannot modify the return value of >> System.Collections.Generic.List<OPS.MyClass>.this[int]' because it is >> not a variable > > My guess is that despite its name, "MyClass" is a struct. Fields in a > struct which are referenced via a non-variable expression (a method > call, property or indexer) aren't classed as variables. > >> And why does this print 20, and not 0? Again I thought i had done this >> in other applications without this happening >> >> List<MyClass> myClasses= new List<MyClass>(); >> myClasses.Add(new MyClass(20)); >> MyClass m = myClasses[0]; >> m.value = 0; >> >> // prints "20" >> Console.WriteLine(myClasses[0].value); > > Again, that would be consistent with MyClass being a value type rather > than a reference type. > Thankyou very much! You are a true legend! One further question if you don't mind, how can I duplicate my list of MyClasses? Thanks, Andrew Andrew Bullock <andrewREMOVEbullockT***@ANDntlworldTHIS.com> wrote:
> Excellent, You want a separate, independent list?> > Thankyou very much! You are a true legend! > > One further question if you don't mind, how can I duplicate my list of > MyClasses? List<MyClass> copy = new List<MyClass>(original); -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Other interesting topics
32-bit signed int to 16-bit unsigned int
Size Weird issues with DataGrid Enterprise Library Application Blocks question... picture compare Help Passing TreeView to another form and then back Editable DataGrid with DropDownList Question Blogging sites Edit Table Assembly Dll's, Interfaces and Swapping |
|||||||||||||||||||||||