How do you declare an Array's Count property to be consistent within a Struct?
I'd like to make a struct with one of its variables being an array that's a consistent size, even if not all data points within that array are filled.
I would like my Values[]'s count property to be consistent, but I can't find a way to set that within the struct (say, all Locations will have an array of 10 Values). How could I do that?
12 Replies
In the constructor
make it get-only and initialize it in the constructor
should do the trick
i think you need new float[size]; at that scenario no?
Angius
REPL Result: Failure
Exception: CompilationErrorException
Compile: 396.738ms | Execution: 0.000ms | React with ❌ to remove this embed.
ye
I guess the
new[]
doesn't work quite everywhere, mbWell, I still want to be able to set the Values, wouldn't this disallow that?
It will prevent you from overriding the array. Say, doing
loc.Values = new float[89267]();
It will still let you set individual values, like loc.Values[4] = 420.69f;
Mmm, but I could still do loc.Value[1] = n, kk, sweet
Also, if you're new to C#, avoid using mutable structs, they can be a cause of great pain and suffering. Mutable structs are evil (generally)
also, a struct with reference type members will work unexpectedly
e.g. if you copy that struct, all copies will reference the same array
yea that really took me off-guard when getting started back in the years