B
Bill Woodruff
Hi,
Assume you have a simple struct defined in its own file
namespace testStruct
{
public struct myStruct
{
public string data1;
public string data2;
}
}
You manufacture instances of myStruct in your application and store them in
a Generic List of form :
public List<myStruct> myStructList;
If you attempt in code to do something like this (assuming there is at least
two myStructs stored in the List) :
myStructList[1].data2 = "changed data";
You'll get a compiler error saying : "Cannot modify the return value of
System.Collections.List<testStruct.myStruct> this[int] because it is not a
variable.
However you can do this with no problem at compile or run-time :
myStruct scratchStruct = myStructList[1];
scratchStruct.data2 = "changed data";
I am baffled by why you can't directly assign to public variables inside an
instance of a public struct. My usual resources (Liberty, Sells, Petzold,
Archer, Gunnerson) don't yield any information on this.
I wonder if this is yet another of the interesting differences in having a
constructor-less struct vs. having a struct with a constructor ?
thanks, Bill Woodruff
Assume you have a simple struct defined in its own file
namespace testStruct
{
public struct myStruct
{
public string data1;
public string data2;
}
}
You manufacture instances of myStruct in your application and store them in
a Generic List of form :
public List<myStruct> myStructList;
If you attempt in code to do something like this (assuming there is at least
two myStructs stored in the List) :
myStructList[1].data2 = "changed data";
You'll get a compiler error saying : "Cannot modify the return value of
System.Collections.List<testStruct.myStruct> this[int] because it is not a
variable.
However you can do this with no problem at compile or run-time :
myStruct scratchStruct = myStructList[1];
scratchStruct.data2 = "changed data";
I am baffled by why you can't directly assign to public variables inside an
instance of a public struct. My usual resources (Liberty, Sells, Petzold,
Archer, Gunnerson) don't yield any information on this.
I wonder if this is yet another of the interesting differences in having a
constructor-less struct vs. having a struct with a constructor ?
thanks, Bill Woodruff