Create a string based on a case selection in C#

Viewed 41

I am brand new to coding and I am re-creating the start of a Pokemon game in order to practice. I am using the 'while' with cases and I currently have this (please note I have created a method for TypeLine to print the text but a character at a time like dialogue rather than the full sentence all at once. This code is working so ignore that and imagine it as Console.WriteLine);

//Player makes their Pokemon choice.
            bool pokemonChoice = true;

            while (pokemonChoice)
            {
                string input = Console.ReadLine();
                int key = Convert.ToInt32(input);

                switch (key)
                {
                    case 1:
                        TypeLine("\nCharmander! What an excellent decision! \n");
                        TypeLine("CONGRATULATIONS! YOU JUST OBTAINED CHARMANDER! \n");
                        pokemonChoice = false;
                        break;
                       

                    case 2:
                        TypeLine("\nSquirtle! What an excellent decision! \n");
                        TypeLine("CONGRATULATIONS! YOU JUST OBTAINED SQUIRTLE! \n");
                        pokemonChoice = false;
                        break;

                    case 3:
                        TypeLine("\nBulbasaur! What an excellent decision! \n");
                        TypeLine("CONGRATULATIONS! YOU JUST OBTAINED BULBASAUR! \n");
                        pokemonChoice = false;
                        break;

What I want to do, is if say case 1 is selected by the user inputting "1", then a new string is created called "pokemon" and is assigned "Charmander".

string pokemon = ("Charmander");

The purpose is so that later, instead of having to type the Pokemon name, I could use;

Console.WriteLine(pokemon + " I choose you!");

I attempted to do so in the case itself, like this;

                switch (key)
                {
                    case 1:
                        string pokemon = ("Charmander");
                        TypeLine("\nCharmander! What an excellent decision! \n");
                        TypeLine("CONGRATULATIONS! YOU JUST OBTAINED CHARMANDER! \n");
                        pokemonChoice = false;
                        break;

But when I then try to use it outside of the 'while', I get the error "The name 'pokemon' does not exist in the current context"

How can I achieve the result I have detailed?

Thanks in advance!

1 Answers

You can't get access to your pokemon string because it is declared inside your switch loop, and cant be accessed anywhere outside of it. Basically, it lives and dies inside that scope.

So, create that same variable in the start of your program, or outside of your function, and you will be able to use it whenever you wish.

Maybe consider some time reading about this ocassion.

Here is the actual code, note that string pokemon = "" is used like a workaround for an Use of unassigned local variable 'pokemon' error. Simply put any value at start. (For int variables we usually do int smth = 0)

bool pokemonChoice = true;
        
        string pokemon = ""; 
        while (pokemonChoice)
        {
            string input = Console.ReadLine();
            int key = Convert.ToInt32(input);
            switch (key)
            {
                case 1:
                    Console.WriteLine("\nCharmander! What an excellent decision! \n");
                    Console.WriteLine("CONGRATULATIONS! YOU JUST OBTAINED CHARMANDER! \n");
                    pokemon = "Charmander"; //assign the value if the case 1 is picked
                    pokemonChoice = false;
                    break;
                   

                case 2:
                    Console.WriteLine("\nSquirtle! What an excellent decision! \n");
                    Console.WriteLine("CONGRATULATIONS! YOU JUST OBTAINED SQUIRTLE! \n");
                    pokemon = "Squirtle"; //assign the value if the case 2 is picked
                    pokemonChoice = false;
                    break;

                case 3:
                    Console.WriteLine("\nBulbasaur! What an excellent decision! \n");
                    Console.WriteLine("CONGRATULATIONS! YOU JUST OBTAINED BULBASAUR! \n");
                    pokemon = "Bulbasaur"; //assign the value if the case 3 is picked
                    pokemonChoice = false;
                    break;
            }
        }
        Console.WriteLine("The chosen pokemon is - " + pokemon);
Related