Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Error 'not all code paths return a value'

Error 'not all code paths return a value'

Scheduled Pinned Locked Moved C#
helpwcfxml
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bootzilla33
    wrote on last edited by
    #1

    Getting errors in this code on 'ToXml':

    public class EventCode
    {
    public static string ToXml(Soap.EventCode.EvCodes value)
    {
    switch (value)
    {
    case Soap.EventCode.EvCodes.DispatchedForDelivery:
    case Soap.EventCode.EvCodes.Delivered:
    return "OD";
    case Soap.EventCode.EvCodes.DepartedFromTerminal:
    return "L1";
    }
    }
    }

    What am I doing incorrectly so I can fix this error:

    P K P 3 Replies Last reply
    0
    • B Bootzilla33

      Getting errors in this code on 'ToXml':

      public class EventCode
      {
      public static string ToXml(Soap.EventCode.EvCodes value)
      {
      switch (value)
      {
      case Soap.EventCode.EvCodes.DispatchedForDelivery:
      case Soap.EventCode.EvCodes.Delivered:
      return "OD";
      case Soap.EventCode.EvCodes.DepartedFromTerminal:
      return "L1";
      }
      }
      }

      What am I doing incorrectly so I can fix this error:

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      If nothing matches the case statements, what value do you want to return? Add a return with this value at the end of the method or use the default keyword inside tour switch and return the value there.

      This space for rent

      B 1 Reply Last reply
      0
      • B Bootzilla33

        Getting errors in this code on 'ToXml':

        public class EventCode
        {
        public static string ToXml(Soap.EventCode.EvCodes value)
        {
        switch (value)
        {
        case Soap.EventCode.EvCodes.DispatchedForDelivery:
        case Soap.EventCode.EvCodes.Delivered:
        return "OD";
        case Soap.EventCode.EvCodes.DepartedFromTerminal:
        return "L1";
        }
        }
        }

        What am I doing incorrectly so I can fix this error:

        K Offline
        K Offline
        Karthik_Mahalingam
        wrote on last edited by
        #3

        public static string ToXml(Soap.EventCode.EvCodes value)
        {
        string returnValue = null; // provide your default value
        switch (value)
        {
        case Soap.EventCode.EvCodes.DispatchedForDelivery:
        case Soap.EventCode.EvCodes.Delivered:
        returnValue ="OD"; break;
        case Soap.EventCode.EvCodes.DepartedFromTerminal:
        returnValue ="L1"; break;
        }
        return returnValue;
        }

        Richard DeemingR 1 Reply Last reply
        0
        • K Karthik_Mahalingam

          public static string ToXml(Soap.EventCode.EvCodes value)
          {
          string returnValue = null; // provide your default value
          switch (value)
          {
          case Soap.EventCode.EvCodes.DispatchedForDelivery:
          case Soap.EventCode.EvCodes.Delivered:
          returnValue ="OD"; break;
          case Soap.EventCode.EvCodes.DepartedFromTerminal:
          returnValue ="L1"; break;
          }
          return returnValue;
          }

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          You'll need some breaks in there. :)


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          K 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            You'll need some breaks in there. :)


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            K Offline
            K Offline
            Karthik_Mahalingam
            wrote on last edited by
            #5

            corrected :)

            Richard DeemingR 1 Reply Last reply
            0
            • K Karthik_Mahalingam

              corrected :)

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              Are you sure? It's showing as "modified", but I still don't see any breaks. :confused:


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              K 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Are you sure? It's showing as "modified", but I still don't see any breaks. :confused:


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                K Offline
                K Offline
                Karthik_Mahalingam
                wrote on last edited by
                #7

                oh my!! earlier "=" sign was missing, initially i thought that and corrected, after adding the code in visual studio code editor i realized that "break" is missing. Now its finally corrected :) this is the problem when writing the code without using code editor software

                1 Reply Last reply
                0
                • P Pete OHanlon

                  If nothing matches the case statements, what value do you want to return? Add a return with this value at the end of the method or use the default keyword inside tour switch and return the value there.

                  This space for rent

                  B Offline
                  B Offline
                  BenScharbach
                  wrote on last edited by
                  #8

                  I agree with adding the Default keyword in the Case block.

                  Ben Scharbach Temporalwars.Com YouTube:Ben Scharbach

                  1 Reply Last reply
                  0
                  • B Bootzilla33

                    Getting errors in this code on 'ToXml':

                    public class EventCode
                    {
                    public static string ToXml(Soap.EventCode.EvCodes value)
                    {
                    switch (value)
                    {
                    case Soap.EventCode.EvCodes.DispatchedForDelivery:
                    case Soap.EventCode.EvCodes.Delivered:
                    return "OD";
                    case Soap.EventCode.EvCodes.DepartedFromTerminal:
                    return "L1";
                    }
                    }
                    }

                    What am I doing incorrectly so I can fix this error:

                    P Offline
                    P Offline
                    Paul_Williams
                    wrote on last edited by
                    #9

                    In your example there would be nothing to return if value where not one of the handled cases. You need to specify a return value for ALL cases or throw an exception for invalid values. One way to do this is by using the default case in your switch statement. Here is an example:

                    public class EventCode
                    {
                    public static string ToXml(Soap.EventCode.EvCodes value)
                    {
                    switch (value)
                    {
                    case Soap.EventCode.EvCodes.DispatchedForDelivery:
                    case Soap.EventCode.EvCodes.Delivered:
                    return "OD";
                    case Soap.EventCode.EvCodes.DepartedFromTerminal:
                    return "L1";
                    // Catch-all for all other cases.
                    // Throwing an exception here is just an example implementation, not mandatory.
                    default: throw new ArgumentOutOfRangeException(nameof(value), "Unsupported Evcode: " + value);

                           }
                       }
                    

                    }

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups