0

这只是代码的一部分

几行下来,我正在尝试将 Int 转换为 double。但是 double 是一个数组的事实使得它很难......

我需要像在上一个函数中那样包含“i”,但它不起作用,并且出现以下错误;

'webKoordx_OpenReadComplete' 没有重载匹配委托 'System.Net.OpenReadCompletedEventHandler'

如果您知道任何解决方案,或者能够看到我错过的东西,请帮助我!

private void getKoord(int i)
    {
        string stringKoX = "http://media.vgy.se/kristoferpk/spots/" + i + "/koordinatx.html";
        string stringKoY = "http://media.vgy.se/kristoferpk/spots/" + i + "/koordinaty.html";

        var webKoordx = new WebClient();
        webKoordx.OpenReadAsync(new Uri(stringKoX));
        webKoordx.OpenReadCompleted += new OpenReadCompletedEventHandler(webKoordx_OpenReadComplete);

        var webKoordy = new WebClient();
        webKoordy.OpenReadAsync(new Uri(stringKoY));
        webKoordy.OpenReadCompleted += new OpenReadCompletedEventHandler(webKoordy_OpenReadComplete);
    }

    void webKoordx_OpenReadComplete(object sender, OpenReadCompletedEventArgs e, int i)//<<-----
    {
        try
        {
            using (var reader = new StreamReader(e.Result))
            {
                koordx = reader.ReadToEnd();
                koordx_d[i] = Convert.ToDouble(koordx);
            }
        }
        catch
        {
            MessageBox.Show("Kan ej ansluta");
            MessageBox.Show("Kontrollera din anslutning");
        }
    }
    void webKoordy_OpenReadComplete(object sender, OpenReadCompletedEventArgs e)//<<-----
    {
        try
        {
            using (var reader = new StreamReader(e.Result))
            {
                koordy = reader.ReadToEnd();
                koordy_d[i] = Convert.ToDouble(koordy);
            }
        }
        catch
        {
            MessageBox.Show("Kan ej ansluta");
            MessageBox.Show("Kontrollera din anslutning");
        }
    }
4

1 回答 1

1

您不能将额外的信息传递给这样的事件处理程序。

相反,您可以添加一个 lambda 表达式来处理事件并从其闭包中传递您的额外信息:

webKoordx.OpenReadCompleted += (sender, e) => MyMethod(e.Result, i);
于 2013-04-12T21:26:17.307 回答