0
Tải bản đầy đủ (.pdf) (89 trang)

BÀI TẬP PHƯƠNG THỨC PHÁ HỦY

Một phần của tài liệu LẬP TRÌNH VB.NET TRỰC QUAN (Trang 56 -56 )

CHƯƠNG 7 CÂU LỆNH PROPERTY

7.1 MỤC ĐÍCH

Câu lệnh Property được sử dụng để dùng để gán giá trị cho một thuộc tính, một biến thành phần hoặc lấy giá trị của một thuộc tính, một biến thành phần đã được khai báo trong Module, Class hoặc Structure.

7.2 CÚ PHÁP KHAI BÁO CÂU LỆNH PROPERTY

[<attributelist>][Default][accessmodifier] [propertymodifiers][Shared][Shadows]

[ReadOnly|WriteOnly]

Property name ( [parameterlist] )

[As returntype] [Implements implementslist] [<attributelist>] [accessmodifier] Get

[statements] End Get

[<attributelist>][accessmodifier] Set

(ByVal value As returntype [,parameterlist]) [statements]

End Set End Property

7.3 VÍ DỤ

Bài mẫu 10. Hãy sử dụng câu lệnh Property

dùng để gán giá trị và lấy giá trị đối với thuộc tính intMaSo trong class CHocSinh.

Khai báo định nghĩa phương thức. Public Class CHocSinh

Private intMaSo As Integer Private strHoTen As String Private dblToan As Double Private dblVan As Double

Private dblTrungBinh As Double Private intMaKhoi As Integer Property _MaSo() As Integer

Get

Return intMaSo End Get

Set(ByVal value As Integer) intMaSo = value End Set End Property End Class Hướng dẫn sử dụng Module Module1 Sub Main()

Dim hs As New ChocSinh

hs._MaSo = 5

Dim a As Integer = hs._MaSo

...

End Module

7.4 CÁC LƯU Ý KHI SỬ DỤNG CÂU LỆNH PROPERTY

- Câu lệnh Property được sử dụng để cập nhật và cung cấp thông tin của một thuộc tính. Một thuộc tính có thể có thủ tục Get (đọc), thủ tục Set (ghi), hoặc cả hai (đọc-ghi).

- Từ khóa để tham khảo trong MSDN là “Property Statement”.

- Nếu không có các từ khóa chỉ phạm vi Public, Protected, Private, Friend thì thuộc tính (Property) của thủ tục Get và thủ tục Set sẽ mặc định là Public.

7.5 TÀI LIỆU THAM KHẢO

[4].Connell–Coding Techniques for Microsoft Visual Basic .NET– Copyright © 2002 by Microsoft Corporation.

[5].Microsoft Visual Studio 2005 Documentation–Copyright © 2002 by Microsoft Cororation.

CHƯƠNG 8 TOÁN TỬ TRONG VB.NET

8.1 CÚ PHÁP CHUNG KHAI BÁO VÀ ĐỊNH NGHĨA TOÁN TỬ

[<attrlist>] Public [Overloads] Shared [Shadows]

[ Widening | Narrowing ]

Operator operatorsymbol ( operand1 [, operand2 ])

[ As [ <attrlist> ] type ] [ statements ] [ statements ] Return returnvalue [ statements ] End Operator

- Từ khóa để tham khảo trong MSDN là “Operator Statement”.

8.2 TOÁN TỬ SỐ HỌC

8.2.1 Khái niệm

8.2.2 Ứng dụng toán tử số học

Bài mẫu 11. Hãy định nghĩa toán tử cộng (operator+) cho lớp đối tượng phân số (CPhanSo).

Public Class CPhanSo Private Tu As Integer Private Mau As Integer Public Sub New()

Tu = 0 Mau = 1 End Sub

Public Shared Operator +(ByVal x As CPhanSo, ByVal y As CPhanSo) As CPhanSo Dim temp As New CPhanSo

temp.Tu = x.Tu * y.Mau + x.Mau * y.Tu temp.Mau = x.Mau * y.Mau

Return temp End Operator Public Sub Nhap()

... End Sub

Public Sub Xuat()

Console.WriteLine(Tu & "/" & Mau) End Sub

End Class

ĐƠN THỂ CHÍNH

Module Module1

Sub Main()

Dim a As New CPhanSo Dim b As New CPhanSo a.Nhap() b.Nhap() Dim kq As CPhanSo kq = a + b kq.Xuat() End Sub End Module

8.3 TOÁN TỬ GÁN

Khái niệm: Toán tử gán trong các ngôn ngữ lập trình được sử dụng để gán giá trị của biến này cho biến khác. Trong ngôn ngữ lập trình hướng đối tượng toán tử gán được sử dụng để gán thành phần dữ liệu của đối tượng này cho đối tượng khác.

Trong ngôn ngữ lập trình VB.NET toán tử gán có ý nghĩa hai đối tượng cùng tham chiếu tới một địa chỉ bộ nhớ.

8.4 TOÁN TỬ SO SÁNH

8.4.1 Khái niệm

Trong ngôn ngữ VB.NET cung cấp các toán tử so sánh như sau:

STT Toán tử Ý nghĩa Ghi chú

1 Operator < Toán tử so sánh nhỏ hơn. 2 Operator > Toán tử so sánh lớn hơn. 3 Operator = Toán tử so sánh bằng. 4 Operator <> Toán tử so sánh khác.

5 Operator >= Toán tử so sánh lớn hơn bằng. 6 Operator <= Toán tử so sánh nhỏ hơn bằng.

8.4.2 Ghi chú

- Toán tử overload được sử dụng để tái định nghĩa lại các thuộc tính hay thủ tục có cùng một tên

8.4.3 Ứng dụng toán tử so sánh

Bài mẫu 12. Hãy định nghĩa toán tử so sánh lớn hơn (operator>) cho lớp đối tượng phân số (CPhanSo).

Public Class CPhanSo Private Tu As Integer Private Mau As Integer Public Sub New()

Tu = 0 Mau = 1 End Sub

Public Overloads Shared Operator –(ByVal x As CPhanSo, ByVal y As CPhanSo) As CPhanSo Dim temp As New CPhanSo

temp.Tu = x.Tu * y.Mau - x.Mau * y.Tu temp.Mau = x.Mau * y.Mau

temp.Rutgon() Return temp End Operator

Public Overloads Shared Operator >(ByVal x As CPhanSo, ByVal y As CPhanSo) As Boolean Dim temp As New CPhanSo

temp = x - y

Return (temp.Tu * temp.Mau > 0) End Operator

Public Overloads Shared Operator <(ByVal x As CPhanSo, ByVal y As CPhanSo) As Boolean Dim temp As New CPhanSo

temp = x - y

Return (temp.Tu * temp.Mau < 0) End Operator

Public Sub Rutgon()

Dim a As Integer = Math.Abs(Tu) Dim b As Integer = Math.Abs(Mau) While (a * b <> 0) If (a > b) Then a -= b Else b -= a End If End While Tu = Tu / (a + b) Mau = Mau / (a + b) End Sub

Public Sub Nhap()

Console.Write("Nhap tu: ")

Tu = Console.ReadLine()

Console.Write("Nhap mau: ")

Mau = Console.ReadLine() End Sub

Public Sub Xuat()

Console.WriteLine(Tu & "/" & Mau) End Sub

End Class

8.5 TOÁN TỬ ÉP KIỂU

8.5.1 Khái niệm

Toán tử ép kiểu được sử dụng để ép một đối tượng thuộc lớp này thành một đối tượng thuộc lớp khác.

8.5.2 Ghi chú

- Để định nghĩa toán tử ép kiểu ta phải sử dụng từ khóa widening trong việc định nghĩa.

- Từ khóa widening được sử dụng chung với thủ tục có khai báo public shared.

8.5.3 Ứng dụng toán tử ép kiểu

Bài mẫu 13. Hãy định nghĩa toán tử ép kiểu (CType) để ép một đối tượng thuộc lớp số nguyên thành một đối tượng phân số (CPhanSo).

Public Shared Widening Operator CType(ByVal t As Integer) As CPhanSo

Dim temp As New CPhanSo temp.Tu = t

temp.Mau = 1 Return temp End Operator

CHƯƠNG 9 CƠ SỞ DỮ LIỆU

9.1 LỚP ĐỐI TƯỢNG OLEDBCONNECTION

9.1.1 Mục đích

OleDbConnection là một lớp đối tượng được sử dụng để kết nối cơ sở dữ liệu.

9.1.2 Các đặc điểm chính của lớp đối tượng OleDbConnection - Không cho phép một lớp đối tượng khác kế thừa. - Kế thừa trực tiếp từ lớp đối tượng DbConnection. - Namespace: System.Data.OleDb

- Assembly: System.Data (in system.data.dll)

9.1.3 Cây kế thừa System.Object System.MarshalByRefObject System .ComponentModel.Component System.Data.Common.DbConnection System.Data.Odbc.OdbcConnection System.Data.OleDb.OleDbConnection

9.1.4 Các thuộc tính của lớp đối tượng OleDbConnection

Danh sách các thuộc tính thuộc phạm vi public của lớp đối tượng OleDbConnection.

Các thuộc tính thuộc phạm vi public của lớp đối tượng OleDbConnection

TT Tên thuộc tính Ý nghĩa

1 ConnectionString Overridden. Gets or sets the string used to open a database.

2 ConnectionTimeout

Overridden. Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.

3 Container Gets the IContainer that contains the Component.(Inherited from Component.)

4 Database Overridden. Gets the name of the current database or the database to be used after a connection is opened.

5 DataSource Overridden. Gets the server name or file name of the data source.

6 Provider Gets the name of the OLE DB provider specified in the "Provider= " clause of the connection string. 7 ServerVersion Overridden. Gets a string that contains the version of the server to which the client is connected.

8 Site Gets or sets the ISite of the Component.(Inherited from Component.) 9 State Overridden. Gets the current state of the connection

Danh sách các thuộc tính thuộc phạm vi protected của lớp đối tượng OleDbConnection.

Các thuộc tính thuộc phạm vi protected của lớp đối tượng OleDbConnection

TT Tên thuộc tính Ý nghĩa

1 CanRaiseEvents Gets a value indicating whether the component can raise an event.(Inherited from Component.)

2 DesignMode

Gets a value that indicates whether the Component is currently in design mode.(Inherited from

Các thuộc tính thuộc phạm vi protected của lớp đối tượng OleDbConnection

TT Tên thuộc tính Ý nghĩa

3 Events Gets the list of event handlers that are attached to this Component.(Inherited from Component.)

9.1.5 Các vấn đề cần lưu ý khi sử dụng lớp OleDbConnection

When you create an instance of OleDbConnection, all properties are set to their initial values. For a list of these values, see the OleDbConnection constructor.

If the OleDbConnection goes out of scope, it is not closed. Therefore, you must explicitly close the connection by calling Close or Dispose, or by using the OleDbConnection object within a Using statement.

9.1.6 Phương thức thiết lập của lớp OleDbConnection Lớp OleDbConnection có hai phương thức thiết lập:

- OleDbConnection () - OleDbConnection (String)

Trong đó phương thức thiết lập mặt định sẽ tạo ra một thể hiện (một đối tượng) của lớp OleDbConnection.

Phương thức thiết lập nhận tham số đầu vào là một chuỗi ký tự tương ứng với chuỗi kết nối dữ liệu.

Phương thức thiết lập

TT Dạng thức khai báo Dạng thức sử dụng 1 Public Sub New Dim instance As New OleDbConnection

Phương thức thiết lập

TT Dạng thức khai báo Dạng thức sử dụng

2 Public Sub New (connectionString As String)

Dim connectionString As String

Dim instance As New OleDbConnection (connectionString)

9.1.7 Các phương thức chính của lớp đối tượng OleDbConnection Danh sách các phương thức chính

TT Dạng thức khai báo Dạng thức sử dụng 1 Public Overrides Sub Close Dim instance As OleDbConnection Instance.Close

2 Public Sub Dispose Dim instance As Component Instance.Dispose

3 Public Overrides Sub Open Dim instance As OleDbConnection Instance.Open

9.1.8 Hàm kết nối minh họa

Hàm cài đặt dưới đây thực hiện kết nối với cơ sở dữ liệu HocSinh.mdb được tạo bởi Microsoft Access. Tập tìn HocSinh.mdb được để trong thư mục Bin hoặc thư mục Release trong thư mục Bin của project đang viết.

Public Function ConnectionData() As OleDbConnection Dim objOleDbConnect As OleDbConnection

Dim strConnectString As String

strConnectString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=HocSinh.mdb;" Try objOleDbConnect= New OleDbConnection(strConnectString) objOleDbConnect.Open() Return objOleDbConnect Catch ex As Exception Console.Write(ex.ToString) Return Nothing

End Try End Function

9.2 LỚP ĐỐI TƯỢNG OLEDBCOMMAND

9.2.1 Mục đích

Represents an SQL statement or stored procedure to execute against a data source.

9.2.2 Các đặc điểm chính của lớp đối tượng OleDbCommand - Không cho phép một lớp đối tượng khác kế thừa. - Kế thừa trực tiếp từ lớp đối tượng DbCommand. - Namespace: System.Data.OleDb

- Assembly: System.Data (in system.data.dll)

9.2.3 Cây kế thừa System.Object System.MarshalByRefObject System.ComponentModel.Component System.Data.Common.DbCommand System.Data.Odbc.OdbcCommand System.Data.OleDb.OleDbCommand System.Data.OracleClient.OracleCommand System.Data.SqlClient.SqlCommand System.Data.SqlServerCe.SqlCeCommand

9.2.4 Các thuộc tính của lớp đối tượng OleDbCommand

Danh sách các thuộc tính thuộc phạm vi public của lớp đối tượng OleDbCommand.

Các thuộc tính thuộc phạm vi public của lớp đối tượng OleDbCommand

TT Tên thuộc tính Ý nghĩa

1 CommandText Overridden. Gets or sets the SQL statement or stored procedure to execute at the data source.

2

CommandTimeout Overridden. Gets or sets the wait time before terminating an attempt to execute a command and generating an error.

3 CommandType Overridden. Gets or sets a value that indicates how the CommandText property is interpreted.

4 Connection Gets or sets the OleDbConnection used by this instance of the OleDbCommand.

5 Container Gets the IContainer that contains the Component.(Inherited from Component.)

6

DesignTimeVisible Overridden. Gets or sets a value that indicates whether the command object should be visible in a customized Windows Forms Designer control. 7 Parameters Gets the OleDbParameterCollection.

8 Site Gets or sets the ISite of the Component.(Inherited from Component.)

9 Transaction Gets or sets the OleDbTransaction within which the OleDbCommand executes.

10

UpdatedRowSource Overridden. Gets or sets how command results are applied to the DataRow when used by the Update method of the OleDbDataAdapter.

Danh sách các thuộc tính thuộc phạm vi protected của lớp đối tượng OleDbCommand.

Các thuộc tính thuộc phạm vi protected của lớp đối tượng OleDbCommand

TT Tên thuộc tính Ý nghĩa

Các thuộc tính thuộc phạm vi protected của lớp đối tượng OleDbCommand

TT Tên thuộc tính Ý nghĩa

2

DesignMode Gets a value that indicates whether the Component is currently in design mode.(Inherited from

Component.)

3 Events Gets the list of event handlers that are attached to this Component.(Inherited from Component.)

9.2.5 Các vấn đề cần lưu ý khi sử dụng lớp OleDbCommand

When an instance of OleDbCommand is created, the read/write properties are set to their initial values. For a list of these values, see the

OleDbCommand constructor.

9.2.6 Phương thức thiết lập của lớp OleDbCommand Lớp OleDbCommand có các phương thức thiết lập:

- OleDbCommand () - OleDbCommand (String)

- OleDbCommand (String, OleDbConnection)

- OleDbCommand (String, OleDbConnection, OleDbTransaction)

Các dạng thức khai báo và sử dụng Phương thức thiết lập

TT Dạng thức khai báo Dạng thức sử dụng 1 Public Sub New Dim instance As New OleDbCommand 2 Public Sub New (cmdText

As String )

Dim cmdText As String

Phương thức thiết lập

TT Dạng thức khai báo Dạng thức sử dụng

3

Public Sub New (cmdText As String,connection As OleDbConnection)

Dim cmdText As String

Dim connection As OleDbConnection

Dim instance As New OleDbCommand(cmdText, connection)

4

Public Sub New ( cmdText As String, connection As OleDbConnection, transaction As OleDbTransaction )

Dim cmdText As String

Dim connection As OleDbConnection Dim transaction As OleDbTransaction

Dim instance As New OleDbCommand(cmdText, connection, transaction)

9.2.7 Các phương thức chính của lớp đối tượng OleDbCommand Danh sách các phương thức chính TT Dạng thức khai báo Dạng thức sử dụng 1 Public Function ExecuteReader As OleDbDataReader

Dim instance As OleDbCommand Dim returnValue As OleDbDataReader

returnValue = instance.ExecuteReader 2 Public Sub Dispose Dim instance As OleDbCommand instance.Dispose

Phương thức ExecuteReader Sends the CommandText to the Connection and builds an OleDbDataReader.

Phương thức Dispose kế thừa từ lớp Component.

9.3 SR

System.Object

System.MarshalByRefObject System.Data.Common.DbDataReader

To create an OleDbDataReader, you must call the ExecuteReader method of the OleDbCommand object, instead of directly using a constructor.

While the OleDbDataReader is being used, the associated OleDbConnection is busy serving the OleDbDataReader, and no other operations can be performed on the OleDbConnection other than closing it. This is the case until the Close method of the OleDbDataReader is called. For example, you cannot retrieve output parameters until after you call Close.

Changes made to a result set by another process or thread while data is being read may be visible to the user of the OleDbDataReader. However, the precise behavior is timing dependent.

IsClosed and RecordsAffected are the only properties that you can call after the OleDbDataReader is closed. Although the RecordsAffected

property may be accessed while the OleDbDataReader exists, always call

Close before returning the value of RecordsAffected to guarantee an accurate return value.

PHỤ LỤC A. CÁC KIỂU DỮ LIỆU CHUẨN

TRONG VB.NET

A.1 CÁC KIỂU DỮ LIỆU CƠ SỞ

Các kiểu dữ liệu cơ sở là các kiểu dữ liệu được cung cấp bởi Trình Biên Dịch (compiler). Giá trị của các kiểu dữ liệu này luôn luôn được phép truy xuất một cách trực tiếp. Mặt khác VB.NET là một ngôn ngữ hoàn toàn huớng đối tượng nên ta có thể nói các kiểu dữ liệu cơ sở là: các lớp đối tượng cơ sở và các lớp này đều kế thừa từ lớp System.Object.

STT Tên Kiểu Cây kế thừa thước Kích Giá trị mặc định Phạm vi biễu diễn

1 Boolean System.Boolean 4 byte False True hoặc False

2 Byte System.Byte 1 byte 0 [0, 255]

3 Char System.Char 2 byte Char(0) [0..65.535]

4 Date System.DateTime 8 byte #01/01/2001 12:00:00 AM#

[01/01/0001, ..,31/12/9999] 5 Decimal System.Decimal 12 byte 0D 7.9...E+28 6 Double System.Double 8 byte 0.0 1.7...E+308 7 Integer System. Int32 4 byte 0 4.2...E+9 8 Long System.Int64 8 byte 0 1.8...E+19

STT Tên Kiểu Cây kế thừa thước Kích Giá trị mặc định Phạm vi biễu diễn

9 Short System.Int16 2 byte 0 6.5...E+4 10 Single System.Single 4 byte 0.0 3.4...E+38 11 Structure System.ValueType

Structure là một kiểu dữ liệu do người dùng tự định nghĩa (UDT– user defined type).

Ví dụ: Khai báo kiểu dữ liệu phân số. Structure PhanSo

Dim Tu As Integer Dim Mau As Integer End Structure

Các đặc điểm của một structure trong VB.NET

- Structure được xem như là một đơn thể đơn (single unit). - Structure không được kế thừa từ một kiểu dữ liệu khác. - Một Class-Lớp đối tượng không được kế thừa từ Structure. A.2 KIỂU THAM CHIẾU

STT Tên Kiểu Cây kế thừa Kích

thước Giá trị mặc định

Phạm vi biễu diễn

1 Object System. Object 4 byte

A.3 ĐỐI TƯỢNG TRONG VB.NET

A.3.1 Khái niệm

Khái niệm: đối tượng là một sự thể hiện của lớp.

A.3.2 Cú pháp

Dim <Tên đối tượng> As <Tên Lớp>

Ví dụ 1: Khai báo đối tượng n thuộc lớp Integer.

Dim n As Integer ‘Đối tượng n thuộc lớp số nguyên

Ví dụ 2: Khai báo và khởi gán giá trị ban đầu là 100 đối tượng n thuộc lớp Integer.

Dim n As Integer = 100

A.3.3 Các qui định khi khai báo đối tượng - Không có khoảng trắng. - Không có khoảng trắng.

- Không được bắt đầu bằng ký số. Phải bắt đầu bằng một chữ cái hay dấu gạch dưới (_).

- Không được chứa ký tự đặc biệt.

- Không được trùng với các từ khóa (keyword) trong ngôn ngữ VB.NET.

- Chiều dài không vượt quá 16.383 ký tự. - Phải là duy nhất trong tầm vực (scope) của nó. Ví dụ: Các cách thúc đặt tên đối tượng dưới đây là sai:

Dim so nguyen As Integer ‘Sai vì có khoảng trắng

Dim 123A As Double ‘Sai vì bắt đầu bằng một ký số

Dim for As String ‘Sai vì trùng với từ khóa

A.4 TÀI LIỆU THAM KHẢO

[1].Connell–Coding Techniques for Microsoft Visual Basic .NET– Copyright © 2002 by Microsoft Corporation.

[2].Microsoft Visual Studio 2005 Documentation–Copyright © 2002 by Microsoft Cororation.

PHỤ LỤC B. CÁC TỪ KHÓA TRONG VB.NET

B.1 DANH SÁCH CÁC TỪ KHÓA TRONG VB.NET

Chữ cái Các từ khóa

A Alias AddHandler Ansi

A As Assembly Auto

B Binary ByRef ByVal

Một phần của tài liệu LẬP TRÌNH VB.NET TRỰC QUAN (Trang 56 -56 )

×