0

I wrote simple code that generates random array of int's on webservice, i joined it with few buttons in html form but i cant achive propper output. Every time im invoeking the service im getting this output in my textbox "Macierze.myservice.ArrayOfInt[]". Anyone know how can i solve this and get full array in my textbox?

webservice code

[WebMethod]
    public int[][] GenerateMatrix(int column, int row)
    {
        Random rnd = new Random();
        int[][] matrix = new int[column][];
        for (int i = 0; i < matrix.Length; i++)
        {
            matrix[i] = new int[column];
            for (int j = 0; j < matrix[i].Length; j++)
                matrix[i][j] = rnd.Next(1, 1000);
        }
        return matrix;
    }

Button code

 protected void Button1_Click(object sender, EventArgs e)
    {
        var serviceClient = new myservice.WebService1SoapClient("WebService1Soap");
        output.Text = serviceClient.GenerateMatrix(Convert.ToInt32(input1.Text), Convert.ToInt32(input2.Text)).ToString();
    }
2
  • That's just what ToString called on jagged array returns. What is the proper output for you here? I am pretty sure it can be done with some LINQ Commented May 10, 2016 at 8:55
  • I just want to get a matrix generated in webservice method to be displayed on this textbox. Like [1 2 3 4] [5 6 7 8] but im keep getting this comunicate instead of generated numbers. Commented May 10, 2016 at 8:59

2 Answers 2

3

As mentioned in the comment, what you currently see is the default output of the ToString method. That will happen on all of the object in .NET which do not override ToString inherited from Object class. For sure, arrays, including jagged ones, are an example of such objects.

I guess for your case a little bit of Linq and String.Join can easily do the trick though:

var matrix = serviceClient.GenerateMatrix(Convert.ToInt32(input1.Text), Convert.ToInt32(input2.Text));
output.Text = String.Join(" ", matrix.Select(a => "[" + String.Join(", ", a) + "]"));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You very much, im new in c# and i was struggling with this since yesterday :)
0

Performing a .ToString() on an int[][] will not automatically output all its values. The same is for almost every object in .NET unless the .ToString() method has been overwritten to output a value.

You must iterate through all values in the array and add them to a string. Exactly the same way that you generated the array in the first place.

You could write an extension method to generate the string you want. Note that you cannot call your extension method ToString as a default ToString already exists.

public static class ExtensionMethods
{
    public static string ToText(this int[][] value)
    {
          StringBuilder sb = new StringBuilder();
          for (int i = 0; i < value.Length; i++)
          {               
               for (int j = 0; j < value[i].Length; j++)
                   sb.AppendLine(value[i][j]);
          }
          return sb.ToString();
     }
}

You can read up on extension methods here http://www.dotnetperls.com/extension

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.