此名称在当前上下文中不存在

时间:2016-02-16 16:44:44

标签: c# asp.net

我知道这是一个基本的编码问题,但我正在学习这个工作,并试图弄清楚这个方法正在寻找的参数,因为当我把它留空时似乎告诉我它正在寻找“位置”,这就是我在那里所拥有的。这是指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();
    }
}

2 个答案:

答案 0 :(得分:4)

查看代码TradeCalledBackevent,签名为:

public event MyTradeCallBackEventHandler TradeCalledBack;

因此,TradeCalledBack需要将MyTradeCallBackEventHandler中定义的参数传递给它,delegate的签名为:{/ p>

public delegate void MyTradeCallBackEventHandler(TposTradeCallback trade);

因此TradeCalledBack需要传递一个TposTradeCallback类型的变量。

要克服错误,您可以将null传递给方法:

TradeCalledBack(null);

或将整个方法更改为以下内容并将变量传递给它:

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

遵循此处的逻辑,您可以对OnPositionCalledBack进行类似的更改。

答案 1 :(得分:0)

只需将定义更改为

即可
public delegate void MyPositionCallBackEventHandler(TposPositionCallback position = null);

如果没有提供参数,则默认传递null