1

I am programming in C# and I keep getting an error for my string variable result.

When I hover over the line return result it says use of unassigned local variable.

Do I have to assign result a value before I use it? How come i do not get the same error for SqlDataReader reader?

string searchbyLastName(string lastname)
{
    string result; 

    SqlDataReader reader;

    try
    {
        reader = myCommand.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                if (reader["LastName"].ToString() == lastname)
                {
                    result = reader.GetString(0);
                    break;
                }
            }

            return result;
        }
        else 
           return "No results found";
    }
    catch (Exception)
    {
        return("Database Error");
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2510809
  • 235
  • 3
  • 14

2 Answers2

3
if (reader["LastName"].ToString() == lastname)

If that is never true then result is not initialized. The SqlDataReader is always initialized prior to being used. Just initialize the string.

var result = "";
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • 1
    Bad practice. Use var result = String.Empty; – Dan Hunex Oct 03 '14 at 04:11
  • @DanHunex: No thank you, please explain why you consider it a "bad practice". There hasn't been any actual semantic difference in quite some time due to string interning. Do you simply enjoy typing? – Ed S. Oct 03 '14 at 04:12
  • Why do you think String.IsNullOrWhitespace is added? Because people sometimes write " " instead of "" . But String.Empty always guarantee that it is empty – Dan Hunex Oct 03 '14 at 04:17
  • @DanHunex: They do? Weird. I have never, in over a decade of work, seen that error. I think I'll stick with `""`, thanks though. PS: `IsNullOrWhitespace` was almost certainly *not* added to account for typos when attempting to initialize a variable to an empty string. It's meant to check for empty *input*, not incorrectly initialized variables. – Ed S. Oct 03 '14 at 04:18
  • Well, you can also see this but i m not trying to convince you with more than 10 years of experience http://stackoverflow.com/questions/2905378/string-empty-versus – Dan Hunex Oct 03 '14 at 04:22
  • @DanHunex: Well... the highest voted question there says pretty directly "there is no difference", but that's a technical answer. How about this one? http://stackoverflow.com/questions/263191/in-c-should-i-use-string-empty-or-string-empty-or. I agree with Jon Skeet there; you're inventing a problem. If you like it, that's fine, I have nothing against it, but I prefer `""`, and I have never heard of someone typing `" "` which led to a bug. Ever. Of course, that doesn't mean it hasn't or will not happen, but I'll take my chances on this one. – Ed S. Oct 03 '14 at 04:24
  • @DanHunex: And I didn't mean to say that my ten years is some awesome accomplishment, but it's certainly a large enough sample size to have written many thousands of `""`'s – Ed S. Oct 03 '14 at 04:29
0

The compiler needs to know you have definitely assigned a value to a variable, at the point where you are using that value.

In your case reader is always assigned a value (reader = myCommand.ExecuteReader();), but result isn't, because that depends on reader["LastName"].ToString() == lastname being true, which may never happen (even if you designed the system so that in reality you always find a match - the compiler doen't know that).

So somewhere before you enter that while-loop you have to assign a value. result = null would be enough. This value could also be used to signal a "lastname not found" condition to the calling code.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111