Creating Objects in the Sample Application


Now it's time to enhance the sample application a little. Let's add the code that creates instances of the ToDoItem class and the ToDoList class.

To create objects in the sample application:

  1. From the Solution Explorer, double-click the file entertask.aspx . You should see the Enter Task Web page appear ( Figure 2.51 ).

    Figure 2.51. Double-clicking on an aspx file in Solution Explorer opens the graphical form for editing.

    graphics/02fig51.gif

  2. Click the Add Task button. The code editor should open up and add the btnTask_Click function .

  3. Inside the function type the code in Figure 2.48 . (We'll add more to this function later in the chapter.)

  4. Back in Solution Explorer right-click on the file Global.asax and choose View Code from the context menu.

  5. Locate the function Application_Start . Within the curly brackets, add the code for Figure 2.49 .

graphics/tick.gif Tips

  • The application won't run yet. The code you have just added creates an instance of the ToDoItem class when you click the Add Task button. It then calls SetInfo passing the text from the item and description textboxes. However, you have not added all the functions needed to each class.

  • The code you added to Global.asax requires a little explanation, namely, what in the world is this Global.asax file? Well, when you execute your Web application, the ASP.NET framework creates an application object. The application object is an instance of the class defined in Global.asax . The first time the application is run, ASP.NET calls the Application_Start function. If you stop the Web server (not if you close the browser), or if you update any files in your project, the Application object will call the Application_End function. In Application_Start we are creating a ToDoList object to be shared by all users of the Web site. You haven't added the code yet to make it shared, but that is the intent, and the reason we are adding the code to Application_Start .




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net