Thursday, July 14, 2005

Passing SortedList via WebServices [Q&A]

Classes which implements IDictionary are very useful for developers. Concept of key - value addressing could be applied in many areas, including web services.
however, when you try to return a SortedList, ListDictionary or Hashtable from webservice you get run-time error when accessing web service:

System.NotSupportedException: The type System.Collections.SortedList is not
supported because it implements IDictionary.

Cheapest server side solution is to return array of DictionaryEntry filled by SortedList.CopyTo method. Sample code is :
[WebMethod]
public DictionaryEntry[] HelloWorld()
{
SortedList sl = new SortedList();
sl.Add("Key","Value");
DictionaryEntry[] deArray = new DictionaryEntry[sl.Count];
sl.CopyTo(deArray, 0);
return deArray;
}
And on client side you need to create SortedList and fill it manually.
Please note, that you can export entries from one list and add it to another. For example, server can use HashTable for better performance and client SortedList in order to display items ordered for end user.

Applies to all dotnet languages: C#, VB.NET, C++.NET, J#
Idea of this topic was suggested by google search engine.

1 Comments:

At 12:31 PM, Anonymous Anonymous said...

Nice solution. It was very helpful for me.

 

Post a Comment

<< Home