By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,083 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,083 IT Pros & Developers. It's quick & easy.

Error: not all code paths return a value

P: 1
i've write a simple code in vs c#.net,at run time i have this problem. Error: not all code paths return a value
Please help me.

Expand|Select|Wrap|Line Numbers
  1. class Car
  2.     {
  3.         private string m_model;
  4.         private string m_make;
  5.  
  6.         public string make
  7.         {
  8.             get
  9.             {
  10.                 return m_make;
  11.             }
  12.             set
  13.             {
  14.                 if
  15.                     (value != "saipa" || value != "reno")
  16.                 {
  17.                     m_make = "";
  18.                 }
  19.                 else
  20.                 {
  21.                     m_make = value;
  22.                 }
  23.             }
  24.         }
  25.  
  26.         public string model
  27.         {
  28.             get
  29.             {
  30.                 return m_model;
  31.             }
  32.             set
  33.             {
  34.                 m_model = value;
  35.             }
  36.         }
  37.         public string sub() <-- ERROR IS HERE
  38.         { 
  39.  
  40.                 switch (m_make)
  41.                 {
  42.                     case "saipa":
  43.                         {
  44.  
  45.                             return "Tehran";
  46.                             break;
  47.                         }
  48.                     case "reno":
  49.                         {
  50.                             return "France";
  51.                             break;
  52.                         }
  53.                     case "IRKCO":
  54.                         {
  55.                             return "Tehran";
  56.                             break;
  57.                         }
  58.                 }
  59.  
  60.  
  61.         }  
  62.     }
Apr 8 '10 #1

✓ answered by GaryTexmo

I hope you appreciate the eye bleeding I went through to read that! I probably should have waited until T got in here with his code cleanup... but you should really be doing that on your own so he doesn't have to!

You're missing a return at the end of your method. You've got a return for several cases, but what if none of them are met? To be honest, I'm not sure if that would go away if you had a "default" case there either... but that would be a compiler there.

Here's a big tip, though I suppose it's a matter of personal preference. I'm a fan of a single return statement per method, it makes it much easier to keep track of what's going on and anticipate how your method is executing. This doesn't always apply, but in like 95% of cases (that I've come across), a single return statement is nicer, cleaner, and easier to understand.

For example...
Expand|Select|Wrap|Line Numbers
  1. public string DoSomething(int code)
  2. {
  3.   string returnString = "";
  4.   switch (code)
  5.   {
  6.     case 0:
  7.       returnString = "The code was 0!";
  8.       break;
  9.     ...
  10.   }
  11.  
  12.   return returnString;
  13. }

Share this Question
Share on Google+
5 Replies


GaryTexmo
Expert 100+
P: 1,501
I hope you appreciate the eye bleeding I went through to read that! I probably should have waited until T got in here with his code cleanup... but you should really be doing that on your own so he doesn't have to!

You're missing a return at the end of your method. You've got a return for several cases, but what if none of them are met? To be honest, I'm not sure if that would go away if you had a "default" case there either... but that would be a compiler there.

Here's a big tip, though I suppose it's a matter of personal preference. I'm a fan of a single return statement per method, it makes it much easier to keep track of what's going on and anticipate how your method is executing. This doesn't always apply, but in like 95% of cases (that I've come across), a single return statement is nicer, cleaner, and easier to understand.

For example...
Expand|Select|Wrap|Line Numbers
  1. public string DoSomething(int code)
  2. {
  3.   string returnString = "";
  4.   switch (code)
  5.   {
  6.     case 0:
  7.       returnString = "The code was 0!";
  8.       break;
  9.     ...
  10.   }
  11.  
  12.   return returnString;
  13. }
Apr 8 '10 #2

tlhintoq
Expert 2.5K+
P: 3,525
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Apr 8 '10 #3

tlhintoq
Expert 2.5K+
P: 3,525
I probably should have waited until T got in here with his code cleanup...
My title shouldn't be 'Moderator'... It should be 'Editor'... I feel like I work at a newspaper, editing other people's submissions.
Apr 8 '10 #4

GaryTexmo
Expert 100+
P: 1,501
I liked your suggestions about making that code button more obvious. It's quite surprising how many people come here and miss it. You do a lot of edits and I don't envy that task ><
Apr 8 '10 #5

tlhintoq
Expert 2.5K+
P: 3,525
What kills me is that it is so plainly stated when someone starts a new post

I think it just goes to illustrator that a LOT of our new members are new to forum posting in general, and don't understand how to put in tags in general.
Apr 8 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.