简体   繁体   中英

Adding an enum to a list of a specific class c#

I am trying to add an value of an enum into a list of a specific class type.

This is my class:

class Vak
{
    public string naamVanVak;
    public int theorieCijfer;
    public enum PraktijkBeoordeling
    {
        Geen, Absent, Onvoldoende, Voldoende, Goed
    }
}

This is my list where i need to add my enumeration into:

            List<Vak> rapport = new List<Vak>();

Here is where the enum gets decided:

Vak.PraktijkBeoordeling LeesPraktijkBeoordeling(string vraag)
    {
        while (true)
        {
            Console.Write(vraag);
            string invoer = Console.ReadLine();

            Vak.PraktijkBeoordeling praktijkBeoordeling = new Vak.PraktijkBeoordeling();

            if (invoer.ToLower() == "geen")
            {
                praktijkBeoordeling = Vak.PraktijkBeoordeling.Geen;
            }
            else if (invoer.ToLower() == "absent")
            {
                praktijkBeoordeling  = Vak.PraktijkBeoordeling.Absent;
            }
            else if (invoer.ToLower() == "onvoldoende")
            {
                praktijkBeoordeling = Vak.PraktijkBeoordeling.Onvoldoende;
            }
            else if (invoer.ToLower() == "voldoende")
            {
                praktijkBeoordeling = Vak.PraktijkBeoordeling.Voldoende;
            }
            else if (invoer.ToLower() == "goed")
            {
                praktijkBeoordeling =  Vak.PraktijkBeoordeling.Goed;
            }
            return praktijkBeoordeling;

        }

    }

Currently what im trying to do is:

void LeesRapport(List<Vak> rapport, string vraag, string vraag2, string vraag3)
    {
        for (int i = 0; i < 3; i++)
        {
            Vak vak = LeesVak(vraag);
            rapport.Add(vak);

            Vak theorieCijfer = LeesCijfer(vraag2);
            rapport.Add(theorieCijfer);

            Vak praktijk = LeesPraktijkBeoordeling(vraag3);
            Console.WriteLine("Geen, Absent, Onvoldoende, Voldoende, Goed");
            rapport.Add(praktijk);
        }
    }

Obviously this isnt working but how can i make this work?

thanks in advance

You cant add them like this. Follow this example

Generate Class with Enum getter and setter that you missed:

public class Vak
{
    public string naamVanVak { get; set; }; 
    public int theorieCijfer { get; set; };
    public PraktijkBeoordeling enumPraktijkBeoordeling { get; set; }; //you need to add this one

    public enum PraktijkBeoordeling
    {
        Geen, Absent, Onvoldoende, Voldoende, Goed
    }
}

then out of your main add a list

List<Vak> rapport = new List<Vak>();

populate the list:

private void LeesRapport(List<Vak> rapport, string vraag, string vraag2, string vraag3)
    {
        for (int i = 0; i < 3; i++)
        {
            Vak vak = new Vak();

            vak.naamVanVak = "data";
            vak.theorieCijfer = 1111;
            vak.enumPraktijkBeoordeling = LeesPraktijkBeoordeling(vraag);
            rapport.Add(vak);


        }
    }

It seems you are trying to add a Vak.PraktijkBeoordeling to the list, while it is a list of Vak . Maybe you want to return a Vak , which has a PraktijkBeoordeling from LeesPraktijkBeoordeling(string vraag) ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM