DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Design Question

  1. #1
    Michael Guest

    Design Question


    I have a small class and I want to know if it need
    a Dispose Method and Finallizer?

    Why or why not?

    Michael Wassermann

    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    using RigWatch.Framework;

    namespace RigWatch.RigChart {

    public enum PlotWidth { Thin = 1, Medium, Thick }

    /// <summary>
    /// Summary description for Trace.
    /// </summary>
    public class Trace {

    #region Instance variables
    private Channel _channel = null;
    private Color _color = Color.White;
    private DashStyle _lineStyle = DashStyle.Solid;
    private PlotWidth _plotWidth = PlotWidth.Thin;
    private int _currentSize = 0;
    private float[] _values = null;

    #region Constructors
    public Trace() {
    BufferSize = 200;

    public Trace(int bufferSize) {
    BufferSize = bufferSize;

    public Trace(Channel channel, int bufferSize) {
    _channel = channel;
    BufferSize = bufferSize;

    #region Properties
    public Channel Channel {
    get { return _channel; }
    set { _channel = value; }

    public float Value { get { return _channel.Value;
    } }
    public string Name { get { return _channel.Name;
    } }
    public string Units { get { return _channel.Units;
    } }
    public float ScaleMin { get { return _channel.ScaleMin;
    } }
    public float ScaleMax { get { return _channel.ScaleMax;
    } }
    public int Precision { get { return _channel.DisplayPrecision;
    } }

    public Color PlotColor {
    get { return _color; }
    set { _color = value; }

    public DashStyle LineStyle {
    get { return _lineStyle; }
    set { _lineStyle = value; }

    public PlotWidth PlotWidth {
    get { return _plotWidth; }
    set { _plotWidth = value; }

    public int BufferSize {
    get { return _values.Length; }
    set {
    float[] temp = null;
    if (_values.Length > 0) {
    temp = _values;

    _values = new float[value];

    if (temp != null && temp.Length > 0) {
    Array.Copy(temp, _values, temp.Length);

    public int CurrentSize {
    get { return _currentSize; }
    set { _currentSize = value; }

    public float[] History {
    get { return _values; }

    public override int GetHashCode() {
    return _channel.IDNumber;

  2. #2
    PWilmarth Guest

    Re: Design Question

    Garbage collection is automatically done in C#. If you want to release system
    resources, you can call the garbage collector or the dispose method. But
    programmatically, you have no control over when the collector actually disposes
    of the object.

  3. #3
    PWilmarth Guest

    Re: Design Question

    I'm also looking at Jeffrey Richter's book, Applied Microsoft .NET Framework
    programming. He's got a rather nice explanation of using finalize and dispose
    methods, and the "whys and wherefores" of garbage collection, including garbage
    collection using threads. For lots of reasons, I recommend this book to
    people trying to figure out .NET programming.

  4. #4 Guest

    Re: Design Question

    Anytime you have resources that may be blocked from other clients that may
    use your object you should think about implementing IDisposable. IE a file
    at class scope that may require access by multiple clients. If the client
    that gets the object holding the file first and does not release it then
    no clients will have access to update the file. It looks as though your Channel
    objects may fall in this category. I have not experienced with channels
    yet to say what the capability is to modify them.

    You could wrap your Channel object in a singleton that all clients have access
    to and the Channel never changes. In this case it would not be held as class
    scope and clients can read and change accordingly provided you provide the
    correct synchronization.

  5. #5
    Michael Guest

    Re: Design Question


    Sounds like a winner!

  6. #6
    Michael Guest

    Re: Design Question

    >I have a small class and I want to know if it need
    >a Dispose Method and Finallizer?
    >Why or why not?

    The general rule of thumb that we have used in implementing IDispose requires
    an answer to two simple questions:
    Does your component use rare resources? (ie. database connections, file
    resources, etc)
    Does your component use unmanaged resources?

    If the answer is yes then it is recommended that you implement IDispose and
    make your component disposable. As I understand it, what happens is this:

    garbage collection (GC) will process objects on the heap that have been
    there awhile. The process of eliminating the object is to use the Finalize

    Prior to doing this, GC looks for any objects that expose IDispose. If
    so, the Dispose method is called first before any other objects Finalize
    is called. Your code can then dispose of the resources and as an added benefit,
    you can signal to GC that you do not need to have your Finalize method called.

    This is a means of eliminating (to some degree) the unknown time an object
    lives until GC finally decides to destroy it. GC give priority to IDispose
    over Finalize. HOWEVER: If IDispose is abused, the end result is you may
    slow down the performance of the entire system. So it has to be carefully
    considered before it is used.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center