thanosazlin
thanosazlin

Reputation: 189

visual c# problems with return in a method

I found this code to search for a registry key, it works fine if i set it to void and have it pop up a window using messagebox.show , and i see the key it found. but, if i change it to return a string, the compiler fails with "not all code paths return a value" if i try to set a string in the method called "reg_result" and return it, i am returning the value to a string variable that calls the method.

string reg_result2 = Search_For_Registry_Keys(Registry.Users, search);

private static string Search_For_Registry_Keys(RegistryKey rk, string search)
 {
     string reg_result = "";
     if (rk.SubKeyCount > 0)
     {
         foreach (var temp in rk.GetSubKeyNames())
         {
             if (temp.ToLower().Contains(search.ToLower()))
             {
                 reg_result = (String.Format(rk.Name + "\\" + temp));
                 MessageBox.Show(String.Format("Match Found In Registry Key {0} Present At Location {1}", temp, rk.Name + "\\" + temp));
                 return reg_result;

             }
         }
         foreach (var temp in rk.GetSubKeyNames())
         {
             try
             {
                 if (rk.OpenSubKey(temp).SubKeyCount > 0)
                 {
                     Search_For_Registry_Keys(rk.OpenSubKey(temp), search);
                 }
             }
             catch
             {
             }
         }
     }

 }

I then tried the following by putting a 2nd return after the if statement and that got rid of the compiling error about "not all code paths return a value", but that didn't seem to help as then the method would never return when i stepped through it until it reached the end of it's searching, so I'm little lost on how to fix this?? yes i am newbie. I would think i my first code would work and i only need one return for when i find the result i am looking for... :)

Upvotes: 2

Views: 105

Answers (4)

Steve Morgan
Steve Morgan

Reputation: 13091

Big edit:

Ignore what I posted; it was nonsense. Just see dlev's answer, it's much better!

Upvotes: 0

dlev
dlev

Reputation: 48596

Your code should look like this (comments indicate changes):

private static string Search_For_Registry_Keys(RegistryKey rk, string search)
 {
     string reg_result = "";
     if (rk.SubKeyCount > 0)
     {
         foreach (var temp in rk.GetSubKeyNames())
         {
             if (temp.ToLower().Contains(search.ToLower()))
             {
                 reg_result = (String.Format(rk.Name + "\\" + temp));
                 MessageBox.Show(String.Format("Match Found In Registry Key {0} Present At Location {1}", temp, rk.Name + "\\" + temp));
                 return reg_result;

             }
         }
         foreach (var temp in rk.GetSubKeyNames())
         {
             try
             {
                 if (rk.OpenSubKey(temp).SubKeyCount > 0)
                 {
                     // Added and changed lines here
                     string retVal = Search_For_Registry_Keys(rk.OpenSubKey(temp), search);
                     if (!retVal.Equals(""))
                     {
                         return retVal;
                     }
                 }
             }
             catch
             {
             }
         }
     }

     // Added this line
     return reg_result;
 }

Note what I've done: if no key is found at all, then you return "" (the value initially assigned to reg_result). Also, when you recurse on a subkey (in the second foreach loop,) and you do find a result, that result is immediately returned.

Whenever you have a method that promises to return a value, you have to ensure that all code paths through it either a) actually return a value, or b) throw an uncaught exception.

Upvotes: 1

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25445

In this situation you should create an empty string at the top of your method

var retValue = string.Empty;

Then change this return reg_result to this

retValue = reg_result;

Then at the end of you method outside all ifs and for ect.

return retValue;

Hope this helps.

Upvotes: 1

Paul Sonier
Paul Sonier

Reputation: 39480

If you define that a function returns a value (by stating the type of the return value) you have to return a value in every single case, not only when you find what you're looking for, but also when you DON'T find what you're looking for. Put a return reg_result; as the last line of your function, and it should work. Of course, you may want to change the value you return if you want some indicator other than the empty string that you didn't find what you were looking for.

Upvotes: 2

Related Questions