Click here to Skip to main content
12,697,850 members (26,759 online)
Rate this:
 
Please Sign up or sign in to 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 16-Feb-13 5:23am
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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  
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
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... :) :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170118.1 | Last Updated 17 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100