Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
        bool CheckSubproject() {
            #pragma warning disable 649 // Unreachable code detected 0162 changed to 649
            if (sub != "")


                return true;
                string result = "";
//(string) is the unreachable code that is detected
            


            if (project == "bmte") {
                            List<fileinfo> files = SearchFiles(dataPath);
                            FileInfo adAssistant = files.Find(x => x.Name == "AdAssistant.cs");
                            if (adAssistant != null)
                                result = "pro";
                            else
                                result = "standard";
                        }
                        
            if (result != "") {
                FileInfo fileInfo = SearchFiles(dataPath).Find(x => x.Name == "Fixer.cs");

                if (fileInfo != null) {
                    StreamReader fileR = new StreamReader(fileInfo.FullName);
                    string code = fileR.ReadToEnd();
                    fileR.Close();

                    Regex re = new Regex(@"const\s+string\s+sub\s+\=\s+""{2}\;");
                    code = re.Replace(code, "const string sub = \"" + result + "\";");

                    StreamWriter fileW = new StreamWriter(fileInfo.FullName);
                    fileW.WriteLine(code);
                    fileW.Close();

                    StopDownloadIssues();
                    AssetDatabase.Refresh();
                    
                }
            }
            return false;
                     #pragma warning restore 649 // Unreachable code detected  # CS0162
        }


What I have tried:

I have tried setting the string to Public, Local, and static. Nothing seems to work with this string. I have a #pragma warning disable and enable for code 649, but when removing the code I get over 60 errors. With the code the only error is with the string.
Posted
Updated 4-Apr-17 22:25pm
v4
Comments
Bernhard Hiller 5-Apr-17 3:58am    
Did you miss some {} when you copied your code here? In order to get that compiler warning, the code should rather look like
if (sub != "")
 {
     return true;
     string result = "";
}
- and now it is clearly visible why that line is "unreachable code".

Looking the code
bool CheckSubproject() {
#pragma warning disable 649 // Unreachable code detected 0162 changed to 649
if (sub != "")
    return true;
string result = "";
//(string) is the unreachable code that is detected
...

if the compiler marks the string result=""; as unreachable it means that the compiler deduces that sub variable has a value that is necessarily distinct that "".
So, the lines that correspond to be executed when sub=="" NEVER will used.
 
Share this answer
 
Quote:
error code of CS0162 "unreachable code detected"

This error message do not tell you that there is an error where you see it, it tells you that preceding code is made in such a way that the line of "unreachable code" will never be executed.
At compile time, the compiler can detect that sub will never be empty and thus will always "return true" and following code will never be executed.
This can be done on purpose, for testing, thus the warning. Your pragma transform the warning to an error.

So you can do any change on string, it will never solve the problem because it is not the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900