WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

C# Object Serialization
Pages: 1, 2, 3, 4

Class Variables

Some of the class variables are given below.


    private ArrayList shapes = new ArrayList();
    private enum shapeType
    {
      line, rectangle, ellipse,
    }

    private shapeType drawnShapeType = shapeType.line;
    private Point startPoint;
    private String filename;

The shapes ArrayList contains all shapes drawn on the form's surface. The shapeType enumerator contains the three types of shapes. The drawnShapeType denotes the type of shape that the user chooses to draw. The startPoint variable is a System.Drawing.Point indicating the point at which the user clicks the mouse button to start drawing. The filename is the name of the file to which to serialize the shapes.

The OnPaint Method

The OnPaint method is overriden to draw all of the shapes on the form's surface. It first obtains the Graphics object of the form.


      Graphics g = e.Graphics;

It then creates an Ienumerator from the GetEnumerator method of the shapes ArrayList.


      IEnumerator shapeEnum = shapes.GetEnumerator();

Next, it iterates through all members of the shapes ArrayList in a while loop, casts each member into an IShape object, and calls its Draw method.


      while (shapeEnum.MoveNext()) 
      {
        IShape shape = (IShape) shapeEnum.Current;
        shape.Draw(g);
      }

The toolbar1_ButtonClick Event Handler

This event handler is invoked every time the user clicks a toolbar button to select a shape to draw. The event handler checks which button is clicked and updates the value of drawnShapeType.


      if (e.Button==lineToolBarButton)
        drawnShapeType = shapeType.line;
      else if (e.Button==rectangleToolBarButton)
        drawnShapeType = shapeType.rectangle; 
      else
        drawnShapeType = shapeType.ellipse; 

The this_MouseDown Event Handler

The form's MouseDown event is wired to the this_MouseDown event handler. What it does is simple: it checks if the user clicks the mouse's left button, and, if so, assigns the startPoint class variable with the click point.


      if (e.Button==System.Windows.Forms.MouseButtons.Left) 
      {
        startPoint = new Point(e.X, e.Y);
      }

The this_MouseUp Event Handler

This event handler is invoked when the user releases the mouse button. It first checks if the released button is the left button of the mouse, and if yes, creates a shape. The shape created depends on the value of drawnShapeType. If its value is ShapeType.line, then a Line object is constructed. If the value is ShapeType.rectangle, a Rect object is created. If the value is ShapeType.ellipse, an Ellipse object is created. The newly-created shape is then added to the shapes ArrayList.

The Save Method

The Save method serializes the shapes ArrayList into a file. The part that does that is as follows:


        Stream myStream ;
        myStream = File.OpenWrite(filename);
        if (myStream != null)
        {
          IFormatter formatter = new BinaryFormatter();
          // serialize shapes
          formatter.Serialize(myStream, shapes);
          myStream.Close();
          return true;
          }
        }

It first creates a Stream object, and then a BinaryFormatter object. Serializing the shapes ArrayList is done by calling the Serialize method of the formatter.

The OpenDocument Method

The OpenDocument method de-serializes the persisted ArrayList object into shapes. The code that does the de-serialization is as follows:


        Stream myStream = openFileDialog.OpenFile();
        if (myStream != null)
        {
          IFormatter formatter = new BinaryFormatter();
          shapes = (ArrayList) formatter.de-serialize(myStream);
          myStream.Close();
          this.Refresh();
        }

It first constructs a Stream object and then a BinaryFormatter object. To de-serialize an object, it calls the formatter's de-serialize method.

Summary

In this article, you have learned the object serialization technique in the .NET Framework. You have also seen how this technique finds application in a vector-based drawing application.

Budi Kurniawan is a senior J2EE architect and author.


Return to .NET DevCenter