我對C#中的數組相當陌生,習慣於將大量數據存儲在字符串和INI文件中,然後使用分隔符將其分解爲基本數組......然後,我的知識幾乎不存在。對象數組的C#問題
我的主窗體類開始這樣的定義:
public CAirportData[] _AirportData; //size not known
這是我用來創建數組的方法:
...string[] airports = possibleAirports.Split(','); //size is known
foreach (string airport in airports)
{
string[] rwys = inif.Read(airport, "rwys").Split(':'); //size is known (2)
_AirportData = new CAirportData[] { new CAirportData() { icao=airport, depRwy=rwys[0], arrRwy=rwys[1] } };
}
我知道這只是歸結到我的有限對象的認識和數組。但我似乎無法在互聯網上找到任何使用這種事情的東西。我試圖將其他人的代碼結合起來,取得一點成功。
我需要_AirportData數組在表單之外,因此是公共的,並在任何方法之外聲明。我提出的主要問題是,我覆蓋數組和foreach機場,我正在創建一個新的數組,因此失去了前一個。我曾嘗試將.. = new CAirportData []移動到各種位置,但Visual Studio不喜歡它。
下面是CAirportData類的定義:
public class CAirportData
{
public string icao { get; set; }
public string depRwy { get; set; }
public string arrRwy { get; set; }
public override string ToString()
{
string result = string.Format("ICAO: {0}, Dep: {1}, Arr: {2}", this.icao, this.depRwy, this.arrRwy);
return result;
}
}
public class CMRunways
{
public string icao { get; set; }
public string depRwy { get; set; }
public string arrRwy { get; set; }
}
提前任何幫助非常感謝!
閱讀第一部分。你正在聲明一個數組_AirportData,然後每次創建一個元素的新數組。 在你的情況下,你應該使用像List等通用控件。 –
Alleo
閱讀第二個。你是對的。閱讀關於列表(例子也有)http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx。 – Alleo
通用列表是答案 – ScruffyDuck