Friday, November 18, 2005

 

TRIM Webservice: Adding Items to a Request Dynamically

This is more so I don't forget how to do this. So this will be boring to anyone who isn't me (or cares about the TCT). Mikal might like to comment on this post.

The problem: You've got your TrimRequest, and you're trying to populate the items. Unfortunately, the number of Operations in TrimRequest.Items is going to change dynamically, and there isn't any .Add method.

The solution: Create an ArrayList, and stick all your Operations in there. 'Cause their cool, ArrayLists do have a .Add method (and a .AddRange). Once you're finished with it, jam it into the TrimRequest.Items.

The trick is that the cast isn't good, so the code won't complie.

Thanks to the awesomness of Resharper, I discovered that if you hack the generated code and change the line
public Operation[] Items;
to
public object[] Items;
That will let you pass all the operations into the collection, and through the joys of XML, it will magically work at the other end.

I need to do some further testing on this, but it looks good for the moment.

Comments:
There is also a ToArray() method on the ArrayList which will do the right thing.

Mikal
 
No, I was using .ToArray() to pass it back into the req.Items. That was where the cast was failing.

I think it has trouble realising that the objects in the ArrayList are actually operations.
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?