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
       }
4
  • 1
    The case 30 is missing a : and the code to be executed. What did you expect it to do? Commented Oct 9, 2015 at 4:18
  • Are you getting errors did you try a default case for the switch statement Commented Oct 9, 2015 at 4: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. Commented Oct 9, 2015 at 4:21
  • 1
    You should accept the answer of @MickyD. You may also see this question posted in 2011, with many other answers. Commented May 30, 2016 at 11:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Alright so, the default starting value of injtaille will be 0. Let's say I write in 50 at the ReadLine line, will injtaille take on the value of whatever the user writes in the ReadLine?
@Dr.Roflcopter yes it is.
@Dr.Roflcopter Yes, it will as you can easily see by running the code
@Dr.Roflcopter You're missing a break; right at the end after the }

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.