我在我的C#.Net程序中有一个小问题...我有一个带有计数值的JSON文件,它指示我将会有多少个值存储到我的“对象数组”...在读取该值后,我需要我创建的类的实例来存储值...问题是...我如何访问主窗体函数以外的实例使用他们在我的一个事件函数,因为我不能让全局实例的类,因为我的计数器是在函数内部,即使它是全局的我有我的主要形式函数内的其他操作,需要在声明我的对象后立即执行...我需要访问主函数以外的类的实例C#
这里是代码...
public CurrencyForm()
{
InitializeComponent();
int count = 0;
var request = WebRequest.Create("https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json");
request.ContentType = "application/json; charset=utf-8";
string text;
var response = (HttpWebResponse)request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadLine();
while (!text.Contains("count"))
{
text = sr.ReadLine();
}
count = CurrencyUtilities.getCount(text);
}
Currency[] currency = new Currency[count];
for (int i = 0; i < count; i++)
currency[i] = new Currency();
...
}
private void SelectCurrency1_onItemSelected(object sender, EventArgs e)
{
if (SelectCurrency1.selectedValue != "USD")
{
int i = 0;
//while (!currency[i].Name.Contains(SelectCurrency1.selectedValue))
// i++;
}
}
感谢任何人!
在类范围中使用列表:'私人列表货币=新列表();'。然后,在你的函数中,只需使用列表。请参阅:https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx –
NikxDa
为什么不使用'Newtonsoft.Json'来为您解序列化数组?这是现在在c#中的标准方式 –
@NikxDa你说,而不是使用一个对象数组应该使用一个列表并推入我的对象内的权利?我从来没有使用过一个清单...我要去尝试 –