简体   繁体   English

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

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

I know this is a basic coding question but I am learning on the job and trying to figure out the parameter this method is looking for, because when I leave it blank it seems to tell me it is looking for "position", which is what I have in there. 我知道这是一个基本的编码问题,但是我正在学习此作业,并试图找出该方法正在寻找的参数,因为当我将其保留为空白时,似乎告诉我它正在寻找“位置”,这就是我在那里 This is referring to the OnTradeCalledBack and OnPositionCalledBack methods. 这是指OnTradeCalledBackOnPositionCalledBack方法。

This is the error I get when leaving the parameter blank: 这是我将参数留空时得到的错误:

There is no argument given that corresponds to the required formal parameter 'position' of 'SocketServer.AsynchronousSocketListener.MyPositionCallBackEventHandler' SocketServer 没有给出与“ SocketServer.AsynchronousSocketListener.MyPositionCallBackEventHandler” SocketServer的所需形式参数“ position”相对应的参数

Here is the code: 这是代码:

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();
    }
}

Looking into the code TradeCalledBack is an event with a signature of: 查看代码TradeCalledBack是一个事件 ,其签名为:

public event MyTradeCallBackEventHandler TradeCalledBack;

Therefore TradeCalledBack will need to have the parameters passed to it that are defined in the MyTradeCallBackEventHandler which is a delegate with a signature of: 因此, TradeCalledBack将需要传递在MyTradeCallBackEventHandler中定义的参数,该参数是具有以下特征的委托

public delegate void MyTradeCallBackEventHandler(TposTradeCallback trade);

Therefore TradeCalledBack needs to have a variable of type TposTradeCallback passed into it. 因此TradeCalledBack需要有类型的变量TposTradeCallback传递给它。

To overcome the error you can pass null into the method as such: 要克服该错误,您可以将null传递给方法,如下所示:

TradeCalledBack(null);

or change the whole method to something like the following and pass the variable into it: 或将整个方法更改为以下内容,然后将变量传递给它:

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

Following the logic here you can do similar changes for OnPositionCalledBack . 按照此处的逻辑,您可以对OnPositionCalledBack进行类似的更改。

just change the definition to 只需将定义更改为

public delegate void MyPositionCallBackEventHandler(TposPositionCallback position = null);

which passes null as default if no parameter is supplied 如果没有提供参数,则默认为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM