P
pete
Hi Guys,
Apologies for the long post.
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?
Cheers, Pete
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);
}
}
}
Apologies for the long post.
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?
Cheers, Pete
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);
}
}
}