簡體   English   中英

此名稱在當前上下文中不存在

[英]This name doesn't exist in current context

我知道這是一個基本的編碼問題,但是我正在學習此作業,並試圖找出該方法正在尋找的參數,因為當我將其保留為空白時,似乎告訴我它正在尋找“位置”,這就是我在那里 這是指OnTradeCalledBackOnPositionCalledBack方法。

這是我將參數留空時得到的錯誤:

沒有給出與“ SocketServer.AsynchronousSocketListener.MyPositionCallBackEventHandler” SocketServer的所需形式參數“ position”相對應的參數

這是代碼:

public class AsynchronousSocketListener
{
    public AsynchronousSocketListener(int port)
    {
    }

    //Need to create a delagate to handle the positions and trade information.
    public delegate void MyPositionCallBackEventHandler(TposPositionCallback position);
    public delegate void MyTradeCallBackEventHandler(TposTradeCallback trade);

    //Indicates something has happened and finished.
    //Event defined here, based on delegate

    public event MyPositionCallBackEventHandler PositionCalledBack;
    public event MyTradeCallBackEventHandler TradeCalledBack;

    //Raise the Event, need a method to do this.
    //Responsible for notifying subscribers

    protected virtual void OnPositionCalledBack()
    {
        //need to fix this, just added local to avoid error
        //TposPositionCallback position = null;
        if (PositionCalledBack != null)
            PositionCalledBack(position);
    }

    protected virtual void OnTradeCalledBack()
    {
        //need to fix this, just added local to avoid error
        //TposTradeCallback trade = null;
        if (TradeCalledBack != null)
            TradeCalledBack();
    }
}

查看代碼TradeCalledBack是一個事件 ,其簽名為:

public event MyTradeCallBackEventHandler TradeCalledBack;

因此, TradeCalledBack將需要傳遞在MyTradeCallBackEventHandler中定義的參數,該參數是具有以下特征的委托

public delegate void MyTradeCallBackEventHandler(TposTradeCallback trade);

因此TradeCalledBack需要有類型的變量TposTradeCallback傳遞給它。

要克服該錯誤,您可以將null傳遞給方法,如下所示:

TradeCalledBack(null);

或將整個方法更改為以下內容,然后將變量傳遞給它:

protected virtual void OnTradeCalledBack(TposTradeCallback trade)
{
    if (TradeCalledBack != null)
    {
        TradeCalledBack(trade);
    }
}

按照此處的邏輯,您可以對OnPositionCalledBack進行類似的更改。

只需將定義更改為

public delegate void MyPositionCallBackEventHandler(TposPositionCallback position = null);

如果沒有提供參數,則默認為null

暫無
暫無

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

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