WriteLine with a class
I'm making a SchoolApp program to learn about C# and I have this main function that I'm trying to make work:
namespace SchoolApp
{
class Program
{
public static void Main(string[] args)
{
School sch = new School("Local School");
sch.AddStudent(new Student { Name = "James", Age = 22 }); // this function
sch.AddStudent(new Student { Name = "Jane", Age = 23 });
Console.WriteLine(sch); // this implementation
}
}
}
School class:
namespace SchoolApp
{
class School
{
public string name;
public School()
{
}
public School(string the_name)
{
name = the_name;
}
public void AddStudent(Student student)
{
}
}
}
Student class: namespace SchoolApp
{
class Student
{
public int Age;
public string Name;
public Student()
{
Age = 0;
Name = "NONAME";
}
public string _name
{
get
{
return this.Name;
}
set
{
Name = value;
}
}
public int _age
{
get
{
return this.Age;
}
set
{
Age = value;
}
}
}
}
I'm new to C# and I am pretty lost as to how I should make the AddStudent and how to work with the WriteLine to write the class. The output should be along these lines:
School: Local School James, 22 Jane, 23
Someone suggested using List<> I hit the same problem of not knowing enough about it.
EDIT: Thank you all for your extremely fast answers. I got it working and will now commense reading the answers in depth so I don't have to ask related questions again!
Console.WriteLine(t)
perform implicit conversion of t to string. So you need to override ToString()
for all your classes or create string from your class explicitly.
Example:
class School
{
public string name;
List<Student> Students = new List<Student>();
...
public override string ToString()
{
var stringValue = name;
foreach(var student in Students)
{
stringValue += student.ToString();
}
return stringValue;
}
}
class Student
{
public int Age;
public string Name;
...
public override string ToString()
{
return string.Format("{0} {1}", Name, Age);
}
}
In your School
class it looks like you want to have a private list of Students. List is simple, it takes in a generic type, in your case Student
and you can add and remove items to/from it.
In your School
class use:
private List<Student> students;
And in your AddStudent code you could simply use:
students.Add(student); // student is the variable being passed in and students is the list. NOTE: You will have to make sure students is intalized to a new List of Student. students = new List<Student>();
Take a look at the following links. MSDN is the proper documentation, but the dotnetperls is good for simple stuff like this.
http://msdn.microsoft.com/en-gb/library/3wcytfd1.aspx
http://www.dotnetperls.com/list-add
class School
{
public string name;
private List<Student> students = new List<Student>();
public School()
{
}
public School(string the_name)
{
name = the_name;
}
public void AddStudent(Student student)
{
students.Add(student);
}
public override string ToString()
{
var sb = new StringBuilder();
sb.AppendLine(name);
foreach (var student in students)
{
sb.AppendLine(student.ToString());
}
return sb.ToString();
}
}
class Student
{
...
public override string ToString()
{
return "Student : " + Name + ", " + Age;
}
}
Someone suggested using
List<>
I hit the same problem of not knowing enough about it.
Yes, the List<T>
class is appropriate for this situation. It has an .Add method which allows you to add objects to it. And then you could loop through the elements of this list (using a foreach
statement) and then print each element details to the console using Console.WriteLine method.