Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
3.50/5 (4 votes)
See more:
Hi,

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 ?

Here is some code:
SQL
[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
            {
                return "Error Message";
            }
        }

Many thanks,
Posted
Updated 24-Feb-11 8:03am
v4

When we are unsure about what Type a function will return, we should use Object in that scenario.
you can achieve this without generic. look at the following code.

C#
protected void Page_Load(object sender, EventArgs e)
 {
   // method call
    object ret = method(0);
    if (ret.GetType() == typeof(string))
    {
        Response.Write("String Object Error");

    }
    else
    {
        Response.Write("List Object");
    }
 }


// magic method declaration ..
 object method(int input)
 {
     if (input == 0)
     {
         return new List<int>();
     }
     else
     {
         return "Error in input";
     }
 }



you may use polymorphism concept also. you make return type of Type Base and return the derived types as well. test the type and do accordingly.


--------------------------------------------------------------------------------------------

Return types are pre-defined at the time of declaration of the Method. you cannot change its Type at run-time using the same function and within the function. return Type can be changed at run-time but not from within the Method.

In generic the return Type is passed along with the Method call like below.
T method<t>(int a)
{ 
   T t1
   return t1;
}

//call
bool c = method<bool>(1);
</bool></t>


here the return type is passed when we are calling the method. we cannot decide the Type T from with in the method like .
method(int a)
{
  return "ass";
 .
 .
  return new List<payment>();
}

</payment>
 
Share this answer
 
Not a good idea. Is this WCF? If so, the error should be communicated back via an exception. Take a look at the FaultException class.

[Update]
~~~~~~~~~~~

This is in response to your comment. You could try something like this:

C#
public class Payment
{
    public int Cost { get; set; }

    public string Detail { get; set; }
}

[Serializable]
public class Payments
{
    public string ErrorMessage { get; set; }

    public List<Payment> PaymentItems { get; set; }
}

[WebMethod]
public Payments GetPayments(int x)
{
    Payments payments = new Payments();

    if (x == 0)
    {
        payments.ErrorMessage = "An error has occured";
    }
    else
    {
        payments.PaymentItems = new[] { new Payment() { Cost = 99, Detail = "Apples" } }.ToList();
    }
    return payments;
}


Here's the normal output:

XML
<PaymentItems>
  <Payment>
    <Cost>99</Cost>
    <Detail>Apples</Detail>
  </Payment>
</PaymentItems>


And here's what you get with the error message set:

XML
<ErrorMessage>An error has occured</ErrorMessage>


[Foot Note]
~~~~~~~~~~~~~

Remember that generic types will not work with web-services unless they are instantiated. You will see this message if you attempt to do that : Only instantiated generic types can be serialized
 
Share this answer
 
v4
Comments
Close Network 24-Feb-11 13:49pm    
Not it is not WCF. I know there are other ways to do this, I want to get a taste of using a generic method/class.
Nish Nishant 24-Feb-11 13:50pm    
What does it use then? Is this a regular ASP.NET web service?
Close Network 24-Feb-11 13:59pm    
Yes, regular asp.net web service.
Nish Nishant 24-Feb-11 14:47pm    
Check my updated answer for one way to do this.
Close Network 24-Feb-11 15:10pm    
Yes, this is an acceptable solution. And also it would be very nice If could do that with generics.
Using generics, you can substitute data types that your method acts acts on. It wont fit your problem.
Here are a few alternatives
1. Return a type of Object. So that you may return a List on success and null on error.
Object o = MyFunction();
if(o==null)
   // error
else
  // typecast object to your list & use


2. If you wish to return different error strings, Return a string & pass out the list from a parameter
eg:
private String Func(out List<yourobj> lst)</yourobj>

So that on error the function will return an error description & on success an empty string would be returned along with the list
 
Share this answer
 
Comments
Nish Nishant 24-Feb-11 13:53pm    
Keep in mind that this is a web service.
Probably, you don't really need generics in this simple case; classic OOP might be enough. All types are derived from System.Object so they all has the virtual method System.Object.ToString. You can override this method in any class to represent its content in string form better.

In case of error, you can return something like

C#
string.Format("Invalid input parameters: {0}", GetParameterInfo());


The method GetParameterInfo can traverse all problematic instances and collect the information is some list of strings based on the return of ToString for each.

I realize your case could me more complex, so this simple approach would be not enough. In this case you should share more of your code.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900