通过引用传递带有数组的结构 [英] passing a struct with an array by reference

查看:62
本文介绍了通过引用传递带有数组的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含数组的结构。我需要在函数中为

数组分配空格,并通过引用传递相应的结构,然后将另一个函数存储到数组中,以便它可以将值存储到数组中。当我用
尝试使用以下代码时我会得到这些错误

1.使用可能未分配的字段''micData''

2。 out参数''rem''必须在控制离开之前分配

当前方法


有人可以指出如何做到这一点以及我是什么做错了。任何

帮助非常感谢。

---------代码-----------

Class X {

public struct micDataReturn

{

public double [] micData; //来自麦克风的数据

public double noiseValue; //以dB为单位的噪音数据

}


public X()

{


}


public void getData(out micDataReturn rem)

{

try {

rem.micData [0] = 0;

rem.micData [1] = 0;

rem.micData [ 2] = 0;

}

catch(例外e)

{

}

}


static void main()

{

X myx = new X();

micDataReturn res = new micDataReturn();

res.micData = new double [10];

X.getData(out res)

}


}


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


提前致谢。

解决方案

&quo吨; abhiM" < ab ******** @ gmail.com写信息

新闻:11 ********************* @ u30g2000hsc.googlegro ups.com ...


1.使用可能未分配的字段''micData''

2. out参数'rem''必须在控制离开之前分配

当前方法


res.micData = new double [ 10];

X.getData(out res)



我相信你的问题可以通过使用ref来解决。而不是

out (在声明和调用中),因为数组将进入

并且不在方法之外。


有几件事情继续在这里。首先是你将
声明为rem参数作为out参数。这意味着你不会期望值总是被传入。这就是为什么你得到

的错误。


你想做的是这样的:


public void getData(out micDataReturn rem)

{

/ /分配新实例。

rem = new micDataReturn();


//设置noiseValue。

rem.noiseValue =< some value> ;;


//分配数组。

rem.micData = new double [10];

}


当然,如果你只是用

双值初始化0的结构,你真的不需要做任何事情,除了声明数组。

如果你想要设置其他值,那么你必须在

你的方法中这样做。


然后,您的客户代码变为:


static void main()

{

X myx = new X( );

micDataReturn res;

X.getData(out res)

}


希望这会有所帮助。

-

- Nicholas Paldino [。 NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" abhiM" < ab ******** @ gmail.com写信息

新闻:11 ********************* @ u30g2000hsc.googlegro ups.com ...


>我有一个包含数组的结构。我需要在函数中为

数组分配空格,并通过引用传递相应的结构,然后将另一个函数存储到数组中,以便它可以将值存储到数组中。当我用
尝试使用以下代码时我会得到这些错误

1.使用可能未分配的字段''micData''

2。 out参数''rem''必须在控制离开之前分配

当前方法


有人可以指出如何做到这一点以及我是什么做错了。任何

帮助非常感谢。

---------代码-----------

Class X {

public struct micDataReturn

{

public double [] micData; //来自麦克风的数据

public double noiseValue; //以dB为单位的噪音数据

}


public X()

{


}


public void getData(out micDataReturn rem)

{

try {

rem.micData [0] = 0;

rem.micData [1] = 0;

rem.micData [ 2] = 0;

}

catch(例外e)

{

}

}


static void main()

{

X myx = new X();

micDataReturn res = new micDataReturn();

res.micData = new double [10];

X.getData(out res)

}


}


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


先谢谢。



我使用''ref''关键字而不是out来传递参数并且

似乎可以继续工作。

''ref''和'out'之间究竟有什么区别?


5月3日下午1点29分,Nicholas Paldino [.NET / C#MVP]"

< m ... @ spam.guard.caspershouse.comwrote:


这里有一些事情发生。首先是你将
声明为rem参数作为out参数。这意味着你不会期望值总是被传入。这就是为什么你得到

的错误。


你想做的是这样的:


public void getData(out micDataReturn rem)

{

/ /分配新实例。

rem = new micDataReturn();


//设置noiseValue。

rem.noiseValue =< some value> ;;


//分配数组。

rem.micData = new double [10];


}


当然,如果你只是为

双值初始化0结构,你真的不需要除了声明数组之外什么也不做。

如果你想要设置其他值,那么你必须在

你的方法中这样做。


然后,您的客户端代码变为:


static void main()

{

X myx =新X();

micDataReturn res;

X.getData(out res)


}


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- m ... @ spam.guard.caspershouse.com


" abhiM" < abhi.me ... @ gmail.com写信息


新闻:11 ********************* @ u30g2000hsc.googlegro ups.com ...


我有一个结构,里面有一个数组。我需要在函数中为

数组分配空格,并通过引用传递相应的结构,然后将另一个函数存储到数组中,以便它可以将值存储到数组中。当我用
尝试使用以下代码时我会得到这些错误

1.使用可能未分配的字段''micData''

2。 out参数''rem''必须在控制离开之前分配

当前方法


有人可以指出怎么做这个以及我做错了什么。任何

帮助非常感谢。

---------代码-----------


Class X {

public struct micDataReturn

{

public double [] micData; //来自麦克风的数据

public double noiseValue; //数据的dB噪声

}


public X()

{


}


public void getData(out micDataReturn rem)

{

try {

rem.micData [0] = 0;

rem.micData [1] = 0;

rem.micData [2] = 0;

}

catch(例外e)

{

}

}


static void main()

{

X myx = new X();

micDataReturn res = new micDataReturn();

res.micData = new double [10];

X.getData(out res)

}


}


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


先谢谢。



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.

解决方案

"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.


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.



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.



这篇关于通过引用传递带有数组的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆