1. Trang chủ
  2. » Công Nghệ Thông Tin

Defining''''ve Pairs Classing ppt

6 35 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Nội dung

Defining Operator Pairs Some operators naturally come in pairs. For example, if you can compare two Hour values by using the != operator, you would expect to be able to also compare two Hour values by using the == operator. The C# compiler enforces this very reasonable expectation by insisting that if you define either operator== or operator!=, you must define them both. This neither-or-both rule also applies to the < and > operators and the <= and >= operators. The C# compiler does not write any of these operator partners for you. You must write them all explicitly yourself, regardless of how obvious they might seem. Here are the == and != operators for the Hour struct: struct Hour { public Hour(int initialValue) { this.value = initialValue; } public static bool operator==(Hour lhs, Hour rhs) { return lhs.value == rhs.value; } public static bool operator!=(Hour lhs, Hour rhs) { return lhs.value != rhs.value; } private int value; } The return type from these operators does not actually have to be Boolean. However, you would have to have a very good reason for using some other type or these operators could become very confusing! NOTE If you define operator== and operator!=, you should also override the Equals and GetHashCode methods inherited from System.Object. The Equals method should exhibit exactly the same behavior as operator== (define one in terms of the other). The GetHashCode method is used by other classes in the .NET Framework. (When you use an object as a key in a hash table for example, the GetHashCode method is called on the object to help calculate a hash value. For more information, see the .NET Framework Reference documentation supplied with Visual Studio 2005). All this method needs to do is return a distinguishing integer value (don't return the same integer from the GetHashCode method of all your objects though as this will reduce the effectiveness of the hashing algorithms). Editing Data You have seen how to use a GridView control to fetch and browse data. The following set of exercises concentrate on deleting and modifying data using a GridView control. Deleting Rows from a GridView Control The GridView control allows you to add buttons to the grid to indicate that a command should be performed. You can add your own custom buttons and commands, but Visual Studio 2005 supplies some predefined buttons for deleting and editing data. In the following exercise, you will add a Delete button to the GridView control. Create the Delete button 1. Ensure that CustomerData.aspx is displayed in the Design View window. Click the Smart Tag icon to display the GridView Tasks menu. 2. On the GridView Tasks menu, check the Enable Deleting box. A hyperlink labeled Delete is added to the start of each row in the GridView control. 3. On the GridView Tasks menu, click Edit Columns. The Fields dialog box is displayed. You can use this dialog box to set the properties of the fields (columns) displayed in the GridView control. 4. In the Selected fields list, select the Delete field. In the CommandField properties list, change the ButtonType property to Button. Click OK. The Delete link in the GridView control changes to a button. 5. Run the application. Log in, and then go to page 3 of the data. Delete the customer with the ID of FISSA. This should be successful. Try to delete customer FAMIA. This will fail with an error because this customer has outstanding orders; the referential integrity rules of the Northwind Traders database forbid you from deleting a customer that has outstanding orders. TIP The exception that is displayed is not very user-friendly (although a developer will find it very useful). If a Web form generates an exception, you can arrange for a more friendly message to be displayed by redirecting the user to another page using the ErrorPage attribute to the @Page directive in the form's source definition: <%@ Page … ErrorPage="ErrorPage.aspx" %> You can display a more comforting message to the user on this page. 6. Close Internet Explorer and return to the Visual Studio 2005 programming environment. Updating Rows in a GridView Control You can also add an Edit button to a GridView to allow a user to change the data in a selected row in the GridView. The row changes into a set of TextBox controls when the user clicks the Edit button. The user can save the changes or discard them. This is achieved using two additional buttons: Update and Cancel. In the following set of exercises, you will add these buttons to the CustomerData form. Create the Edit, Update, and Cancel buttons 1. Display the CustomerData.aspx form in Design View. Click the Smart Tag for the GridView control to display the GridView Tasks menu, and then click Enable Editing. An Edit button is added to each row in the GridView control. NOTE The Edit button is generated to match the style of the Delete button; if the Delete button was still a hyperlink, then Edit would also appear as a hyperlink. 2. Run the application. Log in, and then click the Edit button on the first row displayed on the CustomerData form. The first row changes into a collection of TextBox controls, and the Edit and Delete buttons are replaced with an Update button and a Cancel button. NOTE The CustomerID column remains as a label. This is because this column is the primary key in the Customers table. You should not be able to modify primary key values in a database; otherwise, you risk breaking the referential integrity between tables. 3. Modify the data in the ContactName and ContactTitle columns, and then click Update. The database is updated, the row reverts back to a set of labels, the Edit and Delete buttons reappear, and the new data is displayed in the row. 4. Close Internet Explorer and return to Visual Studio 2005. • If you want to continue to the next chapter Keep Visual Studio 2005 running and turn to Chapter 28. • If you want to exit Visual Studio 2005 for now On the File menu, click Exit. If you see a Save dialog box, click Yes.  Defining and Using a Class In C#, you use the class keyword, a name, and a pair of curly braces to define a new class. The data and methods of the class occur in the body of the class, between the curly braces. Here is a C# class called Circle that contains one method (to calculate the circle's area) and one piece of data (the circle's radius): class Circle { double Area() { return 3.141592 * radius * radius; } double radius; } The body of a class contains ordinary methods (such as Area) and fields (such as radius)—remember that variables in a class are called fields. You've already seen how to declare variables in Chapter 2, “Working with Variables, Operators, and Expressions,” and how to write methods in Chapter 3, “Writing Methods and Applying Scope”; in fact, there's almost no new syntax here. Using the Circle class is similar to using other types that you have already met; you create a variable specifying Circle as its type, and then you initialize the variable with some valid data. Here is an example: Circle c;// Create a Circle variable c = new Circle();// Initialize it Note the use of the new keyword. Previously, when you initialized a variable such as an int or a float, you simply assigned it a value: int i; i = 42; You cannot do the same with variables of class types. One reason is that C# just doesn't provide the syntax for assigning literal class values to variables. (What is the Circle equivalent of 42?) Another reason concerns the way in which memory for variables of class types is allocated and managed by the common language runtime—this will be discussed further in Chapter 8, “Understanding Values and References.” For now, just accept that the new keyword creates a new instance of a class (more commonly called an object). IMPORTANT Don't get confused between the terms class and object. A class is the definition of a type. An object is an instance of that type, created when the program runs. For example, it is possible to create many instances of the Circle class in a program by using the new keyword, just as you can create many int variables in a program. Each instance of the Circle class is an object that occupies its own space in memory, and runs independently of all the other instances. . Defining Operator Pairs Some operators naturally come in pairs. For example, if you can compare two Hour values by using the != operator,

Ngày đăng: 01/07/2014, 09:20

w