开发者

Name cannot be found in current context

This is a pretty newbie question, but because I spent an afternoon trying and don't 开发者_如何转开发have any ideas left, I thought I post it here on Stackoverflow.

I'm building a program which pulls data from an open source API. I'm still a beginner, so I try to slowly tweaking the data so that it is displayed in the way I want it. However, I receive the error "The name 'MyOptionChainsInput' does not exist in the current context", and don't know how to solve it.

My goal is to make a list which gets populated in 'contract_details', in such a way that the list can be accessed in other methods (for example in Main(), which my properties have yet failed to do).

My code

using System;
using System.Collections.Generic;
using System.Text;
using Krs.Ats.IBNet;
using System.Threading;
using Krs.Ats.IBNet.Contracts;

namespace Krs.Ats.TestApp
{
public class Tester
{
    List<double> optionChainStrikes = new List<double>();
    List<string> optionChainExpiration = new List<string>();

    // properties
    public List<double> OptionChainStrikes
    {
        get
        {
            return optionChainStrikes;
        }
        set
        {
            value = optionChainStrikes;
        }
    }
    public List<string> OptionChainExpiration
    {
        get
        {
            return optionChainExpiration;
        }
        set
        {
            value = optionChainExpiration;
        }
    }

    public void MyOptionChainsInput(string expirationDate, double strike)
    {
        optionChainExpiration.Add(expirationDate);
        optionChainStrikes.Add(strike);
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Commented code
        client.ContractDetails += contract_details; 
        // Because of this line, the method 'contract_details' (see below) has to be static.

        // Output the info of the option chains (This works and is an option, however, is it the best way?)
        Tester t = new Tester();
        t.MyOptionChainOutput(); 
    }

    static void contract_details(object sender, ContractDetailsEventArgs e) // This method has to be static.
    {
        //Tester t = new Tester(); // This creates on each call an new Tester instances, which also deletes the list.
        //t.MyOptionChainsInput(e.ContractDetails.Summary.Expiry, e.ContractDetails.Summary.Strike);
        MyOptionChainsInput(e.ContractDetails.Summary.Expiry, e.ContractDetails.Summary.Strike); // this doesn't work either
    }
}
}

At first glance, the solution is simple:

  • Make the method 'contract_details' public, so it can access the 'MyOptionsChainInput' method, or
  • Create a new instance of Tester in 'contract_details' and then access MyOptionsChainInput.

However, because of the constraints of the API interface which provides this option data, both options aren't possible. How can I solve this? Perhaps I even need to discard the class Tester and find another way?


So, keep a static instance of tester around.

    // keep a static instance around
    static Tester tester = new Tester();
    static void contract_details(object sender, ContractDetailsEventArgs e) // This method has to be static.
    {
        // call the method on the static instance                
        tester.MyOptionChainsInput(e.ContractDetails.Summary.Expiry, e.ContractDetails.Summary.Strike); 
     }


Simplest way is to just put the method in a class. Look at your design, see what object should contain such a method, and put it there. The idea is to create an instance of an object that will register to the event.

Lets say class Blah will own the method:

static void Main(string[] args)
   {
        // Commented code
        Blah temp = new Blah();
        client.ContractDetails += temp.contract_details; 
        // Because of this line, the method 'contract_details' (see below) has to be static.

        // Output the info of the option chains (This works and is an option, however, is it the best way?)
        Tester t = new Tester();
        t.MyOptionChainOutput(); 
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜