Read without ads and support Scribd by becoming a Scribd Premium Reader.
 
 
6. The C# code in .Net Applications
In the previous chapter, we had explored all that was required to be learnt and discoveredabout C#, in order to understand the code generated by the Framework. A crucial point thatscreams for attention is, that any application built in Visual Studio.Net with Project Type asVisual C#, eventually is converted into C# code. In this chapter, we will build applications inVisual Studio.Net, and at every stage thereon, we shall explain the code generated by the .NetFramework. We could have written the same code manually, but it certainly gives ourproductivity a boost, if the code is automatically generated by the system.Let us revert back to Visual Studio.Net, to create a new application. As always, click on menuFile - New - Project. In the dialog box, select Visual C# Project in Project Type pane, andWindows Application in the Template pane. Enter the name z10, and ensure that the locationis set to c:\mukhi. On pressing F5 to run the application, you will come across a completely blank window.When you navigate into the folder c:\mukhi\z10, you will encounter a large number of files,created by this framework. One of the many files created, is named as Form1.cs. This is similarto the .cs file, which was created in the previous chapter. Now, run the compiler using thecommand
>Csc Form1.cs
 The compiler creates an exe file named Form1.exe, which when executed, reveals the samewindow, as is seen with the F5 command. The program Form1.cs has been generated by VisualStudio.Net, which internally calls the C# compile and creates the executable file. Pressing F5runs the executable. Let us now attempt at understanding the code, that gets generated inForm1.cs.
Form1.csusing System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace z10{/// <summary>/// Summary description for Form1./// </summary>public class Form1 : System.Windows.Forms.Form{/// <summary>/// Required designer variable./// </summary>private System.ComponentModel.Container components = null;public Form1()
 
{//// Required for Windows Form Designer support//InitializeComponent();//// TODO: Add any constructor code after InitializeComponent call//}/// <summary>/// Clean up any resources being used./// </summary>protected override void Dispose( bool disposing ){if( disposing ){if (components != null){components.Dispose();}}base.Dispose( disposing );}#region Windows Form Designer generated code/// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>private void InitializeComponent(){this.components = new System.ComponentModel.Container();this.Size = new System.Drawing.Size(300,300);this.Text = "Form1";}#endregion/// <summary>/// The main entry point for the application./// </summary>[STAThread]static void Main(){Application.Run(new Form1());}}}
We hope to be able to refresh your memory by reiterating that, the above program has beenwritten by the C# compiler, and not by us. The file reveals a large number of statementsbeginning with the term 'using'. For those of you who tuned in late, the 'using' statements areinserted to ease the typing effort. The name of a namespace is provided after the 'using'keyword, so that the namespace is not required to be inserted before each 'class.function name'combination. As the project has been named z10, the entire C# program is depicted asenclosed within the namespace of z10. Since the solitary role of a namespace is to ensure that
 
the class names in one project, do not clash with class names in the other projects, it cansafely be overlooked.You may recollect that in the last chapter, we had gathered that any line beginning with threeslashes, is an XML comment. This program appears very large, due to the comments insertedat every possible nook and corner. More often than not, comments are not of much utility,since they are computer generated; and thus, lack the essential human touch. In future, wewill strip-off all comments, before displaying the code.A class known as Form1, which is derived from the class Form, contains all the code requiredfor the current project. In spite of employing the term 'using', which includesSystem.Windows.Forms, the code generated, still contains the full name of the class. It isabsurd and illogical, but then; computers are not expected to generate code that makes perfectsense. In the first place, an instance variable called 'components' in the class, has beeninitialized to 'null', which represents no value. The 'private' modifier limits the access only tothe members of the current class. The Run function, located in the Main function, is called after passing it an object, which is aninstance of Form1. The newly created object calls the constructor of the Form1 class, and then,it calls the Run function. The constructor simply calls a function named InitializeComponent.But prior to this, study the square brackets above the Main function closely. It represents an'attribute'. Attributes have a range of functions, which undertake certain tasks, such as,determining the wherewithal of generating the code with the aid of the compiler, ordocumenting the classes in the executable file, etc. We shall not venture into the details of theattribute [STAThread], however, for those burning with curiosity, it suffices to say that the fullform of STA is Single Threaded Apartment. It implies that, once a window is created in athread, it will not be permitted to switch to another thread. If you are unable to comprehendthe finer nuances of the above statement, then cheer up, you have company!It is in this function, that all the form widgets get initialized. We employ this function, only inthe event of initializing the object components. The computer program that generated the code,prefixed all the instance variables with the term 'this'. In this case, eliminating the term 'this',does not affect the outcome. The class Form has a large number of instance variables, such asSize and Text, which we are entitled to be used in the class Form1. The Size variable or Size property, determines the size of the initial window, while the Textproperty decides the title of the window. The 'this' keyword is optional. A point to be noted isthat, all properties have a default value. No sooner do we change a single property in theProperty window, a line gets inserted in the InitializeComponent function. The Size property isinitialized to a Size object, which is passed the width and height of the window in pixels. Thus,a GUI is used to build applications and all actions get converted into C# code. The next function called Dispose, can only be called by the system, as and when it is required.A programmer does not enjoy the facility of calling it manually. We are completely aware aboutthe circumstances under which a constructor gets called. Further, we also know unequivocally,that the Dispose function gets called, when the resources in the program are to be disposed off.But, we are not aware as to when this disposal takes place; in other words, when is an objectgoing to die. In C#, unlike C++, there is no control at all, over the termination of an object. TheGarbage Collector is solely responsible for removing an object from memory; thereby, shieldingthe programmer from carrying out any memory management chores. The Dispose function canalso be ignored, since it merely calls the Dispose function from the Components object and thebase class. Hence, the use of keyword 'base' can be explained. In our next display, we do notintend to expose you to the Dispose function any longer.
Search History:
Searching...
Result 00 of 00
00 results for result for
  • p.
  • Notes
    Load more