best way/how to....?

P

Peter Row

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
 
J

Jon Skeet [C# MVP]

Peter Row said:
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?).

The difference between a struct and a class is nothing to do with
whether or not you want to do things with them. The difference is
between a value type and a reference type - you should work out what
kind of semantics you want and use the appropriate type.

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.

It really shouldn't - and a quick test program doesn't show that
behaviour.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
I get around this by declaring as class Example instead of struct Example,
but using a class seems like a bit of overkill.

Why?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top