【问题标题】:Name cannot be found in current context在当前上下文中找不到名称
【发布时间】:2010-12-23 17:26:35
【问题描述】:

这是一个相当新手的问题,但是因为我花了一个下午的时间尝试并且没有任何想法,所以我想我将它发布在 Stackoverflow 上。

我正在构建一个从开源 API 提取数据的程序。我还是个初学者,所以我试着慢慢调整数据,让它以我想要的方式显示。但是,我收到错误“当前上下文中不存在名称'MyOptionChainsInput'”,不知道如何解决。

我的目标是创建一个在“contract_details”中填充的列表,以便可以在其他方法中访问该列表(例如在 Main() 中,我的属性有但没能做到)。

我的代码

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
    }
}
}

乍一看,解决方法很简单:

  • 将方法“contract_details”公开,以便它可以访问“MyOptionsChainInput”方法,或者
  • 在“contract_details”中创建一个新的 Tester 实例,然后访问 MyOptionsChainInput。

但是,由于提供此选项数据的 API 接口的限制,这两个选项都不可能。我该如何解决这个问题?也许我什至需要丢弃类 Tester 并找到另一种方法?

【问题讨论】:

    标签: c# methods static


    【解决方案1】:

    所以,保留一个静态的测试器实例。

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

    【讨论】:

    • 感谢您的回复。我已经尝试过了,但是因为 API 程序为每一行新的数据调用 'contract_details' 方法,我的测试器在每次调用时都会被重写,结果列表永远不会被填满。 :)
    • @Jura25 查看我的更新答案。也许测试器的静态实例会为你工作
    【解决方案2】:

    最简单的方法是将方法放在一个类中。 看看你的设计,看看哪个对象应该包含这样的方法,然后把它放在那里。这个想法是创建一个将注册到事件的对象的实例。

    假设 Blah 类将拥有该方法:

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

    【讨论】:

    • 感谢您的快速回复。我不认为我完全理解你。我已将该方法放在一个类中(MyOptionsChainInput 位于 Tester 类中)。无法移动方法“contract_details”,因为 API 使用它来补充 ContractDetails() 方法。还是除了 Tester 类之外我还需要一个新类 Blah?
    猜你喜欢
    • 2016-02-07
    • 1970-01-01
    • 2013-10-02
    • 2014-04-22
    • 2017-06-17
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多