Custom component and the Dispose method

P

pete

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);

}
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top