извинений, если я упускаю что-то очевидное с этим!
Я надеюсь, что пользователь расшифрует код, а затем восстановит имя класса и извлечет из него все данные.
здесь я буду отображать указанную информацию пользователю:
Console.Write("Input a code:");
Console.Read();
Console.WriteLine("code: " + HU2H02.HeadCode);
var i = 0;
Console.WriteLine("Stop No: Station: Arr: Dep:");
foreach (Station stop in HU2H02.CallingPattern)
{
if (i < HU2H02.CallingPattern.Count-1)
{
if (i == 0)
Console.WriteLine("stop {0}: {1} \t {3: hh:mm}", i, stop.Name, HU2H02.ArrTimes[i], HU2H02.DepTimes[i]);
else
Console.WriteLine("stop {0}: {1} \t {2: hh:mm} - {3: hh:mm}", i, stop.Name, HU2H02.ArrTimes[i], HU2H02.DepTimes[i]);
}
else
{
Console.WriteLine("stop {0}: {1} \t {2: hh:mm}", i, stop.Name, HU2H02.ArrTimes[i]);
}
i++;
}
Вот мой код класса: (Извините, это немного грязно, так как я все еще работаю над ним.
class Train
{
public string HeadCode { get; set; }
public string Origin { get; set; }
public string Destination { get; set; }
public List<Station> callingPattern = new List<Station>();
public List<DateTime> arrTimes = new List<DateTime>();
public List<DateTime> depTimes = new List<DateTime>();
public DateTime DepartOrigin { get; set; }
public DateTime ArriveDestination { get; set; }
public string Via1 { get; set; }
public string Via2 { get; set; }
public string Via3 { get; set; }
public string Via4 { get; set; }
public object this[string Headcode]
{
get
{
Type myType = typeof(Train);
PropertyInfo myPropInfo = myType.GetProperty(Headcode);
return myPropInfo.GetValue(this, null);
}
set
{
Type myType = typeof(Train);
PropertyInfo myPropInfo = myType.GetProperty(Headcode);
myPropInfo.SetValue(this, value, null);
}
}
public Train(List<Station> patterns, string newheadCode, string timetoDepartOrigin, string Origin)
{
TimeSpan previousTime = new TimeSpan();
TimeSpan holder = new TimeSpan();
TimeSpan calcTimeSpan = new TimeSpan();
int i = 0;
foreach (Station pattern in patterns)
{
CallingPattern.Add(pattern);
if (pattern.Name == Origin)
{
depTimes.Add(Convert.ToDateTime(timetoDepartOrigin));
ArrTimes.Add(Convert.ToDateTime(timetoDepartOrigin));
previousTime = TimeSpan.Parse(timetoDepartOrigin);
}
else
{
holder = TimeSpan.Parse(callingPattern[callingPattern.Count - 2].DwellTimePeak);
calcTimeSpan = previousTime + holder;
ArrTimes.Add(DateTime.Parse(Convert.ToString(calcTimeSpan)));
if (callingPattern[callingPattern.Count - 1].DwellTimePeak != null)
{
holder = TimeSpan.Parse(callingPattern[callingPattern.Count - 1].DwellTimePeak);
calcTimeSpan = calcTimeSpan + holder;
depTimes.Add(DateTime.Parse(Convert.ToString(calcTimeSpan)));
previousTime = depTimes[depTimes.Count - 1].TimeOfDay + TimeSpan.Parse(callingPattern[callingPattern.Count - 1].TimetonextStationUP[patterns[i + 1].Name]);
}
}
i++;
}
HeadCode = newheadCode;
DepartOrigin = Convert.ToDateTime(timetoDepartOrigin);
}
public List<DateTime> ArrTimes { get => arrTimes; set => arrTimes = value; }
public List<DateTime> DepTimes { get => depTimes; set => depTimes = value; }
internal List<Station> CallingPattern { get => callingPattern; set => callingPattern = value; }
}
}
}
Нужно ли объявлять весь класс как Public?
Я надеюсь проанализировать весь класс обратно в основную программу или другой класс для отображения и далее использовать, так как я хочу, чтобы этот класс был подклассом нескольких других классов позже при разработке моей программы.
Я немного свеж в C #, поэтому извиняюсь, если что-то из этого не имеет смысла! Спасибо,
Chris