Display array values from web service method
I'm new to web services and im actually trying to learn how to develop one in C#. I have the following method in my web service which actually displays an array of int when i test it.
[WebMethod]
public int[] FindID(string str1,string str2)
{
Customer obj = new Customer();
obj.FindMatch(str1,str2);
return obj.customer_id;
}
Now in my web application in which i have a button, the code is as below:
Dim obj As localhost.Service = New localhost.Service
Dim str1 As String = Session("str1")
Dim str2 As String = Session("str2")
Response.Write(obj.FindID(str1, str2开发者_如何转开发))
The problem is that only the first value from the array is being displayed. Can anyone please help me to solve this problem?
You could also use String.Join()
http://msdn.microsoft.com/en-us/library/57a79xd0.aspx which takes a delimiter and an array of strings, and returns a single string containing the original strings inside the array, delimited by your delimiter.
This can be achieved very simply:
Console.WriteLine(", ", string.Join(obj.FindID(str1, str2)));
Console.Write
displays only a single value such an integer, a float number or a string. It will never walk through an array to display each value.
Instead, you can call a Console.Write
on each entry in your array.
foreach (int value in obj.FindID(str1, str2))
{
Console.WriteLine(value.ToString());
}
Note that calling Console.Write
is resource expensive. If you need to display values of a very long array, maybe you will achieve better results by using StringBuilder class, then calling Console.Write
once.
StringBuilder stringBuilder = new StringBuilder();
foreach (int value in obj.FindID(str1, str2))
{
stringBuilder.AppendLine(value.ToString());
}
// Call this once.
Console.Write(stringBuilder.ToString());
精彩评论