Classes and Methods (C#)

Hi, I've been working on this code involving classes and methods. I need help with one specific area. In form1.cs, the last two lines with comments are the codes I am having issues with. I want to display the last name of the Advisor that is associated to a student. I called a method within the student class, but when I debug the application, nothing shows up (value is null).

I would appreciate any form of help!

Thanks a lot 🙂

namespace StudentAdvisor {

class Advisor { //Declare Fields private string _FName; private string _LName; private string _Department; //Constructor public Advisor() { _FName = ""; _LName = ""; _Department = ""; } //Field Properties public string FirstName { get { return _FName; } set { _FName = value; } } public string LastName { get { return _LName; } set { _LName = value; } } public string Department { get { return _Department; } set { _Department = value; } } public string getLName() { return LastName; } } 

}

namespace StudentAdvisor {

class Student { //Fields private string _FName; private string _LName; private string _Major; private double _GPA; //Constructor public Student() { _FName = ""; _LName = ""; _Major = ""; _GPA = 0; } //Field Properties public string FirstName { get { return _FName; } set { _FName = value; } } public string LastName { get { return _LName; } set { _LName = value; } } public string Major { get { return _Major; } set { _Major = value; } } public double GPA { get { return _GPA; } set { _GPA = value; } } public string getLName() { return _LName; } } 

}

namespace StudentAdvisor {

public partial class Form1 : Form { List<Advisor> AdvisorsList = new List<Advisor>(); List<Student> StudentsList = new List<Student>(); public Form1() { InitializeComponent(); } private void btnClearAdvisor_Click(object sender, EventArgs e) { txtAddAdvisorFName.Text = ""; txtAddAdvisorLName.Text = ""; txtAddAdvisorDepartment.Text = ""; } private void btnClearStudent_Click(object sender, EventArgs e) { txtAddStudentFName.Text = ""; txtAddStudentLName.Text = ""; txtAddStudentMajor.Text = ""; txtAddStudentGPA.Text = ""; } private void GetAddAdvisorData(Advisor advisor) { advisor.FirstName = txtAddAdvisorFName.Text; advisor.LastName = txtAddAdvisorLName.Text; advisor.Department = txtAddAdvisorDepartment.Text; } private void GetAddStudentData(Student student) { student.FirstName = txtAddStudentFName.Text; student.LastName = txtAddStudentLName.Text; student.Major = txtAddStudentMajor.Text; student.GPA = double.Parse(txtAddStudentGPA.Text); } private void btnAddAdvisor_Click(object sender, EventArgs e) { Advisor myAdvisor = new Advisor(); GetAddAdvisorData(myAdvisor); AdvisorsList.Add(myAdvisor); lstAdvisors.Items.Add(myAdvisor.FirstName + " " + myAdvisor.LastName); txtAddAdvisorFName.Clear(); txtAddAdvisorLName.Clear(); txtAddAdvisorDepartment.Clear(); txtAddAdvisorFName.Focus(); } private void lstAdvisors_SelectedIndexChanged(object sender, EventArgs e) { int index = lstAdvisors.SelectedIndex; txtShowAdvisorFName.Text = AdvisorsList[index].FirstName; txtShowAdvisorLName.Text = AdvisorsList[index].LastName; txtShowAdvisorDepartment.Text = AdvisorsList[index].Department; } private void btnAddStudent_Click(object sender, EventArgs e) { Student myStudent = new Student(); GetAddStudentData(myStudent); StudentsList.Add(myStudent); lstStudents.Items.Add(myStudent.FirstName + " " + myStudent.LastName); txtAddStudentFName.Clear(); txtAddStudentLName.Clear(); txtAddStudentMajor.Clear(); txtAddStudentGPA.Clear(); txtAddStudentFName.Focus(); } private void lstStudents_SelectedIndexChanged(object sender, EventArgs e) { int index = lstStudents.SelectedIndex; Student myStudent = new Student(); txtShowStudentFName.Text = StudentsList[index].FirstName; txtShowStudentLName.Text = StudentsList[index].LastName; txtShowStudentMajor.Text = StudentsList[index].Major; txtShowStudentGPA.Text = StudentsList[index].GPA.ToString("n2"); //txtShowStudentAdvisor.Text = myStudent.getLName(); // MessageBox.Show(myStudent.getLName()); } } 

}

by willyfong via /r/csharp

Leave a Reply