views:

41

answers:

2

Hello all! 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?

A: 

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());
MainMa
Why a down-vote? What's wrong with my answer?
MainMa
+1  A: 

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.

Phong