473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 3340
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. ...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
1
by: Tobias | last post by:
Hi! I have a problem which is quite tricky. I need to pass a struct from .NET to a native Win32 DLL. But i just need to pass the pointer to a reference of that struct. With my first struct this...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
1
by: AM | last post by:
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...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
13
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the...
4
by: arnuld | last post by:
I am passing an array of struct to a function to print its value. First I am getting Segfaults and weired values. 2nd, is there any elegant way to do this ? /* Learning how to use an array...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.