Hi TS,
Thanks for your feedback.
Yes, you statement is basically right. Actually, this is a concept issue,
has not much relation to with Windows Forms.
In .Net, all classes are referece type, only a few primitive types are
value type(such as int, double, and some structures). Reference type is
somewhat like a pointer in C language, it will point to some object(class
instance), if it did not refer any object, it will point to null. These
objects are allocated in heap. In .Net, there is no need for you to explict
free these objects, because there is GC in .Net to collect these memory in
heap.
Let's back to our issue, at runtime, if you created a button, you actually
created a button class instance, and there is a reference point to this
instance. When the reference count of this object drop to 0, it will be
suitable for GC to collect this object. When you add this button instance
to the Form.Controls property, there will be another reference point to the
button object, also, if you add the button object into HashTable, there
will be a third reference point to it.
To store the reference in Hashtable, you just got a convinient way to get
the button OBJECT's reference. This has nothing to do with the button
object self.
If your button is created at design-time, VS.net IDE will added a Form wide
reference for this Button instance as a private variable, like this:
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
Also, in the "Windows Form Designer generated code"'s InitializeComponent()
method, VS.net IDE will add the following code for the button:
this.button1 = new System.Windows.Forms.Button();
this.button1.Location = new System.Drawing.Point(104, 224);
this.button1.Location = new System.Drawing.Point(104, 224);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 40);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
So the button instance is already created InitializeComponent() method,
which is called by Form1's constructor, button1 reference is already point
to this created Button instance.
Hope this makes sense to you.
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.