我正在尝试创建一个进入类的文本文件,并将该对象存储到数据字典中,然后创建一个允许我添加,编辑和删除等的GUI。
我的文本文件使用以下语法:
国家,GDP增长,通货膨胀,贸易平衡,HDI排名,主要贸易伙伴
示例:
无论如何,在我创建GUI之前,我试图让它在控制台中首先运行。国家出现在控制台中但使用调试器中的步骤,我的对象数组和我的数据字典(如果我已正确创建数据字典)似乎是存储但是当它继续到下一个国家时它会覆盖前一个。我怎样才能让所有国家都存储而不仅仅是存储。如果这是有道理的,任何帮助将不胜感激。
我的代码:
class Program
{
static void Main(string[] args)
{
const int MAX_LINES_FILE = 50000;
string[] AllLines = new string[MAX_LINES_FILE];
int i = 0;
//reads from bin/DEBUG subdirectory of project directory
AllLines = File.ReadAllLines(@"C:\Users\Jack\Documents\countries.csv");
country[] newCountry = new country[30];
foreach (string line in AllLines)
{
if (line.StartsWith("Country")) //found first line - headers
{
headers = line.Split(',');
}
else
{
string[] columns = line.Split(',');
newCountry[i] = new country();
newCountry[i].Country=(columns[0]);
newCountry[i].GDP=(columns[1]);
newCountry[i].Inflation=(columns[2]);
newCountry[i].TB =(columns[3]);
newCountry[i].HDI =(columns[4]);
newCountry[i].TP = (columns[5]);
Dictionary<object, string> CountryList = new Dictionary<object, string>();
CountryList.Add(newCountry[i].Country, newCountry[i].GDP + "," + newCountry[i].Inflation + "," + newCountry[i].TB + "," + newCountry[i].HDI + "," + newCountry[i].TP);
i++;
foreach (KeyValuePair<object, string> country in CountryList)
{
Console.WriteLine("Country = {0}, GDP = {1}",
country.Key, country.Value);
}
}
Console.ReadKey();
}
}
public static string[] headers { get; set; }
}
public class country
{
public string Country { get; set; }
public string GDP { get; set; }
public string Inflation { get; set; }
public string TB { get; set; }
public string HDI { get; set; }
public string TP { get; set; }
}
}
编辑:按照建议移动CountryList仍然会遇到countryList计数停留在1的相同问题。
我把它放在国家级的方法中。
public static void addD(country a)
{
Dictionary<object, string> CountryList = new Dictionary<object, string>();
CountryList.Add(a.Country, a.GDP);
foreach (KeyValuePair<object, string> country in CountryList)
{
Console.WriteLine("Country = {0}, GDP = {1}", country.Key, country.Value);
}
}
从这里调用它:
newCountry[i].TB =(columns[3]);
newCountry[i].HDI =(columns[4]);
newCountry[i].TP = (columns[5]);
country.addD(newCountry[i]);
答案 0 :(得分:0)
您在<svg>
<path id="foo" d="M100,100
L150,100
a50,25 0 0,0 150,100
q50,-50 70,-170
Z" stroke-width="20" style="stroke: red; fill: none;" />
<path d="M100,100
L150,100
a50,25 0 0,0 150,100
q50,-50 70,-170
Z" style="stroke: #006666; fill: none;" />
</svg>
块中声明CountryList
,因此它会在每次迭代结束时丢失范围,并在下一次迭代时从头开始重新创建。您需要在foreach
块之外或作为类的成员声明它。
答案 1 :(得分:0)
尝试使用此代码,我已将其清理干净并添加了评论,因为它很难处理。
static void Main(string[] args)
{
// Initalize the loop iterator
int i = 0;
// Initalize a list of country data
country[] newCountry = new country[30];
// Initialize a list of countries
Dictionary<object, string> countryList = new Dictionary<object, string>();
// Read all the lines from the file
string[] fileLines = File.ReadAllLines(@"C:\Users\Jack\Documents\countries.csv");
// For each line in the file
foreach (string line in fileLines)
{
// Check if the line is a header
if (line.StartsWith("Country"))
{
headers = line.Split(',');
}
else
{
// Split the text data
string[] columns = line.Split(',');
// Initalize a new country
newCountry[i] = new country
{
Country = columns[0],
GDP = columns[1],
Inflation = columns[2],
TB = columns[3],
HDI = columns[4],
TP = columns[5]
};
countryList.Add(newCountry[i].Country, string.Format("{0},{1},{2},{3},{4}", newCountry[i].GDP, newCountry[i].Inflation, newCountry[i].TB, newCountry[i].HDI, newCountry[i].TP));
i++;
}
}
// The process is done show the data
foreach (KeyValuePair<object, string> country in countryList)
{
Console.WriteLine("Country = {0}, GDP = {1}", country.Key, country.Value);
}
// Wait for the user key
Console.ReadKey();
}
public static string[] headers { get; set; }
public class country
{
public string Country { get; set; }
public string GDP { get; set; }
public string Inflation { get; set; }
public string TB { get; set; }
public string HDI { get; set; }
public string TP { get; set; }
}
进一步更新为何使用国家/地区收藏?以下是一种更好的方法
static void Main(string[] args)
{
// Initalize a list of country data
List<country> countryList = new List<country>();
// Read all the lines from the file
string[] fileLines = File.ReadAllLines(@"countries.csv");
// For each line in the file
foreach (string line in fileLines)
{
// Check if the line is a header
if (line.StartsWith("Country"))
{
headers = line.Split(',');
}
else
{
// Split the text data
string[] columns = line.Split(',');
// Initalize a new country
country fileCountry = new country
{
Country = columns[0],
GDP = columns[1],
Inflation = columns[2],
TB = columns[3],
HDI = columns[4],
TP = columns[5]
};
countryList.Add(fileCountry);
}
}
// The process is done show the data
foreach (country country in countryList)
{
Console.WriteLine("Country = {0}, GDP = {1}", country.Country, country.GDP);
}
// Wait for the user key
Console.ReadKey();
}
public static string[] headers { get; set; }
public class country
{
public string Country { get; set; }
public string GDP { get; set; }
public string Inflation { get; set; }
public string TB { get; set; }
public string HDI { get; set; }
public string TP { get; set; }
}
根据问题的第二次更新,listbox是开始工作win UI控件的一种很好的方式我添加了下面的例子。添加一个名为button1的按钮和一个名为
的列表框控件// List holding all the country data
private List<country> countryList;
/// <summary>
/// Handle the button click
/// </summary>
/// <param name="sender">See MSDN</param>
/// <param name="e">See MSDN</param>
private void button1_Click(object sender, EventArgs e)
{
// Initalize a list of country data
this.countryList = new List<country>();
// Set the display text to the display property of the country class
this.listBox1.DisplayMember = "Display";
// Set the value of the list box item to the current country
this.listBox1.ValueMember = "Country";
// Populate the data
this.PopulateData();
// Set the list box datasource
this.listBox1.DataSource = this.countryList;
}
/// <summary>
/// Gets the country data from the csv file
/// </summary>
private void PopulateData()
{
// Read all the lines from the file
string[] fileLines = File.ReadAllLines(@"countries.csv");
// For each line in the file
foreach (string line in fileLines)
{
// Check if the line is a header
if (line.StartsWith("Country"))
{
headers = line.Split(',');
}
else
{
// Split the text data
string[] columns = line.Split(',');
// Initalize a new country
country fileCountry = new country
{
Country = columns[0],
GDP = columns[1],
Inflation = columns[2],
TB = columns[3],
HDI = columns[4],
TP = columns[5]
};
this.countryList.Add(fileCountry);
}
}
// The process is done show the data
foreach (country country in this.countryList)
{
Console.WriteLine(country.Display);
}
}
public static string[] headers { get; set; }
public class country
{
public string Country { get; set; }
public string GDP { get; set; }
public string Inflation { get; set; }
public string TB { get; set; }
public string HDI { get; set; }
public string TP { get; set; }
/// <summary>
/// Formats the display string for the country
/// </summary>
public string Display
{
get
{
return string.Format("Country = {0}, GDP = {1}", this.Country, this.GDP);
}
}
}