简体   繁体   English

如何使用本地列表作为函数的输入参数

[英]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:它给了我错误:

  • Cannot implicitly convert type 'MultivariateStatistics.Mathtool.DescriptiveStat' to 'double'无法将类型“MultivariateStatistics.Mathtool.DescriptiveStat”隐式转换为“double”

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.

相关问题 如何将列表从C#传递给Ironpython作为函数的输入参数 - How to pass a list from C# to Ironpython as input parameter of a function 如何在函数中将列表作为参数传递 - how to pass list as parameter in function 如何制作一个遍历列表并以要访问的数据成员作为输入参数的函数 - How can I make a function that loops over a list and takes which data member to access as input parameter 如何使用列表<list<int> &gt; 作为方法中的参数? </list<int> - How to use List<List<int>> as parameter in method? Azure function - 如何传递自定义输入参数 - Azure function - how to pass custom input parameter 如何在 function 中将对象列表作为参数传递,然后使用对象的属性 C# - How to pass list of objects as parameter in function and then use object's attributes C# 如何使用清单 <pair<tstring,tstring> &gt;作为C#中C ++函数的参数 - How to use list<pair<tstring,tstring>> as parameter in C++ function from C# 如何在Dapper中为Oracle使用整数参数列表? - How to use Parameter List of integers for Oracle with Dapper? 如何使用清单 <Tuple<> &gt;作为操作方法参数? - How to use a List<Tuple<>> as an action method parameter? 如何使用RabbitMQ主机连接参数列表 - How to use RabbitMQ list of hosts connection parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM