簡體   English   中英

C#將參數傳遞給另一種方法

[英]c# passing in paramater into another method

我正在嘗試定義一個用於繪制圖形的助手類,有人告訴我使用Zip方法將數組X和Y的每個元素傳遞到另一個方法中,但是效果不佳,任何專家都可以指出出我做錯了什么? 使用Google無法找到任何類似情況。

還是只是我太有想像力,這種方式根本無法解決? 我看到了一些使用Zip方法計算成對的x,y點的示例,但沒有作為參數傳入。

情況:我的程序有2個函數和1個委托,第一個稱為PlotXYAppend的函數用於調用委托PlotXYDelegate,然后將該委托傳遞給Points.addXY方法進行繪制,這是我使用chart.Invoke的原因。安全原因。

但是我遇到的問題是委托或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
    }
}
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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM