Im currently trying to make an addressbook application that holds some person data in an object which is then added to a list. Im currently making a method to try and list the data when prompted but the information isnt showing, does anyone know how I can go about fixing this?
namespace AddressBook
{
class Student : Person
{
public String StudentID { get; set; }
public String grade { get; set; }
public List<Student> studentList = new List<Student>();
public void addStudent(string firstName, string lastName, string address, string city, string country, string postcode, string StudentID, string grade )
{
Student student = new Student();
student.firstName = firstName;
student.lastName = lastName;
student.address = address;
student.city = city;
student.country = country;
student.postcode = postcode;
student.StudentID = StudentID;
student.grade = grade;
studentList.Add(student);
}
public void listStudentData()
{
String fname ="", lname="", add="", city="", country="", post="", id="", grd = "";
String[,] index = { { "First name: ", "Last Name: ", "Address: ", "City: ", "Country: ", "Postcode: ", "StudentID: ", "Grade: " }, { fname, lname, add, city, country, post, id, grd }, };
for (int i = 0; i < studentList.Count; i++)
{
fname = (studentList[i].firstName.ToString());
lname = (studentList[i].lastName.ToString());
add = (studentList[i].address.ToString());
city = (studentList[i].city.ToString());
country = (studentList[i].country.ToString());
post = (studentList[i].postcode.ToString());
id = (studentList[i].StudentID.ToString());
grd = (studentList[i].grade.ToString());
Console.WriteLine(index[0, i] + index[1,i]);
}
Console.WriteLine("");
}
}
}
The format I want to print out is "First Name: John" "Last Name: Smith" etc
however the current format is: "First Name: "
Any advice would be much appreciated
fname,lnameetc local variables, but that doesn't change the values in the array. It's not really clear why you're approaching the formatting this way - or why you'd want to print the first name of the first student, the last name of the second student, the address of the third student etc.studentLista (non-static) member ofStudent? And why are you callingToStringonstringvariables? This all looks rather suspicious