locked
Webservice with GetEnumerator method fires an error RRS feed

  • Question

  • Hi All,

     

    currently I am working on an application which calls a webservice. In the windows application I created the following classes:

     

    Class1:

     

    [Serializable]

    public class VoucherDetails

    {

    List<VoucherSubDetails> allSubDetails;

     

    public List<VoucherSubDetails> AllSubDetails

    {

    get { return allSubDetails; }

    set { allSubDetails = value; }

    }

    ...

    }

     

    Class 2:

     

    [Serializable]

    public class VoucherSubDetails

    {

    ...

    }

     

    Both in the webservice and in the windows application, I have made raference to the generated dll.

    i.e. In the webservice I have made:

     

    [WebMethod()]

    public bool SaveNewVoucher(VoucherDetails voucherDetails)

    {

    foreach (VoucherSubDetails subDetails in voucherDetails.AllSubDetails)

    { ... }

     

    }

     

     

    Everything works fine, until I decided to be more professional and use Enumerator method in order to loop through the subDetails in the voucher details. The complete code is below:

     

     

    [Serializable]

    public class VoucherDetails

    {

    List<VoucherSubDetails> allSubDetails;

     

    IEnumerator IEnumerable.GetEnumerator()

    {

    return allSubDetails.GetEnumerator();

    }

    }

     

    Class 2:

     

    [Serializable]

    public class VoucherSubDetails

    {

    ...

    }

     

    [WebMethod()]

    public bool SaveNewVoucher(VoucherDetails voucherDetails)

    {

    foreach (VoucherSubDetails subDetails in voucherDetails)

    { ... }

    }

     

     

    As soon as I make this code, the following error is shown:

     

    To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. VouchersNamespace.VoucherDetails does not implement Add(System.Object).

     

    Note that this happens as soon as I enter VoucherDetails class in the parameters of the webmethod (i.e. when i removed the foreach statement, the error still popped out.

     

    I think that I am missing something. Can anyone help me please?

     

    thanks

     

    Wednesday, April 9, 2008 1:34 PM

Answers

  • IEnumerable is an interface.  In order to use its methods in your class, you have to implement the interface.  Depending on the requirements of the interface, however, you may have to define a number of functions.

     

    I'm a VBer, so I'm not familiar with the syntax, but here is a C# tutorial on the topic:

    http://www.csharp-station.com/Tutorials/Lesson13.aspx

     

    All of that said, I don't see exactly why you need to do this.  You have already exposed the List<VoucherSubDetails>, and I think the list is enumerable.

     

    As I understand it, you would only implement IEnumerable if your type was a collection of some type.  VoucherDetails class will represent an instance of a VoucherDetails object, not multiple VoucherDetails objects.  If you were to add another class, called maybe VoucherDetailsCollection, you could implement IEnumerable there, but even then I'd suggest inheriting an existing array, list, or other collection type.

    Sunday, April 13, 2008 11:30 PM

All replies

  • Does anyone have a solution for my problem please  since I am still stuck with this problem Sad

     

     

    Thanks to all

    Sunday, April 13, 2008 8:03 AM
  • IEnumerable is an interface.  In order to use its methods in your class, you have to implement the interface.  Depending on the requirements of the interface, however, you may have to define a number of functions.

     

    I'm a VBer, so I'm not familiar with the syntax, but here is a C# tutorial on the topic:

    http://www.csharp-station.com/Tutorials/Lesson13.aspx

     

    All of that said, I don't see exactly why you need to do this.  You have already exposed the List<VoucherSubDetails>, and I think the list is enumerable.

     

    As I understand it, you would only implement IEnumerable if your type was a collection of some type.  VoucherDetails class will represent an instance of a VoucherDetails object, not multiple VoucherDetails objects.  If you were to add another class, called maybe VoucherDetailsCollection, you could implement IEnumerable there, but even then I'd suggest inheriting an existing array, list, or other collection type.

    Sunday, April 13, 2008 11:30 PM