Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys, i was wondering if anyone would help me out with the following solution. I have a warning that says "the variable type is assigned but its value is never used". Also if the user types d in a text box i want the output to be hnc, but id doesn't seem to be doing this. Any idea why. thanks in advance

C#
public string switchGetInformation()
      {

          string Type;
          switch (courseType)
          {
              case 'd': Type = "HND";
                  break;
              case 'c': Type = "HNC";
                  break;
              case 'e': Type = "ECDL";
                  break;
              case 'v': Type = "Vacational";
                  break;
              case 'r': Type = "Reacreational";
                  break;
              case 'a': Type = "Academic";
                  break;
              default: Type = "Unknown";
                  break;

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

Well, it isn't used. You define it at the top of your method, give it a value in one of your case statements, then ignore it until it goes out of scope at the bottom of the method.

Probably what you wanted to say was:
C#
public string switchGetInformation()
      {
          string Type;
          switch (courseType)
          {
              case 'd': Type = "HND";
                  break;
              case 'c': Type = "HNC";
                  break;
              case 'e': Type = "ECDL";
                  break;
              case 'v': Type = "Vacational";
                  break;
              case 'r': Type = "Reacreational";
                  break;
              case 'a': Type = "Academic";
                  break;
              default: Type = "Unknown";
                  break;
          }
      return string.Format("{0} {1} {2}", id, courseType, Type);
      }
(I used String.Format becasue it's a bad idea to concatenate strings, particularly if you do it a lot. "Format" is a lot more efficient)
 
Share this answer
 
You've declared a string called "Type" (just before the switch statement) which you are assigning one of several values within the switch statement, but is then never used.

I assume there is a typo in the above (there are a few in fact), since your return statement is within the switch statement after the default case. I would be surprised if it even compiles like that! You need an extra closing brace before the return.

Not sure how to answer the second part of your question, since it's not clear where "id" is supposed to come from, but I suspect you meant to name the variable that you've called "Type", "id" instead. This would fix both problems. (Not a good idea having a variable called "Type" anyway, since Type is a property of all objects).

If this really is a method in your code, I also suggest you check out Microsoft's coding standards naming conventions. E.g. methods should be Pascal case, not camel case, and I would recommend not beginning with the word "switch".

Finally, since string is immutable, your code would be more efficient if you use StringBuilder, or string.Format, to avoid reallocating memory each time you append to it.

Suggested correction:

C#
public string GetCourseInformation()
      {

          string id;
          switch (courseType)
          {
              case 'd': id = "HND";
                  break;
              case 'c': id = "HNC";
                  break;
              case 'e': id = "ECDL";
                  break;
              case 'v': id = "Vocational";  // I assume you mean Vocational, not vocational!
                  break;
              case 'r': id = "Recreational";  // Note the typo correction.
                  break;
              case 'a': id = "Academic";
                  break;
              default: id = "Unknown";
                  break;
          }  //  You need the closing brace

          return string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0} {1} {2}", id, Convert.ToString(courseType), Convert.ToString(CourseCode));
/* Note the use of culture information format provider.  It probably doesn't matter in this case, and this will always use the invariant culture, but it is good practice to get into using the correct format provider in case you ever have to write code that will work in multiple languages and cultures. */
      }



Regards,
Ian.
 
Share this answer
 
v2
Where are the variables id, courseType and CourseCode declared and initialised? Also, why do you set a value in the variable Type and then never use it for anything? There may be more to your code than you have shown, but it is not obvious.
 
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