0

oh boy, here I go again.

I am currently working on a small C# project of mine to increase my knowledge and skill in coding.

I currently have a student class that holds a students personal info named 'student' and a class (like a class full of students) class named 'klas'. Inside of 'klas' I put a list with students(objects) in it. Further more I made a list with several 'klasses'(objects) although i do not know wether its a good idea.

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> vakken { get; set; } = new List<string>();

    //een no-argument constructor
    public Student(string studentnummer1, string naam1, string postcode1, string telefoonnummer1)
    {
        studentnummer = studentnummer1;
        naam = naam1;
        postcode = postcode1;
        telefoonnummer = telefoonnummer1;
    }
 }

and heres the 'klas' class.

 class Klas
{
    public string klascode { get; set; }
    public string klasnaam { get; set; }

    public Klas(string klasnummer1, string klasnaam1)
    {
        klascode = klasnummer1; 
        klasnaam = klasnaam1;    
    }

    //maakt nieuwe studenten aan in een list (experimenteel)-(niet final)
    //problemen die ik hier mee heb: hoe ga ik individuele studenten aan een lijst toevoegen?, 
    public void studentenlijst()
    {
        var studentlijst = new List<Student>()
        {
            new Student("S101010", "Voornaam - Achternaam", "1234AT", "+06 0571 261442"),                                               //student A
            new Student("S202020", "Voornaamer - Achternaamer", "2345AT", "+06 0571 372553"),                                           //student B
            new Student("S303030", "Voornaamste - Achternaamste", "3456AT", "+06 0571 483664"),                                         //student C
            new Student("S404040", "Voorgenaamste - Achtergenaamste", "4567AT", "+06 0571 594775"),                                     //student D
            new Student("S505050", "Voorgenaamst've - Achtergenaamste've", "5678AT", "+06 0571 605886"),                                //student E
            new Student("S606060", "Voorgenaamste'ntve - Achtergenaamste'ntve", "6789AT", "+06 0571 716997"),                           //student F
            new Student("S707070", "Voorgenaamste'ntve'se - Achtergenaamste'ntve'se", "7890AT", "+06 0571 827008"),                     //student G
            new Student("S808080", "Voorgenaamste'ntve'se'lu - Achtergenaamste'ntve'se'lu", "8901AT", "+06 0571 938119"),               //student H
            new Student("S909090", "Voorgenaamste'ntve'se'lu'yiant - Achtergenaamste'ntve'se'lu'yiant", "9012AT", "+06 0571 049220"),   //student I
            new Student("S010101", "You thought it was a normal name - BUT IT WAS ME, DIO!", "0123AT", "+06 0571 150331")               //student JOJO!
        };
    }

    public void klassenlijst()
    {
        //maak meerdere lists(klassen) en zet daar je studenten in.
        var klassenlijst = new List<Klas>
        {
            new Klas("KL0001", "AO1-A"),    //klas 1
            new Klas("KL0002", "AO1-B"),    //klas 2
            new Klas("KL0003", "AO2-A"),    //klas 3
            new Klas("KL0004", "AO2-B"),    //klas 4
            new Klas("KL0005", "AO3-A"),    //klas 5
            new Klas("KL0006", "GD1-B"),    //Klas 6
            new Klas("KL0325", "STM1-D"),   //klas 7
        };
    }

what I want to do is: I want to be able to place certain students into certain klassen and call/print those klassen using a method.

I just dont know where to start, let alone how i can do this and if its even possible.

Does anybody want to give me some advice and/or suggest an altarnative method to get this done?

2
  • 2
    Your class probably also has a list of students as member, which you fill in studentenlijst. However it makes no sense why a single Klas-instance should have a KlasList-method. You probably need a further School-class, that has a list of Klas. Commented Nov 26, 2019 at 10:40
  • i'll try it out, thanks for the advice. Commented Nov 26, 2019 at 10:57

2 Answers 2

2

You can try something like below.

  class Program
{
    static List<Klas> klasList = new List<Klas>();
    static void Main(string[] args)
    {
        klasList.Add(new Klas("KL0001", "AO1-A", new List<Student>()
                                                  {
                                                    new Student("S101010", "Voornaam - Achternaam", "1234AT", "+06 0571 261442"),       //student A
                                                    new Student("S202020", "Voornaamer - Achternaamer", "2345AT", "+06 0571 372553"),    //student B
                                                    new Student("S303030", "Voornaamste - Achternaamste", "3456AT", "+06 0571 483664")
                                                  }
        ));

        klasList.Add(new Klas("KL0002", "AO1-B", new List<Student>()
                                                 {
                                                    new Student("S707070", "Voorgenaamste'ntve'se - Achtergenaamste'ntve'se", "7890AT", "+06 0571 827008"),                     //student G
                                                    new Student("S808080", "Voorgenaamste'ntve'se'lu - Achtergenaamste'ntve'se'lu", "8901AT", "+06 0571 938119"),               //student H
                                                    new Student("S909090", "Voorgenaamste'ntve'se'lu'yiant - Achtergenaamste'ntve'se'lu'yiant", "9012AT", "+06 0571 049220"),   //student I
                                                    new Student("S010101", "You thought it was a normal name - BUT IT WAS ME, DIO!", "0123AT", "+06 0571 150331")               //student JOJO!
                                                 }
       ));

        foreach (var klas in klasList)
        {

            Console.WriteLine(string.Format("Klasnaam: {0} - Klascode: {1}", klas.Klasnaam, klas.Klascode));
            foreach (var student in klas.Students)
            {
                Console.WriteLine(string.Format("Studentnummer: {0} - Naam: {1}", student.Studentnummer, student.Naam));
            }
        }
        Console.ReadKey();
    }
}   

Modify Klas class as below:

 class Klas
{
    public string Klascode { get; set; }
    public string Klasnaam { get; set; }
    public List<Student> Students { get; set; }
    public Klas(string klasnummer, string klasnaam, List<Student> students)
    {
        Klascode = klasnummer;
        Klasnaam = klasnaam;
        Students = students;
    }
}     

Here is the Student class:

    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; }


    //een no-argument constructor
    public Student(string studentnummer, string naam, string postcode, string telefoonnummer)
    {
        Studentnummer = studentnummer;
        Naam = naam;
        Postcode = postcode;
        Telefoonnummer = telefoonnummer;
    }
}    
Sign up to request clarification or add additional context in comments.

Comments

0

@Fahad's answer is perfectly valid, but I think it lacks some explanation. I will take his answer and explain a little bit more about his changes. You may or may not need that, think of it as me trying to explain the answer to myself. I had a hard time grasping some of these concepts when I first started. I did not actually run all this code, there might be quirks but it is about the idea.

class Klas
{
    // these properties are pretty self-explanatory
    public string Klascode { get; set; }
    public string Klasnaam { get; set; }
    // a Klas has a List of Students
    // I changed List to IList (i.e., an interface rather than a class)
    // for this example it doesn't matter.
    // however, when you get to the point of creating your own interfaces
    // there *can* be a benefit to it (not going to explain now).
    public IList<Student> Students { get; set; }

    // A constructor allows you to set the values of an object at the time of creation.
    // In fact, in this case, you *have* to supply all 3 properties
    // the take-away here is that this is not required.
    public Klas(string klasnummer, string klasnaam, List<Student> students)
    {
        Klascode = klasnummer;
        Klasnaam = klasnaam;
        Students = students;
    }
}

// let's show some stuff
public static void Main(string[] args)
{
    // You already kow how to create students and how to assign them to a list immediately
    IList<Student> studentlijst = new List<Student>()
    {
        new Student("S101010", "Voornaam - Achternaam", "0000xx", "+06 012345678"),
        new Student("S202020", "Voornaamer - Achternaamer", "0000xx", "+06 1234 5678")
    };
    // let's create a single Klas
    Klas KlasA = new Klas("A0", "Eerste klas", studentenlijst);
    // that gives you a KlasA of type Klas, with all info
    // let's list the students:
    foreach (var student in KlasA.Students)
    {
        Console.WriteLine(string.Format("Studentnummer: {0} - Naam: {1}", student.Studentnummer, student.Naam));
    }

    // But wait! You want to add another student.
    // In this case you could simply do:
    KlasA.Students.Add(new Student("0123", "zwarte", "piet", "+34 123 456 789"));
}

Now, let us assume that at the time you are creating your Klas objects, you do not know or care beforehand what name or code or student it has.

Imagine you simplified Klas to this (or overloaded it, another topic):

class Klas
{
    public string Klascode { get; set; }
    public string Klasnaam { get; set; }
    public IList<Student> Students { get; set; }

    // just declare properties, no constructor! A true POCO class

    // just a heads up, but not important for this example:
    // you can have more than one constructor (it's called overloading)
}

Let's see that in action:

public static void Main(string[] args)
{
    // let's create a single Klas
    Klas klasA = new Klas();
    klasA.Klascode = "B0";
    klasA.Klasnaam = "lekker boeie";
    // not that we did not set the Students property

    // let's list the students:
    foreach (var student in klasA.Students)
    {
        // Oops. This crashes. Will throw a Null reference exception
    }
}

The Klas class still has a perfectly fine Students property. But we never set it. So it is still null. This will trip you up in the future, I can almost guarantee you. And the first time you encounter this you will be like 'what the hell happened?'. At some time in the future you will write classes that do not populate all their properties in a constructor. They will have their default values. String will be empty, int will be 0 and objects (like List) will be null. Then you will likely want to explore the concept of 'backing fields' and mysterious terms like that, where applicable.

I like to think of objects (classes, types), the lingo matters little when you are a beginner -you'll get to it-, as lego blocks that you make up because you think they make sense. You can simply throw them in with all the lego blocks that .Net already contains.

Okay, this really was more a talk by me to me, but I hope it provided some additional info.

Enjoy coding!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.