生成输出时出现 C# 命名空间错误
本文关键字:命名空间 错误 输出 | 更新日期: 2023-09-27 18:34:15
我在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
这是因为您在类之外声明了一些变量。 只需将主题放在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));
}
}
}
因此,代码的排列方式是:
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);
}
}
}
}