A
Andrew Falanga
Hi,
As you know, I can't post the actual code. However, what's below is
basically a copy/paste, just cut down to illustrate without the
superfluous code.
The following code shows the problem:
namespace SomeSpace {
private struct Struct1 {
public object val;
public Struct1(object o) {
val = o;
}
}
public class MyClass {
Dictionary<string, Struct1> dict1;
public MyClass() {
dict1 = new Dictionary<string, Struct1>();
dict1.Add("key1", new Struct1(0)); // make the val an int32
dict1.Add("key2", new Struct1(0)); // ditto
}
public void F1() {
// need to fill the dictionary will some meaningful data from
// a config file
F2("myCfgFile.xml");
// now, do more meaningful stuff
}
public void F2(string path) {
// parsing routines using System.XML
// oh, something meaningful is found
dict1["key1"].val = Convert.ToInt32(xmlText); // here's my
error
}
}
}
At the point where it says, "here's my error," I'm getting an error
saying that I cannot modify the return value of Dictionary<string,
Struct1>[] because it is not a variable. What? It is to a variable,
it's an object variable in the structure.
Some digging on Google has brought me to a link that says this can't
be done because structures are allocated on the stack, instead of the
heap. The author of that response said it had to do with the
difference between value objects and reference objects. That's ok,
but I'm still at a loss for why this isn't working. Even the link
that he provided to the C# standard showed code example that looks
similar to what I've got, though not using dictionaries, but works.
Any help is greatly appreciated.
Andy
As you know, I can't post the actual code. However, what's below is
basically a copy/paste, just cut down to illustrate without the
superfluous code.
The following code shows the problem:
namespace SomeSpace {
private struct Struct1 {
public object val;
public Struct1(object o) {
val = o;
}
}
public class MyClass {
Dictionary<string, Struct1> dict1;
public MyClass() {
dict1 = new Dictionary<string, Struct1>();
dict1.Add("key1", new Struct1(0)); // make the val an int32
dict1.Add("key2", new Struct1(0)); // ditto
}
public void F1() {
// need to fill the dictionary will some meaningful data from
// a config file
F2("myCfgFile.xml");
// now, do more meaningful stuff
}
public void F2(string path) {
// parsing routines using System.XML
// oh, something meaningful is found
dict1["key1"].val = Convert.ToInt32(xmlText); // here's my
error
}
}
}
At the point where it says, "here's my error," I'm getting an error
saying that I cannot modify the return value of Dictionary<string,
Struct1>[] because it is not a variable. What? It is to a variable,
it's an object variable in the structure.
Some digging on Google has brought me to a link that says this can't
be done because structures are allocated on the stack, instead of the
heap. The author of that response said it had to do with the
difference between value objects and reference objects. That's ok,
but I'm still at a loss for why this isn't working. Even the link
that he provided to the C# standard showed code example that looks
similar to what I've got, though not using dictionaries, but works.
Any help is greatly appreciated.
Andy