Home > Net >  How to avoid error CS0128 (A local variable is already defined in this scope) when using switch?
How to avoid error CS0128 (A local variable is already defined in this scope) when using switch?

Time:04-24

I'm learning c# so sorry for the stupid question. Here is my code:

                Random prekey = new Random();

            string[] firstname = {"Александр", "Михаил", "Иван", "Максим", "Артем", "Даниил", "Дмитрий", "Кирилл", "Андрей", "Егор"};
            string[] secondname = { "Александов", "Михайлов", "Иванов", "Максимов", "Артемов", "Даниилов", "Дмитров", "Кириллов", "Андреев", "Егоров" };
            string fullname = firstname[prekey.Next(0, 10)]   " "   secondname[prekey.Next(0, 10)];
            int a = prekey.Next(0, 3);
            //string introduce;
            switch (a)
            {
                case 0:
                    string introduce = firstname[prekey.Next(0, 10)];
                        break;
                case 1:
                    string introduce = secondname[prekey.Next(0, 10)];
                        break;
                case 2:
                    string introduce = fullname;
                    break;
            }

As you can see, my plan is to have the "introduce" randomly be either "firstname" or "secondname" or both. But I am facing the error "A local variable named 'introduce' is already defined in this scope". I searched and found this question: C# : error using if/switch : "Local variable already defined in this scope" but the answers didn't help me. For example, I tried to declare a variable before the switch, but nothing changed. So I just removed that line as a comment. One day I will try to use "if", but now I want to use switch. I posted my calculator here before, here it is again:

    static void Main(string[] args)
{
    void MyMet(double? Result = null)
    {
        string[] calc = Console.ReadLine().Split(' ');

        double a = Result ?? double.Parse(calc[0]);
        char b = char.Parse(calc[1]);
        double c = double.Parse(calc[2]);

        switch (b)
        {
            case '*':
                Result = a * c;
                break;
            case ' ':
                Result = a   c;
                break;
            case '-':
                Result = a - c;
                break;
            case '/':
                Result = a / c;
                break;
        }
        Console.Write(Result);
        MyMet(Result);
    }
    MyMet();
}

In this case, everything works, but here the variable is declared with question marks (to be honest, I have no idea how they work, this part was just copied) and I tried to recreate it exactly in the current case, but apparently C# does not wants to treat string as double, so he just wrote "Nullable reference types are not available in C# 7.3. Please use language version 8.0 or higher." Can I solve this problem by continuing to use the switch and not switching to another version?

CodePudding user response:

You need to declare the variable only once, before it is used.

When you assign the variable (as opposed to declaring it) you do not specify the type to the left of the variable name.

string introduce;
switch (a)
{
    case 0:
        introduce = firstname[prekey.Next(0, 10)];
        break;
    case 1:
        introduce = secondname[prekey.Next(0, 10)];
        break;
    case 2:
        introduce = fullname;
        break;
}
  • Related