简体   繁体   English

C#将参数传递给另一种方法

[英]c# passing in paramater into another method

I am trying to define a helper class which is used to plot graphs, I was told to use Zip method to pass in each element of the array X and Y into another method, but it didn't work out nicely, can any expert point out what I have done wrong? 我正在尝试定义一个用于绘制图形的助手类,有人告诉我使用Zip方法将数组X和Y的每个元素传递到另一个方法中,但是效果不佳,任何专家都可以指出出我做错了什么? I couldn't find any similar circumstances using Google. 使用Google无法找到任何类似情况。

Or is it just me being too imaginative, this way couldn't work out at all? 还是只是我太有想像力,这种方式根本无法解决? I have see examples of calculating a pairs of x , y points using Zip method, but not passing in as parameters. 我看到了一些使用Zip方法计算成对的x,y点的示例,但没有作为参数传入。

Situation : My program has a 2 functions and 1 delegate, the first function called PlotXYAppend is used to invoke the delegate PlotXYDelegate which then passed in the method Points.addXY to do the plotting, the reason why I used chart.Invoke here is for thread safety reason . 情况:我的程序有2个函数和1个委托,第一个称为PlotXYAppend的函数用于调用委托PlotXYDelegate,然后将该委托传递给Points.addXY方法进行绘制,这是我使用chart.Invoke的原因。安全原因。

But the problem I had is the delegate or plotxyappend only take a pairs of points at a time,so I came up with a method , which is to create another function called PlotXYPass to pass in the a pair of XY points into plotxyappend to get it working, but i think there is some problem i can't solve, the intelisense is telling me they don't like the parameter i put in this function. 但是我遇到的问题是委托或plotxyappend一次只获取一对点,所以我想出了一个方法,即创建另一个名为PlotXYPass的函数以将一对XY点传递到plotxyappend中来获取它。工作,但我认为有一些我不能解决的问题,智能告诉我他们不喜欢我在此函数中输入的参数。

I greatly appreciate for any help. 我非常感谢您的帮助。

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
    }
}
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));
}

Then remove the laziness upon call, like: 然后在通话时消除懒惰,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM