Control the Creation and Behavior of Classes Now that you have code that can prepare all of the
Số trang: 7
Loại file: pdf
Dung lượng: 21.57 KB
Lượt xem: 14
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Kiểm soát 9,4 Sáng tạo và Hành vi của lớp Bây giờ bạn đã code mà có thể chuẩn bị tất cả các đối tượng bạn cần phải truy cập và cập nhật dữ liệu trong cơ sở dữ liệu, đó là thời gian để viết code mà có thể tải thông tin cho cơ sở dữ liệu vào lớp học của bạn. Trong Visual Basic 6
Nội dung trích xuất từ tài liệu:
Control the Creation and Behavior of Classes Now that you have code that can prepare all of the9.4 Control the Creation and Behavior of ClassesNow that you have code that can prepare all of the objects you need to access and updatedata in the database, its time to write code that can load information for the database intoyour class. In Visual Basic 6, developers typically used one of two techniques to populatea class from a database. One was to have a collection class with a LoadData method thatwould open a recordset and iterate through the rows, creating instances of the classrepresenting the table and setting the objects properties with data from the row. Thesecond was to have a Retrieve method that would accept a primary key value and load thedata from that one row.The problem with both of these techniques was that you could never be sure if adeveloper who was using your component would call the methods in the proper order.For example, that developer might attempt to access properties of the class before callingthe Retrieve method. If those properties were strings and numbers, the developer wouldreceive a 0-length string and a 0, respectively. These could both be valid values withinyour database and would not raise an error. How do you control what is happening? Youcould have an internal Boolean variable representing whether data was loaded into theobject, and add If...Then statements in every property and method that would raise anerror if that Boolean variable was False. This is not, however, a neat solution.The Class_Initialize Event was no help either because it wouldnt accept parameters. Youcould use a global variable that the initialize event would access, but this would onlywork if a single instance of the class could exist in your application.In short, in VB 6, you didnt have control over the creation of objects.TechniqueVisual Basic .NET does give you this kind of control with a new kind of method called aconstructor. A constructor is a method that is called in conjunction with the Newkeyword when youre creating an object instance. It is similar to the Class_Initialize eventthat is available in Visual Basic 6, except that it accepts parameters. If the parameters thatare passed to a constructor are not of the proper type, or if a developer neglects to specifythose parameters, the code does not compile.In this section, you will learn how to write a constructor that accepts a primary key valueand uses that value to populate the properties of a CCustomer object.StepsFirst, the CCustomer class needs access to the data adapter code you created in theprevious section. You could paste all of the code you have written so far in theCCustomer class into the CCustomerData class, but you may want to use theCCustomerData objects elsewhere. That leaves you two options: you could have amodule-level CCustomerData variable within the CCustomer class, or you could have theCCustomer class inherit all of the code in the CCustomerData class. Youve probablydone the former before, so try the latter. 1. In the declaration of the CCustomer class, add Inherits CCustomerData. 2. Add module-level variables of type dsCustomers and dsCustomers.CustomersRow, and name them mdsCust and mdrCust, respectively. 3. Add the following constructor to the CCustomer class, as shown in Listing 9.23. Listing 9.23 frmHowTo9_4.vb: A Constructor for the CCustomer Class Public Class CCustomer Inherits CCustomerData Implements ICustomer #Region Constructors Public Sub New(ByVal pCustomerIDToRetrieve As String) This constructor takes a valid CustomerID as a parameter, and retrieves that row. If the row does not exist, an Exception is thrown. Create an instance of the Customers dataset mdsCust = New dsCustomers() Try Set the parameter for the select command so that we retrieve the proper row from the table. MyBase is used to refer to members of the inherited class. It is only required in certain circumstances, such as when calling a constructor in the inherited class. MyBase.odaCustomers.SelectCommand.Parameters(0).Value = _ pCustomerIDToRetrieve A strongly typed dataset could contain multiple tables, so you must specify the table name to fill. odaCustomers.Fill(mdsCust, Customers) Catch ex As Exception Throw New ApplicationException(The customer row could not be retrieved. & ex.Message) End Try If mdsCust.Customers.Rows.Count = 1 Then Option Strict disallows implicit type conversions. Even though the row returned by dsCustomers.Rows(0) is actually of the type CustomersRow, the method is ...
Nội dung trích xuất từ tài liệu:
Control the Creation and Behavior of Classes Now that you have code that can prepare all of the9.4 Control the Creation and Behavior of ClassesNow that you have code that can prepare all of the objects you need to access and updatedata in the database, its time to write code that can load information for the database intoyour class. In Visual Basic 6, developers typically used one of two techniques to populatea class from a database. One was to have a collection class with a LoadData method thatwould open a recordset and iterate through the rows, creating instances of the classrepresenting the table and setting the objects properties with data from the row. Thesecond was to have a Retrieve method that would accept a primary key value and load thedata from that one row.The problem with both of these techniques was that you could never be sure if adeveloper who was using your component would call the methods in the proper order.For example, that developer might attempt to access properties of the class before callingthe Retrieve method. If those properties were strings and numbers, the developer wouldreceive a 0-length string and a 0, respectively. These could both be valid values withinyour database and would not raise an error. How do you control what is happening? Youcould have an internal Boolean variable representing whether data was loaded into theobject, and add If...Then statements in every property and method that would raise anerror if that Boolean variable was False. This is not, however, a neat solution.The Class_Initialize Event was no help either because it wouldnt accept parameters. Youcould use a global variable that the initialize event would access, but this would onlywork if a single instance of the class could exist in your application.In short, in VB 6, you didnt have control over the creation of objects.TechniqueVisual Basic .NET does give you this kind of control with a new kind of method called aconstructor. A constructor is a method that is called in conjunction with the Newkeyword when youre creating an object instance. It is similar to the Class_Initialize eventthat is available in Visual Basic 6, except that it accepts parameters. If the parameters thatare passed to a constructor are not of the proper type, or if a developer neglects to specifythose parameters, the code does not compile.In this section, you will learn how to write a constructor that accepts a primary key valueand uses that value to populate the properties of a CCustomer object.StepsFirst, the CCustomer class needs access to the data adapter code you created in theprevious section. You could paste all of the code you have written so far in theCCustomer class into the CCustomerData class, but you may want to use theCCustomerData objects elsewhere. That leaves you two options: you could have amodule-level CCustomerData variable within the CCustomer class, or you could have theCCustomer class inherit all of the code in the CCustomerData class. Youve probablydone the former before, so try the latter. 1. In the declaration of the CCustomer class, add Inherits CCustomerData. 2. Add module-level variables of type dsCustomers and dsCustomers.CustomersRow, and name them mdsCust and mdrCust, respectively. 3. Add the following constructor to the CCustomer class, as shown in Listing 9.23. Listing 9.23 frmHowTo9_4.vb: A Constructor for the CCustomer Class Public Class CCustomer Inherits CCustomerData Implements ICustomer #Region Constructors Public Sub New(ByVal pCustomerIDToRetrieve As String) This constructor takes a valid CustomerID as a parameter, and retrieves that row. If the row does not exist, an Exception is thrown. Create an instance of the Customers dataset mdsCust = New dsCustomers() Try Set the parameter for the select command so that we retrieve the proper row from the table. MyBase is used to refer to members of the inherited class. It is only required in certain circumstances, such as when calling a constructor in the inherited class. MyBase.odaCustomers.SelectCommand.Parameters(0).Value = _ pCustomerIDToRetrieve A strongly typed dataset could contain multiple tables, so you must specify the table name to fill. odaCustomers.Fill(mdsCust, Customers) Catch ex As Exception Throw New ApplicationException(The customer row could not be retrieved. & ex.Message) End Try If mdsCust.Customers.Rows.Count = 1 Then Option Strict disallows implicit type conversions. Even though the row returned by dsCustomers.Rows(0) is actually of the type CustomersRow, the method is ...
Tìm kiếm theo từ khóa liên quan:
công nghệ máy tính phần mềm kỹ thuật lập trình lập trình dữ liệu Control the CreationTài liệu liên quan:
-
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 266 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 208 0 0 -
Giới thiệu môn học Ngôn ngữ lập trình C++
5 trang 195 0 0 -
6 trang 192 0 0
-
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 169 0 0 -
Luận văn: Nghiên cứu kỹ thuật giấu tin trong ảnh Gif
33 trang 153 0 0 -
Báo cáo thực tập Công nghệ thông tin: Lập trình game trên Unity
27 trang 119 0 0 -
Giáo trình về phân tích thiết kế hệ thống thông tin
113 trang 114 0 0 -
LUẬN VĂN: Tìm hiểu kỹ thuật tạo bóng cứng trong đồ họa 3D
41 trang 109 0 0 -
Bài giảng Kỹ thuật lập trình - Chương 10: Tổng kết môn học (Trường Đại học Bách khoa Hà Nội)
67 trang 106 0 0