c#将参数传递给另一个方法

时间:2014-07-04 11:10:48

标签: c# parameters charts delegates zip

我正在尝试定义一个用于绘制图形的辅助类,我被告知使用Zip方法将数组X和Y的每个元素传递给另一个方法,但它没有很好地解决,任何专家都可以指出我做错了什么吗?我无法使用谷歌找到任何类似的情况。

或者只是我过于富有想象力,这种方式根本无法解决? 我看到了使用Zip方法计算x,y点对的示例,但没有作为参数传入。

情况:我的程序有2个函数和1个委托,第一个名为PlotXYAppend的函数用于调用委托PlotXYDelegate,然后传入方法Points.addXY进行绘图,之所以我使用chart.Invoke here是出于线程安全的原因。

但我遇到的问题是委托或plotxyappend一次只取一对点,所以我想出了一个方法,即创建另一个名为PlotXYPass的函数,将一对XY点传递给plotxyappend让它工作,但我认为有一些问题我无法解决,知识分子告诉我他们不喜欢我在这个功能中放入的参数。

我非常感谢您的帮助。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms.DataVisualization.Charting;


namespace LastTrial
{
    public class PlotHelper
    {
        double[] X = { 1, 2, 3, 4, 5, 6 };
        double[] Y = { 1, 2, 3, 4, 5, 6 };


        Chart chart;// declare chart as chart type
        Series dataSeries;// declare dataSeries as Series type



        private delegate int PlotXYDelegate(double x, double y);

        private void PlotXYAppend(Chart chart, Series dataSeries, double x, double y)
        {
            chart.Invoke(new PlotXYDelegate(dataSeries.Points.AddXY), new Object[] { x, y });
        }// this line invokes a Delegate which pass in the addXY method defined in Points, so that it can plot a new point on a chart.


        private void PlotXYPass(double[] X, double[] Y)
        {
            X.Zip(Y, (x, y) => this.PlotXYAppend(chart,dataSeries,x,y));
        }

// trying to pass in x,y points by extracting pairs of points from list []X and []Y into the function above which only takes a pair of x,y points
    }
}

1 个答案:

答案 0 :(得分:0)

private object PlotXYAppend (Chart chart, Series dataSeries, double x, double y)
{
    return chart.Invoke(new PlotXYDelegate(dataSeries.Points.AddXY), new Object[] { x, y });    
}

public IEnumerable<object> PlotXYPass (double[] X, double[] Y)
{
    return X.Zip<double, double, object>(Y, (x, y) => this.PlotXYAppend(this.chart, this.dataSeries, x, y));
}

然后在通话时删除懒惰,如:

var ph = new PlotHelper();
ph.chart = this.chart;
ph.dataSeries = this.chart.Series[0];
var result = ph.PlotXYPass(ph.X, ph.Y).ToList();
相关问题