[英]This name doesn't exist in current context
我知道这是一个基本的编码问题,但是我正在学习此作业,并试图找出该方法正在寻找的参数,因为当我将其保留为空白时,似乎告诉我它正在寻找“位置”,这就是我在那里 这是指OnTradeCalledBack
和OnPositionCalledBack
方法。
这是我将参数留空时得到的错误:
没有给出与“ 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.