[英]How to use local list as an input parameter for function
I am working on a project for school, and having quite a time with getting it to work.我正在为学校做一个项目,并且花了很长时间让它发挥作用。
The program flow is as follows:程序流程如下:
Here is the function doing all of the statistics with its struct:这是使用其结构进行所有统计的函数:
public struct DescriptiveStat
{
public double max, min, sum, avg, var, stddev;
public string range;
}
public static DescriptiveStat GetDescriptiveStat(List<double> data)
{
DescriptiveStat result = new DescriptiveStat();
double sum = 0, sqSum = 0, max = data[0], min = data[0];
foreach (double x in data)
{
sum += x;
sqSum += Math.Pow(x, 2);
if (x > max)
max = x;
if (x < min)
min = x;
}
int n = data.Count;
result.sum = sum;
result.max = max;
result.min = min;
result.avg = sum / n;
result.var = (sqSum - n * Math.Pow(result.avg, 2)) / n;
result.stddev = Math.Sqrt(result.var);
return result;
}
The function is set by my teacher, and cannot be changed.该功能是我老师设置的,不能更改。 It must take the
List<double> data
and return result.它必须采用
List<double> data
并返回结果。
Currently I have it so that when I press the button "Generate" in the form "DescriptiveStat" with the single variable, it calls my function to load the data into the list to prepare it to send it to DescriptiveStat function above.目前我拥有它,因此当我使用单个变量按“DescriptiveStat”表单中的“生成”按钮时,它会调用我的函数将数据加载到列表中,以准备将其发送到上面的 DescriptiveStat 函数。 Here is my code:
这是我的代码:
public void GetDescriptiveStat()
{
List<double> data = new List<double>();
double temp;
data.ToArray();
for(int i = 0; i < 94; i++)
{
temp = dblValues[i][0];
data.Add(temp);
}
data.ToList();
//data.ForEach(Console.WriteLine);
}
I know it is reading the data into the list correctly, but I am having a problem with using this local list as a parameter for function DescriptiveStat GetDescriptiveStat(List<double>data)
- when I try inserting something like double result = Mathtool.GetDescriptiveStat(data);
我知道它正在正确地将数据读入列表,但是我在使用此本地列表作为函数
DescriptiveStat GetDescriptiveStat(List<double>data)
的参数时遇到问题 - 当我尝试插入类似double result = Mathtool.GetDescriptiveStat(data);
it gives me the error:它给了我错误:
Can anyone please assist with what I am doing wrong, or how I can use the list as a parameter for the function?任何人都可以帮助我做错什么,或者我如何使用列表作为函数的参数?
Thank you.谢谢你。
First you don't have to convert your list to an array ( data.ToArray()
) and then back to a list ( data.ToList()
).首先,您不必将列表转换为数组(
data.ToArray()
),然后再转换回列表( data.ToList()
)。 Second instead of void
let your function return List<double>
like shown below: Second 而不是
void
让您的函数返回List<double>
如下所示:
public List<double> GetDescriptiveStat()
{
List<double> data = new List<double>();
for (int i = 0; i < 94; i++)
{
data.Add(dblValues[i][0]);
}
return data;
}
Now because your function GetDescriptiveStat()
returns a list you can send it as an input parameter into the teachers function GetDescriptiveStat(List<double> data)
:现在,因为您的函数
GetDescriptiveStat()
返回一个列表,您可以将其作为输入参数发送到教师函数GetDescriptiveStat(List<double> data)
:
DescriptiveStat myData = GetDescriptiveStat(GetDescriptiveStat());
There are maybe better ways to do this but you explicitly said that the teachers function cannot be changed.也许有更好的方法可以做到这一点,但您明确表示教师功能无法更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.