Error 'not all code paths return a value'
-
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:
-
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:
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
-
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:
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;
} -
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;
}You'll need some
break
s in there. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
You'll need some
break
s in there. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
corrected :)
-
corrected :)
Are you sure? It's showing as "modified", but I still don't see any
break
s. :confused:
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Are you sure? It's showing as "modified", but I still don't see any
break
s. :confused:
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
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 -
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
I agree with adding the Default keyword in the Case block.
Ben Scharbach Temporalwars.Com YouTube:Ben Scharbach
-
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:
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 thedefault
case in yourswitch
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);} }
}