List<> of struct with property. Cannot change value of property. why?
- From: Zytan <zytanlithium@xxxxxxxxx>
- Date: 14 May 2007 10:28:01 -0700
This returns the following error:
"Cannot modify the return value of
'System.Collections.Generic.List<MyStruct>.this[int]' because it is
not a variable"
and I have no idea why! Do lists return copies of their elements?
Why can't I change the element itself?
class Program
{
private struct MyStruct
{
private int myVar;
public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}
}
private static List<MyStruct> list = new List<MyStruct>();
private static void Main(string[] args)
{
MyStruct x = new MyStruct();
x.MyProperty = 45;
list.Add(x);
list[0].MyProperty = 45; // <----------- ERROR HERE
}
}
Zytan
.
- Follow-Ups:
- Re: List<> of struct with property. Cannot change value of property. why?
- From: Samuel R . Neff
- Re: List<> of struct with property. Cannot change value of property. why?
- From: Bruce Wood
- Re: List<> of struct with property. Cannot change value of property. why?
- Prev by Date: Re: Sending a Sharing Invitation for a RSS Feed to a Outlook 2007 user
- Next by Date: Re: best way to enumerate List<> & remove unwanted elements?
- Previous by thread: Implicit unsafe casting in foreach? What gives?
- Next by thread: Re: List<> of struct with property. Cannot change value of property. why?
- Index(es):
Relevant Pages
|