الاثنين، 14 أبريل 2008

c # c

Decide between Abstract class and Interface If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members. If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes. Below is how interface and abstract classes are used in project. In order to define a interface we use the interface keyword and to define abstract class we use the abstract keyword. public interface iEmployee { } public abstract class clsEmployee : iEmployee { public clsEmployee() { } } Note :- 60 % of the software projects do not use OOPS or are either far from the reality of OOPS.In this project book have a close look ah the chat application and job site of how the classes are made. Chapter 3 Understanding the IDE The IDE Humans are lazy, oh yes and programmers are humans. If you go back to the good old times, compiling was luxury. We still remember people first writing the whole code on a book and then sitting on the computer. To display a simple window or a command button itself was a huge task. To develop as simple windows UI (User Interface) it use to take hours. In order to increase programmer’s productivity and reduce redundancy RAD (Rapid Application Development) tools where introduced. In today’s competitive world no language can come up if there is no RAD tool companion with the language. JAVA has ECLIPSE, VB6, VC++ and VFP had Visual studio and so on. In the same way C# and VB.NET have Visual Studio.NET as the RAD tool . As this is a C# Page 69 of 215
projects books lets get introduced to this beautiful tool which will help us to develop C# projects effectively and in more speedy manner. When you try to create new project using VS.NET you will see something as show in the below figure. On the right hand side pane you can see different project templates like Windows application, Class library etc. But let’s not get confused by all the templates at once. Concentrate on the two important templates “Windows Application” and “ASP.NET Web application”. On the left hand side of the pane you can see you can create these templates using any of the languages. Note: - This book will be using C# as the language to demonstrate all projects. The only reason we have chosen C# is because of the huge concentration that Microsoft gives to it. Figure 3.1: - Different types of projects in Visual Studio Note: - Windows applications are programs which run under Microsoft windows operating system. For instance word, excel or a simple MSN chat application are some examples of windows application. Web Application is accessed using a browser over a network such as intranet or internet. Page 70 of 215
Figure 3.2: - Complete IDE view When you open a new project using Visual Studio.NET you will see a complicated environment as shown in the above figure. Don't panic....You remember the first time you took a drilling machine to put nails on the wall and found how complicated it was as compared to a simple hammer. But surely as time passed by you praised the effectiveness and speed by which you can use drilling machine rather than hammer. The above whole environment is called as the IDE (Integrated development environment). The major part of the whole IDE is occupied by the designer pane (its marked with double head arrows as shown above).On the left hand side you can see the tool box which has reusable components like text boxes , combo box etc which you can drag on the designer pane. On the right hand side of the pane you can see the solution explorer. Solution explorer gives view of your files and folder structure in your project. In order to build the project you can click on the build option. You have two options one is you can build the project or you can build the solution. Solution is a combination of one or more projects (We will come to projects concept when we actually develop the project). Page 71 of 215
Figure 3.3: - Compile, Debug and Run C# Projects In order to run the application you will need to press F5 or click on Debug – Start. If you get the above screen you have already made your first C# project. Definitely it has no practical importance….But yes you did it. Windows form Code walk through When you created the above application you will see the designer has some code which is already present by default. Let’s try to understand this sample code as we will frequently come across it. Below figure depicts the pre-defined code inserted by the designer. Page 72 of 215
Figure 3.4: - Code walk through for Windows application A Windows Form is a Windows Form because it inherits its functionality from the System.Windows.Forms namespace. This namespace is used across all .NET languages that must implement Windows Forms applications. All windows application will follow the above sequence defined in the image. The main entry point of the application is the “static void main ()” method. You can also see “[STAThread]” attribute marked on the method. This attribute is especially meant for COM Interop used in application. The “[STAThread]” ensures that the thread in which the application runs is single threaded. With single threaded model we can ensure that communication with COM components works perfectly well. COM components run under single thread apartment and it’s essential that the entity using him should also run in a single thread. In the above figure we have also mapped the sequence with code snippets. So you can easily visualize that there are four important sequences and below is a detail explanation of the same: - The first code snippet to execute is the main entry method i.e. Static void Main (). This is the entry point for any windows application. Second section which is executed is the constructor of the form or UI. Every form is a class. The constructor in turn calls the InitializeComponent() method. The InitializeComponent () section is the section where you can do all initialization activity. Because InitializeComponent () is called by the constructor it will be executed only once when the form object is created. InitializeComponent () section can also be used to bind the event handlers with the method names. Page 73 of 215
The InitializeComponent () section then triggers the Form1_Load event. The IIS In this section we will discuss about IIS (Internet Information Service) which is basically an answer to Microsoft’s web server. As you see from below figure the web server sits between the websites & end user. Here is the process how it actually interacts with the server: - User request for a website from his browser. for e.g. : - www.questpond.com The webserver processes the page and sends back the HTML/ASP page back to the end user. Figure 3.5 - IIS and user interaction Web Application Code walk through Page 74 of 215
Figure 3.6: -.NET and IIS Now we have understood the fundamental of IIS. Let us try to understand how VS.NET interacts with the IIS. As you can see in the below figure. The developer opens the VS.NET and request visual studio to create a web project. VS.NET then creates a project file on IIS. From the below figure you can see visual studio creates project which is default named as “webapplication1” with some default files. The default important files created are: - WebApplication1.csproj Global.asax AssemblyInfo.cs Web.config Webform1.aspx.cs / resx / .aspx Figure 3.7: - Different files in web application Now let’s try to understand every individual file in more detail: Webapplication1.csproj file: - This file is like a index file and also defines which file is present in a visual studio project. We always open this file using VS.NET and that in turn loads all the other files. As you can see in the below figure we have opened “csproj” file in notepad you can see that it is actually pointing towards other file such as assemblyinfo.sys etc.In short, it is a file which says what kind of file are present in a project. Page 75 of 215
Figure 3.8: - CSproj data Global.asax file: - It allows you to write code in application level event. If you open global.asax file you will find the following events. Try to open in notepad you will come across many events. So let’s understand the events in more detail fashion. The first four events are very important rest all are included for knowledge sake which are as follows : - Application_Start: This event happens once in the beginning when any user hits IIS application. Once this event has occurred it does not happen again for every user. Application_End: In end when application ends this event happens. It is used to cleanup memory and variables. When we say application end it can Page 76 of 215
happen both when the server restarts, when IIS resets and when you change the global.asax. Session_Start: This event happens when any new user visits the websites. Session_End: This event actually happens when user close the browser. The other events are Application_Init, Application_Disposed, Application_Error, Application_BeginRequest etc. Figure 3.9:- Global.asax 2) assemblyinfo.cs file : - This file basically contains all information about assembly such as Company, Configuration, Description, Version etc.Try to open assemblyinfo.cs in notepad you will get more attributes like below figure Page 77 of 215
Figure 3.10:-assemblyinfo.cs file 3) Web.config file : - This is the most important file in the web project. This is used to define settings for ASP.net application. It is basically in XML format. Lets try to open in notepad and understand in detail the web.config file which are as follows: - Web.config file is a perfect place to save application configuration settings in ASP.net.For instance, if we consider database connection string it can be placed in web.config file and can easily access in ASP.net.and connection can also be changed to other database server if required. Lets have a look on example of Web.config file: - Page 78 of 215
Figure 3.11:- The AppSettings tag As you observe a new section has added to web.config . define custom settings of ASP.NET application. The child element of the adds value pair and can easily be accessible from your application as follows: - ConfigurationSettings.AppSettings("sConnectionString"). section of web.config file is used to handle web application errors. Lets try to understand the following syntax of customErrors. The default URL in our example is www.questpond.com and 3 modes which decides the status ‘ON’, ‘OFF’ and ‘REMOTEONLY’ means Enabled, Disabled and Enabled for remote machines respectively. ERROR STATUS CODE generate the error number in this case we have no. 404. So it will redirect to default URL www.questpond.com Figure 3.12:- CustomErrors Section Another important section of web.config file is settings. Here identity is required to access the application. For instance, ASPNET Windows account is required to to access ASP.NET resource, this account is by default. Figure 3.13:- Impersonation Page 79 of 215
Impersonate is act by which application (ASP.NET) is executed by authenticated (ASPNETWindows) account while accessing is provided by IIS. Here we can specify an specific account instead of ASPNET Windows account by changing the syntax as follows Figure 3.14:- Impersonation for Windows Account The ASP.NET Web.config file can control many more settings like Session State, Tracing and Authorization & Authentication etc. Below is the figure, where it shows ASP.net lifecycle in action. In the below figure you will find two sections the left section is the sequences of events happens and the right section shows actual coding which happens behind ASP.net. Towards left if we look there are four events page.init event: - In this event we attach the event handlers with the controls. page_load event- In this event we basically loads the controls. Binding of data with the UI can be done in the Page_load event. Control event: - This is a basic event in which triggering or firing is done by the user. For example : - In below figure we have a button click event. page_unload event : - In this event we can write clean up code to close the page. The whole point is to understand the sequence of events to put right code in right event. As we dive in to projects you will easily able to map the below events. Page 80 of 215

ليست هناك تعليقات: