i created a class that i want to serialize each time a move is made on the chessboard.

class ClassMoveToSerialize
    {
        int[] StartPosition;
        int[] EndPosition;
        Dictionary<int[], int[]> serialized;

        public  void DataToSerialize(int rowStartSerialize, int columnStartSerialize, int rowEndSerialize, int columnEndSerialize)
        {
       
            MessageBox.Show("" + (number = int rowEndSerialize+columnEndSerialize));
//it isnt finished, i am only testing them right now.
        }
    }

i created two methods outside the class that will enable me to serialize and deserialize. (the methods are activated in the partial form class).

public static bool WasntSerialized;
  public void SerializeNow()
        {
            ClassMoveToSerialize serializeMeh = new ClassMoveToSerialize();
            FileStream f = new FileStream(@"K:\MiniSteam\ChessGame\Replays\Replays.dat", FileMode.Create);
            BinaryFormatter b = new BinaryFormatter();
            b.Serialize(f, serializeMeh);// i get the exception here
            f.Close();
        }

        public void DeSierializeNow()
        {
            ClassMoveToSerialize serializeMeh = new ClassMoveToSerialize();     
            FileStream f = new FileStream(@"K:\MiniSteam\ChessGame\Replays\Replays.dat", FileMode.Open);
            BinaryFormatter b = new BinaryFormatter();
            serializeMeh = (ClassMoveToSerialize)b.Deserialize(f);
            f.Close();
        }

i have another code in another class file that activates those methods.

if (WasntSerialized)
            {
                chess.DeSierializeNow();// when another move is made, this method should deserialize all the moves
                WasntSerialized = false;
            }
            toSerialize.DataToSerialize(rowStart, columnStart, rowEnd, columnEnd);
            if (!WasntSerialized)
            {
                chess.SerializeNow();// The move coordinates should be saved after this method invoked.
                WasntSerialized = true;
            }

why do i get the exception in the SerializeNow() method?

Add the [Serializable] flag to your ClassMoveToSerialize class if you haven't already.

Also, what exception are you getting?

{"Type 'WindowsFormsApplication1.Class2' in Assembly 'ChessBoardGame, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable."}


i modified my code a bit to look like this:

ClassMoveToSerialize serializeMeh = new ClassMoveToSerialize(new int[2], new int[2], new Dictionary<int[], int[]>(), new Class2());

        public void SerializeNow()
        {    
        
            FileStream f = new FileStream(@"K:\MiniSteam\ChessGame\Replays\Replays.dat", FileMode.Create);
            BinaryFormatter b = new BinaryFormatter();
            b.Serialize(f, serializeMeh);
            f.Close();
        }

        public void DeSierializeNow()
        {
            
            FileStream f = new FileStream(@"K:\MiniSteam\ChessGame\Replays\Replays.dat", FileMode.Open);
            BinaryFormatter b = new BinaryFormatter();
            serializeMeh = (ClassMoveToSerialize)b.Deserialize(f);
            f.Close();
        }

and the class to serialize:

[Serializable] 
    class ClassMoveToSerialize
    {
        int[] StartPosition;
        int[] EndPosition;
        Dictionary<int[], int[]> serialized;
        int number;
        int[] positions;
        Class2 codeClass;
        public ClassMoveToSerialize(int[] StartPosition2, int[] EndPosition2, Dictionary<int[], int[]> serialized2, Class2 class2)
        {
            this.StartPosition = StartPosition2;
            this.EndPosition = EndPosition2;
            this.serialized = serialized2;
            this.codeClass = class2;

        }
     
        public void MySavedPositions()
        {
            positions = codeClass.returnArrayPositons();// this function gives me all the positions of the array in the chess file code
            StartPosition[0]=positions[0];
            StartPosition[1]=positions[1];
            EndPosition[2]=positions[2];
            EndPosition[3]=positions[3];
            serialized.Add(StartPosition, EndPosition);
            MessageBox.Show("" + (number = StartPosition[0] + EndPosition[1]));
        }
    }

Ahhh I see it now.

Because your class named ClassMoveToSerialize contains a field to hold another class instance called Class2, that means Class2 also needs the serializable flag.

Add the [Serializable] flag to Class2 as well.

commented: Thanks for helping me with seriliazation +0

it worked. i didnt know that..
Dictionary<int[], int[]> serialized;
but now it tells me this is wrong:
serialized.Add(StartPosition, EndPosition);

cause same key was added. does a key to a dictionary has to be unique? should i not use a dictionary?

Yes, a key to a dictionary had to be unique.

i put this

[Serializable]
     public class Class2
     {
   int[] rightPositions;
 public void ExecuteAll(int rowStart2,int columnStart2,int rowEnd2,int columnEnd2)
        {
               rightPositions = g.giveMeRightPositions();// this function returns 4 numbers.  it returns this array :       //ArrayOfRightPositions = new int[] { rowStart, columnStart, rowEnd, columnEnd };//rowStart
}

and i added another function in class2

public int[] returnArrayPositons()
        {
            int[] positions = new int[4];

       
                positions[0] = 1;
                positions[1] = rightPositions[1];// it always gives me a nullException here and not at position[1]. does serialization resets the value of the position to null.?
                positions[2] = rightPositions[2];
                positions[3] = rightPositions[3];
      
            return positions;
        }

the exception happened before the data is serialized :(

okay , i fixed it by making the variables static (i think i have a visibility issue)..
that, array int[] rightPositions;, cant be seen by the other funcition, it simply wont update....

i dont know why :( so instead of an array, i used 4 static variables and it worked.

i think serialization works, cause i checked...

MessageBox.Show("" + (number = number+ StartPosition[1] + StartPosition[0] + StartPosition[2] + StartPosition[3]));

that message is inside the serialized class
i serialize and deserialize each time i move a position (if the move is legal, it serializes and deserializes), the number shows to increase.
does it mean serialization works?

To be honest I don't know if it's serializing and deserializing correctly. It appears that you are serializing to a file on the disk.

Clean your solution, delete the file at "K:\MiniSteam\ChessGame\Replays\Replays.dat" start the program in debug mode, do something to make the application serialize and see if the file is created. If so, it's serializing something.

How you use the data afterwards is up to your application's logic.

Since I really don't know what your ultimate goal is I can't really comment on if it "works" or not. The act of serializing "something" is working if the file is made and actually contains the data you need to use.

My suggestion would be to see if you can use the data in the file, if you can, it works, if not, it's not. :)

commented: thanks for the help +0

i got the serialization to work, but now it interfers me with the printing / reseting the board to the initial stage. see my other thread, Serialization issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.