Custom component and the Dispose method

Discussion in 'ASP .Net' started by pete, Dec 4, 2004.

  1. pete

    pete Guest

    Hi Guys,

    Apologies for the cross post. I tried the webcontrols newsgroup but I didn't
    get a response and am pushed for time. The exam is looming :(

    Cheers, Pete

    I'm following a tutorial on developing components and am having trouble
    understanding some of the code. Its a simple RandomNumber component and
    although I've removed the comments (to save space) its pretty easy to
    follow.

    The parts I don't understand are as follows:

    1) Visual Studio add a System.ComponentModel.Container variable named
    components. Can you tell me what this is for?

    2) The tutorial I'm following says I should override the Diposing(bool)
    method. Despite providing the code it doesn't explain why I need to do this
    or explain exactly what is going on in the code. Can you explain this to me?


    using System;
    using System.ComponentModel;
    using System.Collections;
    using System.Diagnostics;

    namespace HelperClasses
    {
    public class RandomNumber : System.ComponentModel.Component
    {
    private int _minValue;
    private int _maxValue;

    private System.ComponentModel.Container components = null;

    public int MinValue
    {
    get
    {
    return _minValue;
    }
    set
    {
    _minValue = value;
    }
    }

    public int MaxValue
    {
    get
    {
    return _maxValue;
    }
    set
    {
    _maxValue = value;
    }
    }

    public RandomNumber(System.ComponentModel.IContainer container)
    {
    container.Add(this);
    InitializeComponent();
    }

    public RandomNumber()
    {
    InitializeComponent();

    MinValue=0;
    MaxValue=100;
    }

    private void InitializeComponent()
    {

    }
    #endregion


    protected override void Dispose(bool disposing)
    {
    if (disposing)
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose(disposing);

    _minValue = 0;
    _maxValue = 0;
    }


    public int GenerateRandomNumber()
    {
    Random r = new Random();
    return r.Next(MinValue, MaxValue);

    }
    }
    }
     
    pete, Dec 4, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SS
    Replies:
    2
    Views:
    473
    Trond-Eirik Kolloen
    Jul 3, 2003
  2. Leon
    Replies:
    12
    Views:
    11,600
    luxmit85
    Feb 12, 2008
  3. Antonio Concepcion
    Replies:
    3
    Views:
    2,772
    Antonio Concepcion
    Feb 17, 2005
  4. fabrice
    Replies:
    4
    Views:
    2,342
    fabrice
    Nov 15, 2006
  5. pete

    Custom Component : Dispose Method

    pete, Dec 3, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    114
Loading...

Share This Page