473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

by reference and by value

Hi,

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

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
Mar 9 '06 #1
3 1046
Andrew Bullock <an*********************@ANDntlworldTHIS.com> wrote:
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.

--
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
Mar 9 '06 #2
Jon Skeet [C# MVP] wrote:
Andrew Bullock <an*********************@ANDntlworldTHIS.com> wrote:
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.

Excellent,

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
Mar 9 '06 #3
Andrew Bullock <an*********************@ANDntlworldTHIS.com> wrote:
Excellent,

Thankyou very much! You are a true legend!

One further question if you don't mind, how can I duplicate my list of
MyClasses?


You want a separate, independent list?

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
Mar 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
19
by: daniel | last post by:
This is a pretty basic-level question, but I'd really like to know, so thanks for any help or pointers you can provide (like what I would google for ;o) Suppose: <code> myFunc() {
6
by: Chris Simmons | last post by:
I know that a String is immutable, but I don't understand why this piece of code fails in nUnit: // BEGIN CODE using System; class Test { public static void Main( String args )
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
9
by: Edward Diener | last post by:
Can one use 'ref' ( or 'out' ) on a reference type to create a reference to a reference in C#. I know one can use it on a value type to create a reference to that value.
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.