468,783 Members | 1,590 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,783 developers. It's quick & easy.

passing a struct with an array by reference

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
public double noiseValue;//noise in dB of the data
}

public X()
{

}

public void getData(out micDataReturn rem)
{
try{
rem.micData[0] = 0;
rem.micData[1] = 0;
rem.micData[2] = 0;
}
catch(Exception e)
{
}
}

static void main()
{
X myx = new X();
micDataReturn res = new micDataReturn();
res.micData = new double[10];
X.getData(out res)
}

}

-------------------

Thanks in advance.

May 3 '07 #1
11 3109
"abhiM" <ab********@gmail.comwrote in message
news:11*********************@u30g2000hsc.googlegro ups.com...
1. Use of possibly unassigned field 'micData'
2. The out parameter 'rem' must be assigned to before control leaves
the current method
res.micData = new double[10];
X.getData(out res)
I believe that your problem would be fixed by using "ref" instead of
"out" (in the declaration and the call), since the array is going both into
and out of the method.
May 3 '07 #2
There are a few things going on here. The first is that you are
declaring the rem parameter as an out parameter. This means that you are
not expecting a value to always be passed in. This is why you are getting
the errors.

What you want to do is something like this:

public void getData(out micDataReturn rem)
{
// Assign the new instance.
rem = new micDataReturn();

// Set the noiseValue.
rem.noiseValue = <some value>;

// Allocate the array.
rem.micData = new double[10];
}

Of course, if you are just initializing the structure with 0 for the
double values, you really don't need to do anything but declare the array.
If you have other values you want to set, then you will have to do that in
your method.

Then, your client code becomes:

static void main()
{
X myx = new X();
micDataReturn res;
X.getData(out res)
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"abhiM" <ab********@gmail.comwrote in message
news:11*********************@u30g2000hsc.googlegro ups.com...
>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
public double noiseValue;//noise in dB of the data
}

public X()
{

}

public void getData(out micDataReturn rem)
{
try{
rem.micData[0] = 0;
rem.micData[1] = 0;
rem.micData[2] = 0;
}
catch(Exception e)
{
}
}

static void main()
{
X myx = new X();
micDataReturn res = new micDataReturn();
res.micData = new double[10];
X.getData(out res)
}

}

-------------------

Thanks in advance.

May 3 '07 #3
I passed the parameters using the 'ref' keyword instead of the out and
it seems to work on.
What exactly is the difference between 'ref' and 'out'?

On May 3, 1:29 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
There are a few things going on here. The first is that you are
declaring the rem parameter as an out parameter. This means that you are
not expecting a value to always be passed in. This is why you are getting
the errors.

What you want to do is something like this:

public void getData(out micDataReturn rem)
{
// Assign the new instance.
rem = new micDataReturn();

// Set the noiseValue.
rem.noiseValue = <some value>;

// Allocate the array.
rem.micData = new double[10];

}

Of course, if you are just initializing the structure with 0 for the
double values, you really don't need to do anything but declare the array.
If you have other values you want to set, then you will have to do that in
your method.

Then, your client code becomes:

static void main()
{
X myx = new X();
micDataReturn res;
X.getData(out res)

}

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"abhiM" <abhi.me...@gmail.comwrote in message

news:11*********************@u30g2000hsc.googlegro ups.com...
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
public double noiseValue;//noise in dB of the data
}
public X()
{
}
public void getData(out micDataReturn rem)
{
try{
rem.micData[0] = 0;
rem.micData[1] = 0;
rem.micData[2] = 0;
}
catch(Exception e)
{
}
}
static void main()
{
X myx = new X();
micDataReturn res = new micDataReturn();
res.micData = new double[10];
X.getData(out res)
}
}
-------------------
Thanks in advance.

May 3 '07 #4
"ref" means that the method can expect the parameter to be initialized
when it is passed to the method. "out" means that the parameter is to be
initialized by the method itself, and it should not be assumed that it is
initialized by the caller.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"abhiM" <ab********@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
>I passed the parameters using the 'ref' keyword instead of the out and
it seems to work on.
What exactly is the difference between 'ref' and 'out'?

On May 3, 1:29 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
> There are a few things going on here. The first is that you are
declaring the rem parameter as an out parameter. This means that you are
not expecting a value to always be passed in. This is why you are
getting
the errors.

What you want to do is something like this:

public void getData(out micDataReturn rem)
{
// Assign the new instance.
rem = new micDataReturn();

// Set the noiseValue.
rem.noiseValue = <some value>;

// Allocate the array.
rem.micData = new double[10];

}

Of course, if you are just initializing the structure with 0 for the
double values, you really don't need to do anything but declare the
array.
If you have other values you want to set, then you will have to do that
in
your method.

Then, your client code becomes:

static void main()
{
X myx = new X();
micDataReturn res;
X.getData(out res)

}

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"abhiM" <abhi.me...@gmail.comwrote in message

news:11*********************@u30g2000hsc.googlegr oups.com...
>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
public double noiseValue;//noise in dB of the data
}
public X()
{
}
public void getData(out micDataReturn rem)
{
try{
rem.micData[0] = 0;
rem.micData[1] = 0;
rem.micData[2] = 0;
}
catch(Exception e)
{
}
}
static void main()
{
X myx = new X();
micDataReturn res = new micDataReturn();
res.micData = new double[10];
X.getData(out res)
}
}
-------------------
Thanks in advance.


May 3 '07 #5
Short answer:
You use ref for in/out parameters, and out for out only parameters.
More info on: http://msdn2.microsoft.com/en-us/library/0f66670z(vs.71).aspx'

Regards,
Tibi
MCT, MCPD

"abhiM" <ab********@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
>I passed the parameters using the 'ref' keyword instead of the out and
it seems to work on.
What exactly is the difference between 'ref' and 'out'?

On May 3, 1:29 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
> There are a few things going on here. The first is that you are
declaring the rem parameter as an out parameter. This means that you are
not expecting a value to always be passed in. This is why you are
getting
the errors.

What you want to do is something like this:

public void getData(out micDataReturn rem)
{
// Assign the new instance.
rem = new micDataReturn();

// Set the noiseValue.
rem.noiseValue = <some value>;

// Allocate the array.
rem.micData = new double[10];

}

Of course, if you are just initializing the structure with 0 for the
double values, you really don't need to do anything but declare the
array.
If you have other values you want to set, then you will have to do that
in
your method.

Then, your client code becomes:

static void main()
{
X myx = new X();
micDataReturn res;
X.getData(out res)

}

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"abhiM" <abhi.me...@gmail.comwrote in message

news:11*********************@u30g2000hsc.googlegr oups.com...
>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
public double noiseValue;//noise in dB of the data
}
public X()
{
}
public void getData(out micDataReturn rem)
{
try{
rem.micData[0] = 0;
rem.micData[1] = 0;
rem.micData[2] = 0;
}
catch(Exception e)
{
}
}
static void main()
{
X myx = new X();
micDataReturn res = new micDataReturn();
res.micData = new double[10];
X.getData(out res)
}
}
-------------------
Thanks in advance.

May 3 '07 #6
One more question,
When i compile this function as a part of a bigger project i get the
following error
Use of unassigned local variable 'res' - This is on LINE 1, while
there is no error on LINE2

any idea whats going on?
void function(bool val)
{
MatlabWrap.micDataReturn res;
res.micData = new double[10];
if(val==true)
{
//Run a continuous loop
while (true)
{
getData(ref res); // ---------LINE 1
Thread.Sleep(1000);
}
}
else
{
getData(ref res); //----------LINE 2
}

}
}
Thanks,
AM

PS: btw I changed the 'out's to 'ref' in my previous code example.

May 3 '07 #7
On Thu, 03 May 2007 10:33:35 -0700, abhiM <ab********@gmail.comwrote:
I passed the parameters using the 'ref' keyword instead of the out and
it seems to work on.
What exactly is the difference between 'ref' and 'out'?
In addition to what Nicholas wrote, I'll point out that you may want to
consider that his original suggestion may be more appropriate than using
"ref" instead of "out".

As Nicholas says, "out" implies that the parameter need not be
initialized. Because of this, the compiler requires it to be initialized
in the method. Conversely, "ref" implies that the parameter *is*
initialized, and the compiler will complain if you try to pass in a
parameter as "ref" without initializing it first.

It's hard just from the code you've posted to know what your actual intent
is. However, I find it a little odd that you allocated an array of length
10, but only initialize the first 3 elements (ignoring for the moment that
the elements are "initialized" to 0, which is their default value
anyway). If the "getData()" method is really just initializing the
parameter, then "out" may well be a more appropriate choice (but in that
case you have to move all of your initialization code into the method,
initializing the struct instance and allocating the array).

If, on the other hand, the "getData()" method is going to be used in some
way to repeatedly fill new data into a buffer that has been pre-allocated
(as the name of the routine suggests), then changing to "ref" is probably
the right way to go.

Only you can say for sure, but please do consider the underlying design
and pick an appropriate solution based on that. Just because "ref" makes
the compiler error go away, that doesn't mean it's necessarily the right
way to fix the issue. IMHO, it's good for the code to not only work, but
also to semantically represent what is actually happening. :)

Pete
May 3 '07 #8
"If, on the other hand, the "getData()" method is going to be used
in some
way to repeatedly fill new data into a buffer that has been pre-allocated
(as the name of the routine suggests), then changing to "ref" is probably
the right way to go."
Your statement above captures it all. Its being repeatedly called to
receive data into the array.

Any idea about whats happening in my second piece of code. attached in
my previous message.

Thanks.

May 3 '07 #9
As res is of type MatlabWrap.micDataReturn which is decalred as struct, it
means that res is ValueType. Value types gets allocated on stack, and
because of that they have to be initialized befor they are first used,
because the compiler doesn't make any assumptions about the res initial
value. Because your method takes a ref parameter, then the compiler expects
that this value (res) is already initialized by you, and because it isn't,
then it generate that error message. Why only on LINE 1, and not on LINE 2?
If you solve the problem so that LINE 1 will compile, then LINE 2 will be ok
as well.

To solve that, just change
MatlabWrap.micDataReturn res;
into
MatlabWrap.micDataReturn res = new MatlabWrap.micDataReturn();

and then everything will be fine.

Regards,

Tibi
MCT, MCPD

"abhiM" <ab********@gmail.comwrote in message
news:11*********************@y80g2000hsf.googlegro ups.com...
One more question,
When i compile this function as a part of a bigger project i get the
following error
Use of unassigned local variable 'res' - This is on LINE 1, while
there is no error on LINE2

any idea whats going on?
void function(bool val)
{
MatlabWrap.micDataReturn res;
res.micData = new double[10];
if(val==true)
{
//Run a continuous loop
while (true)
{
getData(ref res); // ---------LINE 1
Thread.Sleep(1000);
}
}
else
{
getData(ref res); //----------LINE 2
}

}
}
Thanks,
AM

PS: btw I changed the 'out's to 'ref' in my previous code example.
May 3 '07 #10
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.
public double noiseValue;//noise in dB of the data
}

public X()
{

}

public void getData(out micDataReturn rem)
The out keyword should only be used if you create the instance in the
method, which you don't. As you are just changing the contents of an
existing instance, you should use the ref keyword instead.

If you use a class instead of a struct, you will automatically pass it
by reference, without having to specify it.
{
try{
rem.micData[0] = 0;
rem.micData[1] = 0;
rem.micData[2] = 0;
}
catch(Exception e)
{
Ouch. You are catching an exception without doing anything about it.
That is a really bad move, as it hides possible errors without giving
any indication that there is anything wrong. Never do this, not even in
unfinished code. If you intend to add error handling later, just rethrow
the exception until then.
}
}

static void main()
{
X myx = new X();
micDataReturn res = new micDataReturn();
res.micData = new double[10];
X.getData(out res)
}

}

-------------------

Thanks in advance.

--
Göran Andersson
_____
http://www.guffa.com
May 4 '07 #11
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.

May 4 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Dave | last post: by
2 posts views Thread by Steve Turner | last post: by
12 posts views Thread by Mike | last post: by
13 posts views Thread by Andy Baker | last post: by
4 posts views Thread by arnuld | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.