WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

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

Example 1: Classes for objects that can be serialized.


using System;
using System.Drawing;
using System.Runtime.Serialization;

namespace MyCSharp
{
  public interface IShape 
  {
    void Draw(Graphics g);
  }

  [Serializable]
  public class Line : IShape
  {
    Point startPoint, endPoint;
    public Line(Point startPoint, Point endPoint) 
    {
      this.startPoint = startPoint;
      this.endPoint = endPoint;
    }
    public void Draw(Graphics g) 
    {
      g.DrawLine(Pens.Black, startPoint, endPoint);
    }
  }

  [Serializable]
  public class Rect : IShape
  {
    Rectangle rect;
    public Rect(Rectangle rect) 
    {
      this.rect = rect;
    }
    public void Draw(Graphics g)
    {
      g.DrawRectangle(Pens.Black, rect);
    }
  }

  [Serializable]
  public class Ellipse : IShape
  {
    Rectangle rect;
    public Ellipse(Rectangle rect)
    {
      this.rect = rect;
    }
    public void Draw(Graphics g)
    {
      g.DrawEllipse(Pens.Black, rect);
    }
  }

}

The Line class' constructor accepts two Point objects. The Rect and Ellipse classes' constructors accept a System.Drawing.Rectangle object. All classes implement the Draw method from the IShape interface. Now, let's shift our attention to the form for the application. The graphical user interface for the form of this application is shown in Figure 1.

Figure 1.
Figure 1. Drawing application that illustrates the object serialization technique.

As shown in Figure 1, there is a toolbar with three toolbar buttons, each of which is used to draw one of the three shapes. To select a shape to draw, click one of these buttons. The client area of the form contains nothing; however, the form wires the MouseDown and the MouseUp events to the this_MouseDown and this_MouseUp event handlers, respectively.

To draw a shape, the user clicks on the client area, drags, and releases the mouse button. The shapes drawn are added to an ArrayList called shapes. To draw the shapes, we override the form's OnPaint method. The OnPaint method iterates through the elements of the shapes ArrayList, casts each member of the ArrayList to IShape, and calls its Draw method. The Draw method is called by passing the Graphics object of the form, effectively making the drawing happen on the form's surface.

The only menu item, File, contains the following menu items: New, Open, Save, Save As, and Exit. The functions of these items are:

  • Clicking the New menu item clears the shapes ArrayList and the form's surface.
  • Clicking the Open menu item shows an Open File dialog, from which the user can select a file containing an ArrayList full of the previously-drawn shapes. Each shape will then be redrawn on the form.
  • Clicking the Save menu item serializes the shapes ArrayList into a file with the specified filename. If the filename is null, the application will try to call the SaveAs method.
  • Clicking the Save As menu item shows a Save File dialog. The user can choose the directory to which to save the object file, and select a filename.
  • Clicking the Exit item causes the application to exit.

The form class for this application is shown in Example 2.

Pages: 1, 2, 3, 4

Next Pagearrow