Danh mục

Define a Class in Visual Basic .NET

Số trang: 7      Loại file: pdf      Dung lượng: 24.21 KB      Lượt xem: 17      Lượt tải: 0    
Thu Hiền

Phí tải xuống: 3,000 VND Tải xuống file đầy đủ (7 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

9,1 Xác định một Class trong Visual Basic NET Trước khi viết mã., Bạn nên mất một chút thời gian để quyết định loại dữ liệu những gì cần phải có hành động và những gì một nhà phát triển (cho dù đó là bạn hay đồng đội một) sẽ muốn thực hiện trên dữ 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ài liệu được xem nhiều:

Gợi ý tài liệu liên quan: