Error: not all code paths return a value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • masoudshirzadi
    New Member
    • Apr 2010
    • 1

    Error: not all code paths return a value

    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.

    Code:
    class Car
        {
            private string m_model;
            private string m_make;
    
            public string make
            {
                get
                {
                    return m_make;
                }
                set
                {
                    if
                        (value != "saipa" || value != "reno")
                    {
                        m_make = "";
                    }
                    else
                    {
                        m_make = value;
                    }
                }
            }
    
            public string model
            {
                get
                {
                    return m_model;
                }
                set
                {
                    m_model = value;
                }
            }
            public string [U]sub() <-- ERROR IS HERE[/U]
            { 
                
                    switch (m_make)
                    {
                        case "saipa":
                            {
    
                                return "Tehran";
                                break;
                            }
                        case "reno":
                            {
                                return "France";
                                break;
                            }
                        case "IRKCO":
                            {
                                return "Tehran";
                                break;
                            }
                    }
               
               
            }  
        }
    Last edited by tlhintoq; Apr 8 '10, 05:03 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    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...
    Code:
    public string DoSomething(int code)
    {
      string returnString = "";
      switch (code)
      {
        case 0:
          returnString = "The code was 0!";
          break;
        ...
      }
    
      return returnString;
    }

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      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.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Originally posted by Gary
        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.

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          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 ><

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            What kills me is that it is so plainly stated when someone starts a new post
            [imgnothumb]http://files.me.com/tlhintoq/10jihf[/imgnothumb]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.

            Comment

            Working...