Hi,
I've come across this situation a couple of times now and there must be a
better way of doing this.
Situation:
I need several values grouped together and then I need several of them. I
don't need to do any
thing much with the values so a struct would be better than a class
(performance/resource savings?).
i.e.
public struct Example {
double x, y, z;
public Example (double a, double b, double c) {
x = a;
y = b;
z = (y - x) / c;
}
// Properties for x, y, z......
}
Then in the code to use this I need to process a bunch of data that will
form these structs.
However say I process line 1 of data, I don't know if it forms all of 1
struct Example, so I have to read the next line of data. At this point I
find I have got enough data so I create a struct Example and add it to an
array. NOTE: I am not actually reading in lines of data I actually have an
arraylist of custom objects (of the same type) and I use some of their
properties, I'm just simplifying my case.
At present because I don't know how many struct Example's I will end up
creating I use an ArrayList.
When I am finished the method does: return
(Example[])myArrayList.ToArray(typeof(Example));
However here lies the problem the conversion causes all the structs to lose
there values.
I get around this by declaring as class Example instead of struct Example,
but using a class seems like a bit of overkill.
Can anybody think of a way I can create an array of structs where I don't
know how big the array needs to be when I declare the array.
Regards,
Peter