0

I want the function PrintMaanden() to print every month with its string, e.g. "1 => January" Instead the function prints just the month itself, e.g. "January".

What did I do wrong here? Note that for this code I have to use the other function (that does work correctly as it is) PrintMaand() in the function PrintMaanden().

static void Main(string[] args)
{
    Program kalenderMaand = new Program();
    kalenderMaand.Start();
}

void Start()
{
    //New object month of type Month (class Month)
    Maanden maand;
    maand = new Maanden();
    PrintMaanden();
    PrintMaand(maand);

    //Read number of the month
    Console.Write("Typ het nummer van de maand: ");
    string month = Console.ReadLine();
    Maanden monthNumber = (Maanden)int.Parse(month);

    //Display the month based on the number that was typed
    switch (monthNumber)
    {
        case Maanden.Januari:
            Console.WriteLine("1 => Januari");
            break;
        case Maanden.Februari:
            Console.WriteLine("2 => Februari");
            break;
        case Maanden.Maart:
            Console.WriteLine("3 => Maart");
            break;
        case Maanden.April:
            Console.WriteLine("4 => April");
            break;
        case Maanden.Mei:
            Console.WriteLine("5 => Mei");
            break;
        case Maanden.Juni:
            Console.WriteLine("6 => Juni");
            break;
        case Maanden.Juli:
            Console.WriteLine("7 => Juli");
            break;
        case Maanden.Augustus:
            Console.WriteLine("8 => Augustus");
            break;
        case Maanden.September:
            Console.WriteLine("9 => September");
            break;
        case Maanden.Oktober:
            Console.WriteLine("10 => Oktober");
            break;
        case Maanden.November:
            Console.WriteLine("11 => November");
            break;
        case Maanden.December:
            Console.WriteLine("12 => December");
            break;
    }
    Console.ReadKey();
}


void PrintMaand(Maanden maand)
{
    Console.WriteLine(maand);
}

void PrintMaanden()
{
    for (int i = 1; i < 13; i++)
    {
        PrintMaand((Maanden) i);
    }
}
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • I have one other class called Maanden.cs , which is an enum file. It simply contains this: public enum Maanden { Januari = 1, Februari, Maart, April, Mei, Juni, Juli, Augustus, September, Oktober, November, December } } – Leroy de Smet Nov 19 '19 at 12:43
  • Maybe this is what your looking for? https://stackoverflow.com/a/899589/758848 – Steve Nov 19 '19 at 12:51
  • No. It isn't. I just need to know why it prints the month itself rather than the string I've defined in the switch. – Leroy de Smet Nov 19 '19 at 12:53
  • 1
    Because your switch is not beeing called at PrintMaanden but after. The method PrintMaanden is just printing the WriteLine(maand). – Mr.Deer Nov 19 '19 at 13:00
  • 1
    I think you should move the whole switch case to the function PrintMaand(), and call PrintMaand() from start – Peter Bruins Nov 19 '19 at 13:06

4 Answers4

1

The logic to pring both the month index/number as well as the name is not in the PrintMaanden function. It is in the Start function.

Victor Ortuondo
  • 406
  • 3
  • 6
1

Maybe that what youre looking for:

void PrintMaand(Maanden maand)
{
    Console.Write(maand + "\n");
}

void PrintMaanden()
{
    for (int i = 1; i < 13; i++)
    {
        Console.Write($"{i} => ");
        PrintMaand((Maanden) i);

    }
}

I used Write instead of WriteLine because this way you can write on the same line using your PrintMaand() method. And inside the PrintMaand() you have the \n for next line. The PrintMaanden formats the extra info you need for your new print.

MX D
  • 2,453
  • 4
  • 35
  • 47
Mr.Deer
  • 476
  • 6
  • 17
1

This may be what you are looking for:

void PrintMaand(Maanden maand)
{
    Console.WriteLine((int) maand + " => " + maand);
}

void PrintMaanden()
{
    for (int i = 1; i < 13; i++)
    {
        PrintMaand((Maanden) i);
    }
}

It's just of matter of formatting the result.

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
0
    void Start()
    {
        //New object month of type Month (class Month)
        Maanden maand;
        maand = new Maanden();

        //Calling the methods
        PrintMaanden();
        VraagMaand("Typ het nummer van de maand: ");


        Console.ReadKey();
    }


    void PrintMaand(Maanden maand)
    {
        Console.WriteLine(maand);
    }

    void PrintMaanden()
    {
        int teller = 0;
        for (Maanden i = Maanden.Januari; i <= Maanden.December; i++)
        {
            teller = teller + 1;
            Console.WriteLine(string.Format("{0,2}. {1}", teller, i));
        }
    }

    Maanden VraagMaand(string question)
    {
        //Read number of the month
        Console.Write(question);
        string month = Console.ReadLine();
        Maanden monthNumber = (Maanden)int.Parse(month);

        Console.Write("{0} => ", month);
        PrintMaand(monthNumber);
        return monthNumber;
    }
}

}

I figured it out, this was what I was looking for.