我正在嘗試創建一個系統,允許您創建一個醫院病房,然後向每個病房添加患者個人詳細信息。 意圖是能夠讀取單個文本/ csv文件中的所有信息。連接兩個類?
我目前有代碼來創建患者並添加他們的詳細信息,但創建病房並將患者分配到病房是我無法理解的。 我的講師建議使用一系列患者作爲病房班的一個屬性,但目前超出了我的理解範圍。 一個典型的病人將被寫在我的文本文件作爲 'JoeBloggs,28,03/03/1989,8' 而下面的代碼排序的屬性等
這裏是我當前的代碼來創建患者:
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
//read files from file
string[] patientList = File.ReadAllLines("MyList.txt");
string[] patientArray = new string[4];
string patientLong, name;
Patient patient;
int age, blood;
DateTime dob;
.
for (int i = 0; i < patientList.Length; i++)
{
patientLong = patientList[i];//one long string with commas eg johnmurphy,22,12/9/1998,6
patientArray = patientLong.Split(',');//split array according to comma
name = patientArray[0];
age = Convert.ToInt32(patientArray[1]);
dob = DateTime.Parse(patientArray[2]);
blood = Convert.ToInt32(patientArray[3]);
patient = new Patient(name,age,dob,blood);
bookList.Add(patient);
}
// lbxExpenses.ItemsSource = null;
lstPatients.ItemsSource = patientList;
}
和類沃德
class Ward
{
public string Name { get; set; }
public Patient [] Patient { get; set; }//array of patients as a property?
public Ward(string name,int patient)
{
Name = name;
Patient = new int[i];//currently giving error
}
public string GetWardDetails()
{
return string.Format("{0}", Name);
}
}
'病人= INT新[I]'與'公共病人[]病人{get;組; }'。你必須實例化一個'Patient'對象的數組。另外''我'不存在在當前的情況下。做到這一點的正確方法是使用'List',您可以動態添加患者對象。 (即使用'System.Collections.Generic'命名空間)。如果你想使用它,請查看'https:// www.dotnetperls.com/list'。另一種可能性是分配一個固定長度的數組,例如10個病人,你可以使用'Patients = new Patient [10];'(也可以重命名prop) –
好吧,我想我知道你的意思,但是我不太明白。我目前在我的mainwaindow中使用System.Collections.ObjectModel。 –
你可以更具體地嘗試實現嗎?是關於__creating__病房和__adding__病人,還是關於閱讀具有預定義關係的CSV文件,以及__displaying__關聯病人的病房? – grek40