Click here to Skip to main content
Sign Up to vote bad
good
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 16-Feb-13 4:23am
Edited 17-Feb-13 0:13am
Tadit Dash29.9K

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

2 solutions

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.
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.
  Permalink  
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
Tadit Dash - 17-Feb-13 6:07am
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
Tadit Dash - 17-Feb-13 12:20pm
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.
  Permalink  
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Christian Graus 373
1 OriginalGriff 326
2 Prasad_Kulkarni 314
3 Ron Beyer 286
4 Prasad Khandekar 225
0 Sergey Alexandrovich Kryukov 7,061
1 Prasad_Kulkarni 3,830
2 OriginalGriff 3,620
3 _Amy 3,370
4 CPallini 3,074


Advertise | Privacy | Mobile
Web02 | 2.6.130619.1 | Last Updated 17 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid