我正在尝试获取字符串的一部分并将其设置为类的日期时间属性。字符串的日期部分是文本文件的一部分,格式为dd/mm/yyyy
.
我的代码适用于其他属性、字符串 int 等,但在尝试使用 datetime 时遇到错误。我无法确定问题出在哪里。
Hospital.exe 中发生类型为“System.IndexOutOfRangeException”的未处理异常
附加信息:索引超出数组范围。''
//read files from file
string[] linesx = File.ReadAllLines("patients.txt");//creates array of each line in text file
string[] patientInfo = new string[4];
string[] dateInfo = new string[3];
string patientLong, name;
Patient newPatient;
int age,blood,x;
DateTime date;
int year, month, day;
//sort through text and get data.
for (int i = 0; i < linesx.Length; i++)
{
patientLong = linesx[i];//gets each lines details eg.ward,56
patientInfo = patientLong.Split(',');//separates info eg,ward ,56
name = patientInfo[0];//gets name from index 0
age = Convert.ToInt32(patientInfo[1]); //gets age from index1
blood = Convert.ToInt32(patientInfo[2]);
x = Convert.ToInt32(patientInfo[3]);
dateInfo = patientInfo[2].Split('/');
day = Convert.ToInt32(dateInfo[0]);
month = Convert.ToInt32(dateInfo[1]);
year = Convert.ToInt32(dateInfo[2]);
date = new DateTime(year, month, day);
newPatient = new Patient(name, age,blood,x,date);
patients.Add(newPatient);
}
我理解了
dateInfo = patientInfo[2].Split('/');
应该
dateInfo = patientInfo[4].Split('/');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句