Return Array From WebService

Here’s the simplest way to accomplish what you want. Have your webservice return a simple string array:

[WebMethod]
public string[] getPersonIds(string Id)
{
//substitute code to actually populate the array with the dataset data
string[] personIds = {“ADS34579354”, “ASR34579354”, “TYU34579354”};
return personIds;
}

In the client:

//testWs = proxy object.
TestWs.TestWS testWs = new TestWs.TestWS();
string[] personIds = testWs.getPersonIds();
foreach (string personId in personIds)
{
Console.WriteLine(personId);
}
If you actually want to return an object called “PersonResult” with named  instances of “ID” then here’s what you do:

in your WebServices code:

create a class called “PersonResult”, with the one single “Id” member,  declared as a primitive array:
class PersonResult
{
public string[] Id;
}
and your [WebMethod] function should look like this:

[WebMethod]
public PersonResult getPersonIds()
{
PersonResult personResult = new PersonResult();
personResult.Ids.Add(“ADS34579354”);
personResult.Ids.Add(“ASR34579354”);
personResult.Ids.Add(“TYU34579354”);
return personResult;
}

in the client, here’s your code to obtain the instance of “PersonResult”  from the webservice:

TestWs.TestWS testWs = new TestWs.TestWS();
//define a variable of type TestWs.PersonResult, as defined by the proxy  class
TestWs.PersonResult personResult = testWs.getPersonIds();
foreach (string personId in personResult.Ids)
{
Console.WriteLine(personId);
}
Hope that helps!

Jorge

http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.aspnet.webservices/2005-06/msg00068.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: