生成输出时出现C#NameSpace错误

时间:2014-12-14 18:20:22

标签: c#

我在VS2013中使用C#运行此代码,我从这里开始:http://tda.codeplex.com/。该代码应该从我的TD Ameritrade 401k帐户收集数据。代码运行正常,但是当我查看输出文件夹时,没有保存文件。我一直得到这两个我无法解决的错误。我做错了什么?

namespace TDAmeritrade.Samples
{
    using System;
    using TDAmeritrade;

    class Program
    {
        static void Main()
        {
            // Initialize TD Ameritrade client, provide additional config info if needed
            var client = new TDAClient();

            // Log in to the TD Ameritrade website with your user ID and password
            client.LogIn("jessicasusername", "jessicaspassword");

            // Now 'client.User' property contains all the information about currently logged in user
            var accountName = client.User.Account.DisplayName;

            // Get stock quotes snapshot.
            var quotes = client.GetQuotes("GOOG, AAPL, $SPX.X, DUMMY");

            // 'quotes.Error' contains a list of symbols which have not been found
            var errors = quotes.Errors;

            // Find symbols matching the search string
            var symbols = client.FindSymbols("GOO");

            // Get historical prices
            var prices = client.GetHistoricalPrices("GOOG, AAPL", StartDate: DateTime.Today.AddDays(-7), EndDate: DateTime.Today.AddDays(-1));
        }
    }
}
const string SaveFileToLocation = @"C:\Users\jessica\Desktop\json_data";
string json = JsonConvert.SerializeObject(prices, Formatting.Indented);

using (StreamWriter writer = new StreamWriter(SaveFileToLocation))
{
    writer.Write(json);   
}
  

错误1命名空间不能直接包含字段或成员等成员   方法

Error 2   Expected class, delegate, enum, interface, or struct

2 个答案:

答案 0 :(得分:0)

这是因为你在类之外声明了一些变量。把主题放在class Program

namespace TDAmeritrade.Samples
{
    using System;
    using TDAmeritrade;

    class Program
    {
        const string SaveFileToLocation = @"C:\Users\jessica\Desktop\json_data";

        static void Main()
        {
            // find best place for these codes.
            string json = JsonConvert.SerializeObject(prices, Formatting.Indented);
            using (StreamWriter writer = new StreamWriter(SaveFileToLocation))
            {
                writer.Write(json);   
            }

            // Initialize TD Ameritrade client, provide additional config info if needed
            var client = new TDAClient();

            // Log in to the TD Ameritrade website with your user ID and password
            client.LogIn("jessicasusername", "jessicaspassword");

            // Now 'client.User' property contains all the information about currently logged in user
            var accountName = client.User.Account.DisplayName;

            // Get stock quotes snapshot.
            var quotes = client.GetQuotes("GOOG, AAPL, $SPX.X, DUMMY");

            // 'quotes.Error' contains a list of symbols which have not been found
            var errors = quotes.Errors;

            // Find symbols matching the search string
            var symbols = client.FindSymbols("GOO");

            // Get historical prices
            var prices = client.GetHistoricalPrices("GOOG, AAPL", StartDate: DateTime.Today.AddDays(-7), EndDate: DateTime.Today.AddDays(-1));
        }
    }
}

答案 1 :(得分:0)

您的代码的排列方式是:

using System;
using TDAmeritrade;

namespace TDAmeritrade.Samples
{    
    class Program
    {
        const string SaveFileToLocation = @"C:\Users\jessica\Desktop\json_data";

        static void Main()
        {
            // Initialize TD Ameritrade client, provide additional config info if needed
            var client = new TDAClient();

            // Log in to the TD Ameritrade website with your user ID and password
            client.LogIn("jessicasusername", "jessicaspassword")

            // Get historical prices
            var prices = client.GetHistoricalPrices("GOOG, AAPL", StartDate: DateTime.Today.AddDays(-7), EndDate: DateTime.Today.AddDays(-1));

            //You cannot create json until the prices variable has been declared!
            string json = JsonConvert.SerializeObject(prices, Formatting.Indented);
            using (StreamWriter writer = new StreamWriter(SaveFileToLocation))
            {
                writer.Write(json);   
            }
        }
    }
}