0

Another question about arrays. When i create an array and fill it from keyboard, the console shows several System.Object[] instead of the value I set. For example, if i create a [5] array i get 36 System.Object[] instead of my 5 values. Why is that? This is a code I´m using:

 object[] row = new object[5];

 public void fill()
    {
        for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Set the values" + (i+1));
                row[i] = Console.ReadLine();
            }
        Console.ReadKey();
    }

 public void view()
    {
        Console.WriteLine("The values are:");
        for (int i = 0; i <= 5; i++)
        {
            Console.Write("\n");
            for (int j = 0; j <= 5; j++)
            {
                Console.Write(row);
            }
        }
        Console.ReadKey();
    }

    static void Main(string[] args)
    {
        Program objeto = new Program();

        objeto.fill();
        objeto.view();

        Console.ReadKey();
    }

There are no error messages, but when on screen i get this afer setting the 5 values:

The values are:

System.Object[]System.Object[]System.Object[]System.Object[]System.Object[]System.Object[] System.Object[]System.Object[]System.Object[]System.Object[]System.Object[]System.Object[] System.Object[]System.Object[]System.Object[]System.Object[]System.Object[]System.Object[] System.Object[]System.Object[]System.Object[]System.Object[]System.Object[]System.Object[] System.Object[]System.Object[]System.Object[]System.Object[]System.Object[]System.Object[] System.Object[]System.Object[]System.Object[]System.Object[]System.Object[]System.Object[]

What can I do?

3 Answers 3

6

I believe it should be row[j]

for (int j = 0; j <= 5; j++)
{
    Console.Write(row[j].ToString());
}
Sign up to request clarification or add additional context in comments.

1 Comment

You are correct, should be "j" instead of "i". OMG!!! C´mon C#, it´s just a letter!! :D Thank you for your answer :)
4

Because row is an array, the default .ToString() method is printing the type, which is System.Object[].

Try writing the value of row[j] instead the value of row.

Except, you seem to have two loops that almost seem like you are wanting to print a multidimensional array, which your array isn't. So the outer for loop is unnecessary, unless you are wanting to print 5 rows of the same data.

5 Comments

Yeah, you are correct, i made the changes you mention and now i get on screen the values, but am recieving an IndexOutOfRangeException after the array is shown. T_T
Since arrays start from zero, your condition in for loop should be j <= 4 or j<5
What @Sunny says is correct. You either need to change your condition to j <= 4 or j < 5.
Yeah, I see it, and you are all right, just removed the extra "j" loop and modified both loops to i<=4 and now works perfectly, thank you all for your time. I´m learning more in a day here at stackoverflow that the last motnh at school :D
@Gabbo2483 - Yeah, remember that in a for loop, the loop continues until the condition is true. Which means, when you had j <= 5, then it continues through the loop an extra iteration then what was needed.
0

Change your view to this...

public void view()
    {
        Console.WriteLine("The values are:");
        for (int i = 0; i <= 4; i++)
        {
            Console.Write("\n");
            Console.Write(row[i]);
        }
        Console.ReadKey();
    }

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.