1

I am currently receiving data from a client computer in the form of a string but I want it as a 2D array which is [25,3]. So I split it off into a single array first then tried to split that single array into a 2D array. However when I run the code I keep seeing an empty array anyone know why?

private void ClientReceiveData(object sender, ConnectedClient.NetDataEventArgs e)
{
    if (string.IsNullOrEmpty(e.Message) == false)
    {
        if (e.ID == 0)
        {
            result = Array.ConvertAll(e.Message.Split(new[] { ',', }, StringSplitOptions.RemoveEmptyEntries), Double.Parse);
            for (int i = 0; i < result.Length; i++)
            {

                resultarray2D[i % 3, i % 3] = result[i];
                Trace.WriteLine(resultarray2D);
            }
        }

        if (e.ID == 1)
        {
            answer = Array.ConvertAll(e.Message.Split(new[] { ',', }, StringSplitOptions.RemoveEmptyEntries), Double.Parse); 
            for (int i = 0; i < answer.Length; i++)
            {
                answerarray2D[i % 3, i / 3] = answer[i];
                Trace.WriteLine(answerarray2D);
            }
        }

Output: Output

2
  • 4
    You arent seeing an empty array, you are seeing exactly what .ToString() on an array outputs. If you want to see the contents of an array you have to access the individual values by index Commented Oct 11, 2018 at 18:27
  • 1
    ConvertAll does not work with multi-dimentions. Give us a single line of input, so we can judge what you're doing. And why do you use mod there? Commented Oct 11, 2018 at 18:33

2 Answers 2

1

This should work (I made some changes to what the person above said and implemented it in your code):

if (string.IsNullOrEmpty(e.Message) == false)
        {

            if (e.ID == 0)
            {
                result = Array.ConvertAll(e.Message.Split(new[] { ',', }, StringSplitOptions.RemoveEmptyEntries), Double.Parse);


                for (int x = 0; x < result.Length; x++)
                {

                    resultarray2D[x / 3, x % 3] = result[x];
                }
                int rowLength = resultarray2D.GetLength(0);
                int colLength = resultarray2D.GetLength(1);

                for (int i = 0; i < rowLength; i++)
                {
                    for (int j = 0; j < colLength; j++)
                    {
                        Trace.WriteLine(string.Format("{0} ", resultarray2D[i, j]));
                    }
                     Trace.WriteLine("\n");
                }

            }
Sign up to request clarification or add additional context in comments.

Comments

1

I keep seeing an empty array

No, you see what the default output of ToString on an array is, which is the name of the class. If you want to see the contents of the array, you ned to loop through and output the individual items:

int rowLength = arr.GetLength(0);
int colLength = arr.GetLength(1);

for (int i = 0; i < rowLength; i++)
{
    for (int j = 0; j < colLength; j++)
    {
        Console.Write(string.Format("{0} ", arr[i, j]));
    }
    Console.WriteLine();
}

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.