C#传递到paramater另一种方法 [英] c# passing in paramater into another method

查看:131
本文介绍了C#传递到paramater另一种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图定义用来绘制​​图形的辅助类,我被告知使用方法邮编数组中的X和Y到另一种方法中的每个元素通过,但它没有很好地工作了,可以任何专家指出我做了什么错?使用谷歌我找不到任何类似的情况。



或者是它只是我是太有想象力了,这样一来不能工作了呢?
我已经看到计算对x的,Y点使用Zip的方法,而不是传递的参数的例子



情况:我的程序有一个2功能和1代表,被称为PlotXYAppend的第一个函数用于调用委托PlotXYDelegate,然后在方法Points.addXY通过做绘图,为什么我用chart.Invoke这里的原因是线程安全的原因。



但我有问题是委托或plotxyappend每次只取一个点对,所以我想出了一个方法,这是创建一个名为PlotXYPass另一个函数传递在一对XY的点进去plotxyappend得到它的工作,但我认为有一些问题,我解决不了,在智能感知告诉我,他们不喜欢我把这个函数的参数。



我非常感谢所有帮助

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms.DataVisualization.Charting;


命名空间LastTrial
{
公共类PlotHelper
{
双[] X = {1,2,3,4,5 ,6};
双[] Y = {1,2,3,4,5,6};


榜图; //声明图表作为图表类型
系列dataSeries; //声明dataSeries作为系列类型



私人委托INT PlotXYDelegate(双X,双Y);

私人无效PlotXYAppend(图图表,系列dataSeries,双X,双Y)
{
chart.Invoke(新PlotXYDelegate(dataSeries.Points.AddXY),新的对象[ ] {X,Y});
} //这行调用其通过在积分定义的addXY方法,以便它可以绘制在图表上一个新的点代表。


私人无效PlotXYPass(双[] X,双[] Y)
{
X.Zip(Y(X,​​Y)=>这.PlotXYAppend(图表,dataSeries,X,Y));
}

//试图x中通过,从名单[]×提取对点和[] Y考虑上述这些只需要一对x,y中的函数y点指向
}
}


解决方案

 私有对象PlotXYAppend(图图表,系列dataSeries,双X,双Y)
{
返回chart.Invoke(新PlotXYDelegate(dataSeries.Points.AddXY),新对象[] {X,Y});
}

公开的IEnumerable<对象> PlotXYPass(双[] X,双[] Y)
{
返回X.Zip<双,双,对象>(Y(X,​​Y)=> this.PlotXYAppend(this.chart ,this.dataSeries,X,Y));
}



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

  VAR pH值=新PlotHelper(); 
ph.chart = this.chart;
ph.dataSeries = this.chart.Series [0];
VAR的结果= ph.PlotXYPass(ph.X,ph.Y).ToList();


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? I couldn't find any similar circumstances using 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.

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 .

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.

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

这篇关于C#传递到paramater另一种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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