Dinnea
Dinnea

Reputation: 13

Why does binary reader put all the content of my 2d array into a single array row?

I am trying to use binary writer/reader to serialize and deserialize a 2D array, this is the code I am using for this:

public class Connect4BoardData : ASerializable
{
    public int[,] board = new int[6, 7] { 
                                                    { 0, 0, 0, 0, 0, 0, 0},
                                                    { 0, 0, 0, 0, 0, 0, 0},
                                                    { 0, 0, 0, 0, 0, 0, 0},
                                                    { 0, 0, 0, 0, 0, 0, 0},
                                                    { 0, 0, 0, 0, 0, 0, 0},
                                                    { 0, 0, 0, 0, 0, 0, 0} };
    public int rows = 6, columns = 7;
    public override void Serialize(Packet pPacket)
            {
       
                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < columns; j++)
                    {
                        pPacket.Write(board[i, j]);
                    }
                }
            }
            public override void Deserialize(Packet pPacket)
            {
                rows = 6;
                columns = 7;
                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < columns; j++)
                    {
                        board[i, j] = pPacket.ReadInt();
                    }
                }
            }
}

When I eventually try to read it however, it just doesn't work. I seem to get a [42, 1] array instead, and I honestly do not understand why it happens. If I try array.GetLength(1), I get an "index out of bounds exception for some reason, but I am still able to access array[x, 0], where x is 0 to 41. Moreover when I attempted to write and read columns and rows in the methods, it read them as

 rows = 6, columns = 70000000000000 ... 

The code to write and read ints is as follows:

public void Write (int pInt)                            {       writer.Write(pInt);    }
public int ReadInt()                                    { return reader.ReadInt32();   }

with writer and reader assigned like this:

    private BinaryWriter writer;    //only used in write mode, to write bytes into a byte array
    private BinaryReader reader;    //only used in read mode, to read bytes from a byte array

    /**
     * Create a Packet for writing.
     */
    public Packet()
    {
        //BinaryWriter wraps a Stream, in this case a MemoryStream, which in turn wraps an array of bytes
        writer = new BinaryWriter(new MemoryStream());
    }

    /**
     * Create a Packet from an existing byte array so we can read from it
     */
    public Packet (byte[] pSource)
    {
        //BinaryReader wraps a Stream, in this case a MemoryStream, which in turn wraps an array of bytes
        reader = new BinaryReader(new MemoryStream(pSource));
    }

EDIT: Serialize and Deserialize is called like this:

public void Write (ASerializable pSerializable)         {
            //write the full classname into the stream first
            Write(pSerializable.GetType().FullName);
            //then ask the serializable object to serialize itself
            pSerializable.Serialize(this); 
        }
public ASerializable ReadObject() 
        {
            //get the classname from the stream first
            Type type = Type.GetType(ReadString());
            //create an instance of it through reflection (requires default constructor)
            ASerializable obj = (ASerializable)Activator.CreateInstance(type);
            obj.Deserialize(this);
            return obj;
        }

Everything else I write and read works fine, 1D arrays, strings, booleans. I hope I provided enough info and context, but if not let me know and I'll update the question ASAP, its quite a large assignment. And one more thing, I am not going to be using BinaryFormatter for this, its an assignment that requires BianryWriter/Reader, so please don't recommend this in answers

Thank you!

Upvotes: 0

Views: 51

Answers (0)

Related Questions