Define a Class in Visual Basic .NET
Thông tin tài liệu:
Nội dung trích xuất từ tài liệu:
Define a Class in Visual Basic .NET 9.1 Define a Class in Visual Basic .NETBefore writing code, you should take a moment to decide what data the class needs tocontain and what actions a developer (whether it is you or a teammate) would want toperform on that data. For the moment, well keep it fairly simple by limiting the class toproperties representing the columns in the Customers table and to methods that insert,delete, and update data.TechniqueVisual Basic .NET has a specific kind of code file that you can use to describe the publicmembers of a class: an interface. An interface simply lists all the public variables,methods, and properties that any class implementing that interface must expose.Interfaces are an enormously powerful and flexible feature of object-oriented programs,and this chapter just scratches the surface. Because of the fact that an interface has nocode, you can focus on the design of your application without the clutter of hundreds oflines of source code.Steps 1. First, create a new Windows Application project and name it CustomerClasses. 2. To create a new interface, add a new class to your project by right-clicking on the Solution Explorer window and selecting Add Class. Name the new file CustomerInterface9_1.vb. 3. Change the class block declaration so that it reads interface instead of class, and name the class ICustomer. You should have a code file like that shown in Listing 9.1. Listing 9.1 CustomerInterface9_1.vb: An Empty Interface Public Interface ICustomer End InterfaceAdd Properties to the InterfaceThe first items you need to add to your interface are properties to represent the columnsin the Customers table. 1. Using the Server Explorer, drill down to the Customers table in the Northwind database so that you have an easy reference to the Customers table. 2. To make your code more readable, add a #Region/#End Region block with the name Properties so that you can expand and collapse your property declarations as needed. 3. For each column in the table, add a property declaration to your interface that matches the datatype of the column and its name(see Listing 9.2). Listing 9.2 CustomerInterface.vb: The ICustomer Interface with Properties Public Interface ICustomer #Region Properties Define properties for this interface that match the table structure in name and data type Property CustomerID() As String Property CompanyName() As String Property ContactName() As String Property ContactTitle() As String Property Address() As String Property City() As String Property Region() As String Property PostalCode() As String Property Country() As String Property Phone() As String Property Fax() As String #End Region End InterfaceMore About PropertiesICustomer is a simple interface. It has only one kind of datatype-a string-and all of theproperties are read/write. When you begin to write more complex classes, you might needmore than just read/write properties. Visual Basic .NET also has read-only and write-onlyproperties, parameterized properties, and default properties.Read-Only and Write-Only PropertiesIf you have written classes in Visual Basic 6, you might be scratching your head,wondering how the code sample in Listing 9.2 declares your properties read/write. InVisual Basic 6, all that mattered was whether you wrote the appropriate Property Get andProperty Let/Set blocks in your class. If you did not have a Public or Friend PropertyLet/Set, the property was read-only. If you did not have a Public or Friend Property Get,the property was write-only.In Visual Basic .NET, you must explicitly declare a property as read-only or write-onlyusing the ReadOnly and WriteOnly keywords (see Table 9.1). Table 9.1. Visual Basic .NET Property ModifiersObject PurposeReadOnly The property cannot be modified outside of the class.WriteOnly The property can only be modified, but only methods that are internal to the class can read the property.In our example, a developer who is using this class could change the CustomerID.Without the original CustomerID, the class wont be able to find that record in thedatabase when updating the table, so perhaps you should make the CustomerID propertyread-only. To do this, simply add the ReadOnly keyword to the beginning of the propertydeclaration as in Listing 9.3.Listing 9.3 CustomerInterface9_1.vb: Using the ReadOnly KeywordAdding the ReadOnly keyword before the propertydeclaration makes the property read-only.ReadOnly Property CustomerID() As StringParameterized PropertiesYou can also create properties that accept a parameter. For example, in the Northwinddatabase, a customer might have many orders. A parameterized property would be aperfect way to access Orders information from the Customer class.A parameterized property (see Listing 9.4) takes one or more parameters that can be usedto qualify the data that the property returns.Listing 9.4 A Parameterized PropertyThe nIndex parameter would be used to determine whichitem from a collection of orders should be returned.Property CustomerOrders(ByVal nIndex As Integer) As COrderTip One of the advantages to interfaces is the ability to rapidly define all of the classes you plan to use in your application. For the previous code example, you could create an interface called IOrder that was similar to the ICustomer interface in Listing 9.2. Instead of returning an actual class from the CustomerOrders property, you could return the IOrder interface. The interface would act as a placeholder for any object that implemented ...
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 Define a Class in Visual Basic .NETGợi ý tà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 168 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 118 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 -
Giáo trình Lập trình Web với Servlet và JSP: Phần 1
56 trang 96 0 0 -
Giáo trình Nhập môn lập trình VB6: Phần 2
184 trang 93 0 0 -
Giáo trình Nhập môn lập trình VB6: Phần 1
246 trang 85 0 0 -
Giáo trình toán rời rạc - Phụ lục 2
15 trang 85 0 0 -
Nghiên cứu triển khai nội địa hóa máy tính thương hiệu Việt Nam
585 trang 83 0 0 -
Giáo trình Lập trình hướng đối tượng với Java: Phần 2 - Trần Thị Minh Châu, Nguyễn Việt Hà
141 trang 75 0 0 -
Cách chia sẻ File, dữ liệu mạng Lan trong Windows Xp
10 trang 61 0 0 -
Giáo trình Ngôn ngữ lập trình C++: Phần 2 - TS. Vũ Việt Vũ
107 trang 58 0 0 -
Luận văn: TÌM HIỂU KỸ THUẬT LẬP TRÌNH NETWORK SERVICE CHO WINDOW
39 trang 55 0 0 -
Bài giảng Kỹ thuật lập trình: Chương 7 - Trần Quang
28 trang 52 0 0