Setting readonly values in structs within my struct
-
I want to keep the values in "frog" readonly. However, for some reason I'm not being allowed to set these values in my constructor, even though the error reads: A readonly field cannot be assigned to (except in a constructor or a variable initializer) *Note: {{ and }} replace angle brackets, which HTML can't handle
public struct foo { public readonly List{{frog}} al; public struct frog { public readonly int legs; public readonly int eyes; } public foo(int unususedInt) { al = new List{{frog}}(); frog myFrog = new frog(); myFrog.legs = 2; myFrog.eyes = 2; al.Add(myFrog); } }
What am I doing wrong, and how can I implement this? -
I want to keep the values in "frog" readonly. However, for some reason I'm not being allowed to set these values in my constructor, even though the error reads: A readonly field cannot be assigned to (except in a constructor or a variable initializer) *Note: {{ and }} replace angle brackets, which HTML can't handle
public struct foo { public readonly List{{frog}} al; public struct frog { public readonly int legs; public readonly int eyes; } public foo(int unususedInt) { al = new List{{frog}}(); frog myFrog = new frog(); myFrog.legs = 2; myFrog.eyes = 2; al.Add(myFrog); } }
What am I doing wrong, and how can I implement this?JoeRip wrote:
public foo(int unususedInt) { al = new List{{frog}}(); frog myFrog = new frog(); myFrog.legs = 2; myFrog.eyes = 2; al.Add(myFrog); }
It looks like you have the legs and eyes fields on your struct set as readonly. I doubt the error you're encountering is being generated from writing to the list, but instead setting the properties on the frog instance. Hope this helps.
-
I want to keep the values in "frog" readonly. However, for some reason I'm not being allowed to set these values in my constructor, even though the error reads: A readonly field cannot be assigned to (except in a constructor or a variable initializer) *Note: {{ and }} replace angle brackets, which HTML can't handle
public struct foo { public readonly List{{frog}} al; public struct frog { public readonly int legs; public readonly int eyes; } public foo(int unususedInt) { al = new List{{frog}}(); frog myFrog = new frog(); myFrog.legs = 2; myFrog.eyes = 2; al.Add(myFrog); } }
What am I doing wrong, and how can I implement this?Sorry, I misread the first time.... Since those fields are readonly in addition to your list, you need to set them in the a constructor of the frog struct. You might try adding something like the following to your frog struct. public frog(int legs, int eyes) { this.legs = legs; this.eyes = eyes; } so your entire program would look like.... public struct foo { public readonly List<frog> al; public struct frog { public frog(int eyes, int legs) { this.eyes = eyes; this.legs = legs; } public readonly int legs; public readonly int eyes; } public foo(int unususedInt) { al = new List<frog>(); al.Add(new frog(2, 2)); } } Hope that solves your prob. Sorry it took me twice to get the problem. :)
-
Sorry, I misread the first time.... Since those fields are readonly in addition to your list, you need to set them in the a constructor of the frog struct. You might try adding something like the following to your frog struct. public frog(int legs, int eyes) { this.legs = legs; this.eyes = eyes; } so your entire program would look like.... public struct foo { public readonly List<frog> al; public struct frog { public frog(int eyes, int legs) { this.eyes = eyes; this.legs = legs; } public readonly int legs; public readonly int eyes; } public foo(int unususedInt) { al = new List<frog>(); al.Add(new frog(2, 2)); } } Hope that solves your prob. Sorry it took me twice to get the problem. :)