Hi Guys
NP125 activation
In the following program activation of System.Windows.Forms from “Add Reference…” and inclusion of that in the namespace is necessary for program to execuate but as long as System.Drawing is concerned only activation is necessary, not needed that to include in the namespace for the program to execuate. What is the reason for that?
Please explain.
Thank you
//p332
using System;
using System.Windows.Forms;
using System.Drawing;
//descends from the From class
public class WindowWithButton : Form
{
Button button1 = new Button(); //Button object named button1
public WindowWithButton() //constructor
{
this.Size = new System.Drawing.Size(300, 300);
//Size field for the Form
this.Text = "Window Object With Button";
//Text field for the Form
button1.Text = "Press"; //Text field for the Button
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1 });
/*
Controls.AddRange() method to indicate that the Button
you created will become one of the Form's usable controls.
*/
this.button1.Location = new System.Drawing.Point(100, 50);
//locate the Button at position 100, 50 on the Form
}
//Main() method to execuate the application
public static void Main()
{
Application.Run(new WindowWithButton());
}
}
AlanPosted Aug 16, 2008, 6:39 AM
The reason why the program still works, even if you remove the 'using System.Drawing' line, is because the two classes Size and Point which are contained in that namespace are used in fully qualified form in the program i.e. System.Drawing.Size and System.Drawing.Point
If you'd just used Point and Size, then you would have needed the using directive, otherwise the compiler would have complained that it couldn't find them.
Ryan AlfordPosted Aug 16, 2008, 10:31 AM
Posted Aug 16, 2008, 9:14 AM
Thank you for your explanation.
Therefore System.Windows.Forms (highlighted in gray) is redundant because it has been mentioned in the using statement.
Even if size field of the form (highlighted in green) is commented out program is executing. I wish to know whether particular size field is default behaviour of the from.