Re: passing a struct with an array by reference



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...@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
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...@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"abhiM" <abhi.me...@xxxxxxxxx> wrote in message

news:1178211210.757401.31490@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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.


.



Relevant Pages

  • Re: passing a struct with an array by reference
    ... declaring the rem parameter as an out parameter. ... public void getData(out micDataReturn rem) ... you really don't need to do anything but declare the array. ... micDataReturn res = new micDataReturn; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: passing a struct with an array by reference
    ... declaring the rem parameter as an out parameter. ... public void getData(out micDataReturn rem) ... you really don't need to do anything but declare the array. ... > micDataReturn res = new micDataReturn; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: passing a struct with an array by reference
    ... public void getData(out micDataReturn rem) ... // Allocate the array. ... if you are just initializing the structure with 0 for the ... micDataReturn res = new micDataReturn; ...
    (microsoft.public.dotnet.languages.csharp)
  • passing a struct with an array by reference
    ... I have a struct that has an array in it. ... public void getData(out micDataReturn rem) ... micDataReturn res = new micDataReturn; ...
    (microsoft.public.dotnet.languages.vc)
  • passing a struct with an array by reference
    ... I have a struct that has an array in it. ... public void getData(out micDataReturn rem) ... micDataReturn res = new micDataReturn; ...
    (microsoft.public.dotnet.languages.csharp)

Loading