Hack Forums
[C#] Switch Case issue. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Switch Case issue. (/showthread.php?tid=5061405)



Switch Case issue. - MrJamieMcC - Nov 17, 2015

So this is the code

Code
int caseSwitch = 1;
            switch (caseSwitch)
            {
                case 1:
                    Console.WriteLine("1) Book Tickets");
                    break;

                case 2:
                    Console.WriteLine("2) View Timetable");
                    break;

                case 3:
                    Console.WriteLine("3) Exit");
                    break;

            }

I want all cases to display and then the user choses one. But currently, depending on the "int caseSwitch" number thats the only switch that is displayed.

Thanks for the help :)

EDIT: This is what i mean Spoiler (Click to View)

I want all of the options to display not just the first


* - +Haze - Nov 17, 2015

Can you re-phrase what you said. It doesn't make any sense.


* - Odus - Nov 17, 2015

Move your WriteLine statements above the entire switch block, so they'll be printed regardless. Remember sequencing and selection.


* - BahNahNah - Nov 17, 2015

You cant do that with a switch statement.
You could do something like this.

Code
Dictionary<string, Tuple<string, Action>> options = new Dictionary<string, Tuple<string, Action>>
{
    {"1", new Tuple<string, Action>("Book Tickets", () => {})},
    {"2", new Tuple<string, Action>("View Timetable", () => {})}
};

foreach(var o in options)
    Console.WriteLine("{0}) {1}", o.Key, o.Value.Item1);

string i = Console.ReadLine();
if(options.ContainsKey(i))
    options[i].Item2.Invoke();



* - Mouse❤️ - Nov 17, 2015

(Nov 17, 2015 01:53 PM)BahNahNah Wrote:  You cant do that with a switch statement.
You could do something like this.

Code
Dictionary&lt;string, Tuple&lt;string, Action&gt;&gt; options = new Dictionary&lt;string, Tuple&lt;string, Action&gt;&gt;
{
    {"1", new Tuple&lt;string, Action&gt;("Book Tickets", () =&gt; {})},
    {"2", new Tuple&lt;string, Action&gt;("View Timetable", () =&gt; {})}
};

foreach(var o in options)
    Console.WriteLine("{0}) {1}", o.Key, o.Value.Item1);

string i = Console.ReadLine();
if(options.ContainsKey(i))
    options[i].Item2.Invoke();
Overly complicated way of doing exactly what Odus said, which is the answer to what the OP actually needs. Just move the output ...


* - MrJamieMcC - Nov 17, 2015

Ive updated the post guys


* - Mouse❤️ - Nov 17, 2015

(Nov 17, 2015 01:59 PM)MrJamieMcC Wrote:  Ive updated the post guys
Broken image.


* - BahNahNah - Nov 17, 2015

(Nov 17, 2015 01:59 PM)MrJamieMcC Wrote:  Ive updated the post guys

Why are you even using a switch statement then 0_o.


* - Mouse❤️ - Nov 17, 2015

The reason he's using a switch statement is because he wants a person to be able to enter a value, and then the program react, which is correct, but he for some reason can't read the response that this person posted:
(Nov 17, 2015 01:53 PM)Odus Wrote:  Move your WriteLine statements above the entire switch block, so they'll be printed regardless. Remember sequencing and selection.
The answer's right there.


* - MrJamieMcC - Nov 17, 2015

(Nov 17, 2015 02:03 PM)Μouse Wrote:  The reason he's using a switch statement is because he wants a person to be able to enter a value, and then the program react, which is correct, but he for some reason can't read the response that this person posted:
The answer's right there.

You mean like this? Spoiler (Click to View)

I still got that issue. I apologise for being idiotic, ive just started learning


* - BahNahNah - Nov 17, 2015

(Nov 17, 2015 02:07 PM)MrJamieMcC Wrote:  You mean like this? Spoiler (Click to View)

I still got that issue. I apologise for being idiotic, ive just started learning

Remove the conosole writelines from the switch statement.


* - Odus - Nov 17, 2015

(Nov 17, 2015 02:07 PM)MrJamieMcC Wrote:  You mean like this? Spoiler (Click to View)

I still got that issue. I apologise for being idiotic, ive just started learning

Almost. You can remove them from the cases. Mouse saved me the hard work.

P.S You can just call WriteLine without any args to terminate the line. Or append "\n" to your string literal.


* - MrJamieMcC - Nov 17, 2015

Thank you all very much. I got it to work :)