Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public string getInformation()
{
      return (id + " " + Convert.ToString(courseType) + " " + Convert.ToString(CourseCode));

      string type = "";

      if (courseType == 'c')
      {
            type = "HNC";
      }
}


Hey, I was wondering, if anyone there would help me out with this solution.
Its giving me two errors -
1. "unreachable code detected" &
2. "the variable type is assigned but its value is never used".
Posted
v2
Comments
Sergey Alexandrovich Kryukov 16-Feb-13 20:03pm    
By the way, don't use immediate constants like 'c' and "". Instead of "", use string.Empty.
As to the question: why not reading the warning text thoroughly? it explains everything.
—SA

It is because you have written some code after the return statement, which are not getting executed.

That's why the codes written after the return statement are unreachable.
And the type variable is not used, only assigned.
So, your code should look like below.
C#
public string getInformation()
{
     string type = ""; // Use string.Empty instaed of "".

     // You can do this like courseType.Equals(yourVariable); inside if statement, 
     // where yourVariable will contain the character to compare.
     if (courseType == 'c') 
     {
          type = "HNC";
          // try to use this "type" variable or else it is unnecessary.
     }

     return (id + " " + Convert.ToString(courseType) + " " + Convert.ToString(CourseCode));
}


Note
I just corrected the current code. If you face any other problems with your logic, then you have to solve that. Otherwise it should work.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 16-Feb-13 20:07pm    
Perhaps this i s just poor phrasing, but the statement is absolutely not true: return does not have to be the last statement, and in many cases, it cannot. No matter if you actually understand it correctly or not, it would only confuse the reader and mislead a beginner.
—SA
Yes, my apologies...

That was a mistake in hurry. I will be careful on that.

I have updated the answer. And you can now consider it for up-voting. :)

Thanks,
Tadit
Sergey Alexandrovich Kryukov 17-Feb-13 11:22am    
Sure, a 5.
—SA
Thanks a lot @SA. :)

I also reached 15K reputation points with your vote.

Thanks again... :) :)
a) The first statement is a return; nothing after that will be executed.

b) You don't use the type variable.

c) This is basic stuff and the compiler is telling you what's wrong; no need to ask here.
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 16-Feb-13 20:05pm    
My 5, especially for the item (c). I advised just to read the warning text thoroughly... :-)
—SA

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