-2

so, i created this piece of code which is supposed to represent a person/student at a school.

my goal behind this code is to allow me to create a new student without having to manually make a new inherited class for every new student i want to add to my system. The problem is that i dont have the slightest idea of where to begin. Can anybody give me a lead that can help me?

    class student
{
    //alle properties van de student
    public string studentnummer;
    public string naam;
    public string postcode;
    public string telefoonnummer;
    public List<string> vakken;

    //een no-argument constructor
    public student()
    {
        //informatie over de studen
        studentnummer = "S101010";
        naam = "Voornaam Achternaam";
        postcode = "1111AT";
        telefoonnummer = "31+ 0571 123456";

        vakken = new List<string>();
        vakken.Add("Voorbeeld vak 1");
        vakken.Add("Voorbeeld vak 2");  

    }
    //override van de ToString methode? (ik weet niet of dit ook de goede manier is)
    public override string ToString()
    {
        return $"Studentnummer: {studentnummer}, naam: {naam}, postcode: {postcode}, telefoonnummer :{telefoonnummer}, aantal vakken: {String.Join(", ", vakken)}";
    }

}

    class Program
{
    static void Main(string[] args)
    {
        student Student = new student();
        Console.WriteLine(Student.ToString());

        Console.WriteLine(" ");
        Console.WriteLine(" ");
    }

}

I am still in the early stages of learning to code and dont know of any way my goal can be archieved.

EDIT: not only do i have no clue where to begin, I also dont even know how to do it.

Venepskeuten
  • 69
  • 1
  • 12
  • 2
    First, you're getting naming conventions backwards. The *class* should be capitalized (`Student`) and the *local variable* should be lowercase (`student`). Keeping to conventions will allow you to more easily read and understand your code. Aside from that... *"i dont have the slightest idea of where to begin"* - Begin at the beginning. Do you want to be able to supply values when creating a Student? Add parameters to the constructor. Do you want the user to input those values? Read input from the console. Etc. Etc. Some introductory C# tutorials are a good place to start here. – David Nov 19 '19 at 12:46
  • 1
    student information should be dynamic from constructor – Syed Wahhab Nov 19 '19 at 12:52

3 Answers3

4

You probably just want to setup the student class for every student created.

First, some theory:

  • Class is just abstract thing, which just describes what it is. In your example, the student says that the student have some name, and other data.
  • To give the class "life" you need to instantiate it and create object. Thats what you do by following code:

        student Student = new student();
    

This line tells computer to create new student. But in this place you probably want to says: "I wanna create student Voornaam Achternaam". So you need another piece of code, which do this. For this, you need so called Constructor. You already have one, but here you have misunderstood its usage. In empty constructor you should initialize the data of the class into some default values, if necessary. But then you can specify parameter of the constructor. Example:

public student(string number, string name, ..........)
{
    //informatie over de studen
    studentnummer = number;
    naam = name;
    ......................
}

You can use then this constructor by this instantiation:

student Student = new student("S101010", "Voornaam Achternaam", ......);

I use here just two arguments for simplicity, but I think you understand the principle.

BTW: I would create also the empty constructor, but its code would be much more simple:

public student()
{
    vakken = new List<string>();
}

Explanation: all other data are string, which have their default value (null) automatically. It is OK, valid value, because usually you use it after it is assigned. vakken is List, which default value is also null. It is also OK, but here you may be in temptation to use it before initialization. Example:

naam = "Name"; // This is OK, as assigning some string to field with `null` value is OK.
vakken.Add("Voorbeeld vak 1"); // this will throw NullException

The reason, why second line throws exception is because you want to call the method on the object which doesn't exist. As this happens very often that you forget, that the List was not initialized, it is good habit to initialize it in constructors (not necessarily, but in your beginning, do not forget to initialize any data in the class which are also classes).

Zoka
  • 2,312
  • 4
  • 23
  • 33
  • 1
    Your explanation about the empty constructor could be confusing, since you don't explain why string initializing with null is good, and why vakken initializing with null is bad. – Rand Random Nov 19 '19 at 13:07
  • @RandRandom, I have add some brief explanation on the initialization. – Zoka Nov 19 '19 at 13:15
3

What about this:

class Program
{
    static void Main(string[] args)
    {
        Student student = new Student
        {
            //informatie over de studen
            Studentnummer = "S101010",
            Naam = "Voornaam Achternaam",
            Postcode = "1111AT",
            Telefoonnummer = "31+ 0571 123456"
        };
        student.Wakken.Add("Voorbeeld vak 1");
        student.Wakken.Add("Voorbeeld vak 2");

        Console.WriteLine(student.ToString());
    }
}

class Student
{
    //alle properties van de student
    public string Studentnummer { get; set; }
    public string Naam { get; set; }
    public string Postcode { get; set; }
    public string Telefoonnummer { get; set; }
    public List<string> Wakken { get; } = new List<string>();

    //een no-argument constructor
    public Student()
    { }
    //override van de ToString methode? (ik weet niet of dit ook de goede manier is)
    public override string ToString()
    {
        return $"Studentnummer: {Studentnummer}, naam: {Naam}, postcode: {Postcode}, telefoonnummer :{Telefoonnummer}, aantal vakken: {String.Join(", ", Wakken)}";
    }

}
Ive
  • 1,321
  • 2
  • 17
  • 25
2

Here there is a slightly different version showing you how to use a Static Factory Method.

If you want to learn more on this you can read the following

What are static factory methods?

public class Student
{
    public string StudentNummer { get; set; }
    public string Naam { get; set; }
    public string PostCode { get; set; }
    public string TelefoonNummer { get; set; }
    public List<string> Vakken { get; set; }


    public static Student Create(string studentNummer, string naam, string postCode, string telefoonNummer, List<string> vakken)
    {
        return new Student(studentNummer, naam, postCode, telefoonNummer, vakken);
    }

    private Student(string studentNummer, string naam, string postCode, string telefoonNummer, List<string> vakken)
    {
        StudentNummer = studentNummer;
        Naam = naam;
        PostCode = postCode;
        TelefoonNummer = telefoonNummer;
        Vakken = vakken;
    }

    private Student()
    {
        Vakken = new List<string>();
    }

    public override string ToString()
    {
        return $"Studentnummer: {StudentNummer}, naam: {Naam}, postcode: {PostCode}, telefoonnummer :{TelefoonNummer}, aantal vakken: {String.Join(", ", Vakken)}";
    }
}

class Program
{
    static void Main(string[] args)
    {
        var student = Student.Create("S101010", "Voornaam Achternaam", "1111AT", "31+ 0571 123456", 
            new List<string>()
            {
                "Voorbeeld vak 1", "Voorbeeld vak 2"
            });
        Console.WriteLine(student.ToString());
    }
}
Lorenzo
  • 91
  • 5