我在我的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你說,而不是使用一個對象數組應該使用一個列表並推入我的對象內的權利?我從來沒有使用過一個清單...我要去嘗試 –