How To write a generic class/method in C# 2.0 to return a List of objects or just a simple string based on a condition?
I have a webservice which has a method that returns a List of Payment objects provided with some input parameters. However if the input parameters are not in the correct format, I would like to return an error message which is of type string, not a List of Payment objects. I would like do this using Generic Classes concept in C#. Anyone has any idea about how I can manage to do this ?
Many thanks,
Here is some code:
[WebMethod]
public List<Payment> GetPayments(string firstDate, string lastDate, string entegrationStatus)
{
if (Common.IsDateTime(firstDate) && Common.IsDateTime(firstDate) && Common.IsValidEntegrationStatus(entegrationStatus))
{
return paymentManager.GetPayments(firstDate, lastDate, entegrationStatus);
}
else
开发者_C百科 {
return "ERROR MESSAGE";
}
}
I'm fairly sure all you need to do is throw an exception in your service method. This will populate the Error property of the async event args that are returned back to the client. The client can check for errors in its 'async completed' event handler using this property and handle it accordingly.
This is probably a better design than just sending back a string as well because it separates a regular return message from an error return message.
It's as simple as:
Edit - Using the code you posted:
[WebMethod]
public List<Payment> GetPayments(string firstDate, string lastDate, string entegrationStatus)
{
if (Common.IsDateTime(firstDate) && Common.IsDateTime(firstDate) && Common.IsValidEntegrationStatus(entegrationStatus))
{
return paymentManager.GetPayments(firstDate, lastDate, entegrationStatus);
}
else
{
throw new ArgumentException("Your error message.");
}
}
If you REALLY want to do it, you can use a Tuple-like class (they introduced Tuples in C# 4.0). If you are using C# 2.0 then you can use KeyValuePair<List<YourObject>, string>
. Be aware that I'm NOT suggesting you do it! You should throw an exception, or put the string message as an out
parameter.
well you could do something simply like having an ErrorString property on your object, and if your main method fails, set the ErrorString and return null from your method
SomeObject o = new SomeOject();
ILIst<Things> things = o.GetThings();
if(things == null)
Response.Write(o.ErrorString)
A generic isn't really suitable for this use, because you cannot return a generic type based on a runtime condition within a generic method (since the method must be compiled with an exact type to run in the first place).
A common approach is to use an out
parameter for your list and your string, and have your method return a bool denoting whether the list was returned. These methods are usually prefixed with Try...
. eg
bool TryGetList<T>(out IList<T> lst, out string Error) {
if (!somcondition) {
Error = "err!";
return false;
}
lst = ...
return true;
}
The other technique is simply to use exceptions, but they can be more costly if you are likely to have errors frequently. An ArgumentException
for example will let you specify which argument was invalid, and the caller can check the ParamName
of the caught exception to decide what he should do afterwards.
精彩评论