So I want to find the oldest person in a school class. The age is calculated according to his personal code. For example, 120499-12345, where the first part is the date and the first number after the "-" can be 1 or 2,depending on when did person born(before 2000 - "1", after 2000 - "2"). Personal code is a string type and I used substrings to get the year from code and the 1 or 2 to calculate the age. And the problem is that I don't really understand how to find the oldest person.
public void Oldest()
{
int age = 0;
foreach (Student b in students)// students is an array of Student class
{
string sub1 = b.pers_code.Substring(4, 2);
string sub2 = b.pers_code.Substring(7, 1);
int type = 0;
type = Convert.ToInt32(sub2);
int year = 0;
year = Convert.ToInt32(sub1);
if (type == 2)
{
age = 18 - year;
}
else
{
age = 2018 - (year + 1900);
}
}
}
120499-12345is meaning that the person was born at 04 December 1999?yearis always zero in your code! Explain by a complete example how to get the age for a string code!120499is the date. What is the significance of this date? Without a clear description of how you go from120499to an age, nobody can help you.