0

I've cut the code down into this small piece so it would be easier to read. My problem is that the value that the user writes into "injtaille" doesn't transfer over to the switch a little lower down. How can I fix this?

if (inj == "oui")
{
    Console.WriteLine("Quel est la taille de l'injection? (30 - 50 - 60) ");
    injtaille = Convert.ToInt32(Console.ReadLine());
}
switch (client)
{
    case 1:
       consprix = 25;
       imgradprix = 55;
       analyzeprix = 28;
       switch (injtaille)
       {
           case 30
       }
Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
  • 1
    The `case 30` is missing a `:` and the code to be executed. What did you expect it to do? –  Oct 09 '15 at 04:18
  • Are you getting errors did you try a default case for the switch statement – EasyBB Oct 09 '15 at 04:18
  • @Roy Yes I am aware that there is no `:` after `case 30`. The problem is that when I put my cursor on `switch (injtaille)`, it says that I'm using an unassigned value. – Dr.Roflcopter Oct 09 '15 at 04:21
  • 1
    You should accept the answer of @MickyD. You may also see [this question](http://stackoverflow.com/questions/5710485/c-sharp-use-of-unassigned-local-variable) posted in 2011, with many other answers. – Massimiliano Kraus May 30 '16 at 11:58

1 Answers1

2

The problem is that when I put my cursor on switch (injtaille), it says that I'm using an unassigned value

You need to initialise your variables before using them. Your original code (from what I can see) only sets it if the guard inj == "oui" holds true.

Try this:

int injtaille =0; // default value here

if (inj == "oui")
{
    Console.WriteLine("Quel est la taille de l'injection? (30 - 50 - 60) ");
    injtaille = Convert.ToInt32(Console.ReadLine());
}
switch (client)
{
    case 1:
       consprix = 25;
       imgradprix = 55;
       analyzeprix = 28;
       switch (injtaille)
       {
           case 30:
              ....
       }

Depending on the logic on your program, it's generally a good idea to include a default in your switch statements too.