1

If anyone can point out where I'm going wrong .

public string GenerateName(char gender)
    {
        string name;
        Random rand = new Random();


        if (gender == 'm')
        {

            string[] maleNames = File.ReadAllLines(Directory.GetCurrentDirectory() + @"\ModifiedMalesNamesList.txt");
            string[] lastNames = File.ReadAllLines(Directory.GetCurrentDirectory() + @"\ModifiedLastNamesList.txt");
            name = maleNames[rand.Next(0, maleNames.Length - 1)] + " " + lastNames[rand.Next(0, lastNames.Length - 1)];

            return name;
        }
        else
        {

        }
        return name;
    }

The compiler says unassigned local variable. Thanks in advance

Amit Kumar
  • 5,888
  • 11
  • 47
  • 85
  • 1
    What happens when the gender is not `'m'`? – Ken Y-N Nov 08 '17 at 05:51
  • sorry, its incomplete m is short for male when it's not m it generates a female name. – DarkxDivinityX Nov 08 '17 at 05:53
  • @Ken's comment is hinting to you as to the reason of the error message. The compiler is telling you, quite explicitly frankly, that there is a execution path through the method that results in the `name` variable not being assigned, even though you return its value. What value do you think should be returned when the `gender` variable doesn't equal `'m'`? – Peter Duniho Nov 08 '17 at 05:57
  • when I added the else path it still showed the error from before. When I set string name = string.Empty; it fixed the issue. – DarkxDivinityX Nov 08 '17 at 06:03

1 Answers1

2

Try to edit this:

From this:

string name;

To this:

string name = string.Empty;

You can not use string name unless you initialize it with values.

To explain the error in further details, you cannot just declare a string and return it without any values. Since string is a reference type, you need to initialize it as null, empty or any collection of character values.

In this case, in your if and else statement, there is no value of name on else statement so compiler will tell you the error since it detected that "if condition might fail" and it will go to else statement then after that it will return the name variable.

So if that's the case, what will be the value of name if it doesn't go to if statement? Compiler is smart enought to detect this. You can do this as well:

    string name;
    Random rand = new Random();
    if (gender == 'm')
    {
        string[] maleNames = File.ReadAllLines(Directory.GetCurrentDirectory() + @"\ModifiedMalesNamesList.txt");
        string[] lastNames = File.ReadAllLines(Directory.GetCurrentDirectory() + @"\ModifiedLastNamesList.txt");
        name = maleNames[rand.Next(0, maleNames.Length - 1)] + " " + lastNames[rand.Next(0, lastNames.Length - 1)];

        return name;
    }
    else
    {
        name = "";
    }
    return name;

On this case, you cover all grounds and you can safely return string name with values so it will not trigger an error.

Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
  • Thanks that seemed to work could you possibly explain why string.Empty was able to resolve the issue? that would be really helpful so i understand where the problem stems from so i don't repeat it – DarkxDivinityX Nov 08 '17 at 05:58
  • I edited my answer with further explanations. @DarkxDivinityX – Willy David Jr Nov 08 '17 at 06:21