c# - I can't display my array that I have populated through form1Load. Will run without errors but wont run -
public partial class form1 : form { datetime[] birth = new datetime[20]; person[] people = new person[20]; public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { // create class person following fields _firstname, _lastname, _birthdate(datetime type) add constructor, properties (get only) , method getage returns age (int) of person. // in form1, create array of person objects hold 20 people // in form1_load: populate array 20 person objects // add gui display people in list (first , last names, birthdate, , age // add gui //people[0] = new person("john","stockton", datetime.) string[] first = new string[20] { "scott", "ramona", "todd", "melissa", "naomi", "leland", "conor", "julie", "armondo", "leah", "frank", "peter", "ila", "mandy", "sammy", "gareth", "garth", "wayne", "freddy", "mark" }; string[] last = new string[20] { "kennedy", "kennedy", "kennedy", "kennedy", "kennedy", "kennedy", "carrel", "maloythebeautiful", "johnson", "smith", "sinatra", "clemens", "eels", "johnson", "eels", "thompson", "brooks", "world", "crugar", "thomas" }; birth[0] = new datetime(1987, 22, 7); birth[1] = new datetime(1962, 15, 9); birth[2] = new datetime(1984, 21, 4); birth[3] = new datetime(1977, 24, 1); birth[4] = new datetime(1983, 12, 8); birth[5] = new datetime(1979, 14, 1); birth[6] = new datetime(1965, 19, 9); birth[7] = new datetime(1968, 21, 2); birth[8] = new datetime(1980, 22, 7); birth[9] = new datetime(1982, 20, 7); birth[10] = new datetime(1984, 19, 4); birth[11] = new datetime(1968, 11, 9); birth[12] = new datetime(1968, 21, 8); birth[13] = new datetime(1975, 5, 2); birth[14] = new datetime(1945, 15, 3); birth[15] = new datetime(1969, 14, 6); birth[16] = new datetime(1987, 141, 4); birth[17] = new datetime(1976, 23, 5); birth[18] = new datetime(1989, 28, 6); birth[19] = new datetime(1988, 23, 9); // populate array person[] people = new person[20]; (int = 0; < people.length; i++) { people[i]= new person(first[i], last[i], birth[i]); } } private void btndisall_click(object sender, eventargs e) { try { (int = 0; < people.length; i++) {
this need displaying array have populated through form1 load
displaymessage("name: " + people[i].firstname + people[i].lastname + " birthdate: " + people[i].birthdate.year + people[i].birthdate.day + people[i].birthdate.month + "\n\n"); //richtxtdisplay.appendtext(people[i].tostring()); //richtxtdisplay.appendtext(people[i].firstname + people[i].lastname + people[i].birthdate + "\n"); } } catch { } } private void btngetage_click(object sender, eventargs e) { int birth = int.parse(textbox1.text); } public void displaymessage(string message) { // void method messagebox.show(message); } public void displayrichtxtmessappendtext(string message) { // void method richtxtdisplay.appendtext(message); } }
and class person..
public class person { private string _firstname; private string _lastname; private datetime _birthdate; public person(string firstname, string lastname, datetime birthdate) { _firstname = firstname; _lastname = lastname; _birthdate = birthdate; } public string firstname { { return _firstname; } } public string lastname { { return _lastname; } } public datetime birthdate { { return _birthdate; } } public int getage() { timespan ts = datetime.now - _birthdate; int year = (int)ts.totaldays / 365; return year; } public int daysuntillbirthdate() { int age = getage(); datetime proximobirthday = _birthdate.addyears(age + 1); timespan ts = proximobirthday - datetime.now; return ts.days; } }
i have labeled displaying array first names, last names , birth date. don't have errors , won't show , can click button display many times , won't break code. can't remember format exemption use try catch should work enough.
your initializing date wrong
new datetime(1987, 22, 7); // month can't 22!
format should year, month , day.
this not work, correct that. issue.
here constructor signature of datetime
public datetime(int year, int month, int day);
Comments
Post a Comment