On May 3, 11:04 pm, Göran Andersson <g...@guffa.comwrote:
abhiM wrote:
I have a struct that has an array in it. I need to assign space to the
array in a function and pass the corresponding struct by reference to
another function so that it can store values into the array. When I
try it with the following code i get these errors
1. Use of possibly unassigned field 'micData'
2. The out parameter 'rem' must be assigned to before control leaves
the current method
Could someone point out how to do this and what I am doing wrong. Any
help is greatly appreciated.
---------CODE-----------
Class X{
public struct micDataReturn
{
public double[] micData;//data from the microphone
Having an array in a structure is not very practical. You are mixing
value type semantics with reference type semantics, which easily gets
confusing. If you copy the structure, the array won't be copied only
it's reference, so you will get two separate structures that use the
same array.
Just use a class instead of a struct.
Göran stole my thunder. :-) I see no value at all in using "struct"
here. Just make it a class, and a lot of your problems go away.
In fact, there is a more subtle problem with using "struct" here...
one that may seem obscure now, but I predict that it will come back to
seriously bite later. It goes like this.
Your struct contains two things: a reference to an array, and a double
value that appears to contain some additional (summary) information
about the values in the array.
If you make the whole thing a struct, then every time you assign it
from one variable to another, or pass it to a method (just normally,
non-ref / non-out), then the "noise" value (a double), will also be
copied, and the reference to the array will be copied, _but the array
itself will not be copied_. So, if you do this:
micDataReturn a = new micDataReturn();
a.micData = new double[] { 2.5, 5.3, 5.6 };
a.noiseValue = 10;
micDataReturn b = a;
micDataReturn c = a;
then a, b, and c will each contain its own copy of the "noiseValue"
value, but _all three will point to the same micData array_. This
means that if you now change the micData array, like this:
b.micData[2] = 10.5;
b.noiseValue = 25;
then all three: a.micData[2], b.micData[2], and c.micData[2] will all
contain 10.5, but a.noiseValue and c.noiseValue will contain 10, while
b.noiseValue will contain 25.
I'm sure that I've chosen ridiculous numbers, here, but I hope that
you see the problem: the "b" structure has had its noiseValue updated
to reflect the new contents of micData, but a and c are now "damaged":
their noiseValues are out of synch with their micData.
On the other hand, if you make micData a class, then the exact same
code as above will have a, b, and c all referring to the exact same
micData object in memory, so any change to that object will be
"visible" from all three variables.
On top of this, there are issues with giving your callers open access
to internal information (micData, noiseValue are public). However, I
consider the struct / class thing to be the biggest problem here.
As Göran said, just make it a class.