tailieunhanh - The book of visual basic 2005 net insight for classic vb developers 2006 - phần 3

Một khi bạn tạo ra một biến dựa trên một đại biểu, bạn có thể chỉ định một phương pháp của nó bằng cách sử dụng các nhà điều hành AddressOf. Nhà điều hành AddressOf cho phép Visual Basic 2005 biết rằng bạn đang sử dụng một tham chiếu đến một phương pháp, chứ không phải cố gắng để chạy nó trực tiếp. | Once you create a variable based on a delegate you can assign a method to it by using the AddressOf operator. The AddressOf operator lets Visual Basic 2005 know that you are using a reference to a method not trying to run it directly. Dim MyDelegate As ProcessFunction MyDelegate AddressOf CapitalizeName Once you set a delegate you can run the method later just by using the delegate Calls the CapitalizeName function and assigns its return value to UCaseName. Dim UCaseName As String UCaseName MyDelegate samantha jones This is a useful technique because it allows what programmers call an extra layer of rndim tioii. This means that the code you create is more generic and has a better chance of being reused. Here s a function that accepts a delegate as an argument and uses the function specified by the delegate to perform a task Public Sub ProcessArray MyArray As String _ FunctionToUse As ProcessFunction Dim i As Integer For i 0 to 0 MyArray i FunctionToUse MyArray i Next i End Sub You call the subroutine like this Dim CustomerNames As String bob evans chan park jill svetlova ProcessArray CustomerNames AddressOf CapitalizeName The result of this sleight of hand is that each element of CustomerNames will be modified according to the CapitalizeName method. By using a delegate you can create a single ProcessArray subroutine that can process array elements in a variety of different ways depending on the FunctionToUse reference that you supply. You only need to write the code in ProcessArray once but you still get ultimate flexibility and the envy of your colleagues . NOTE It can become a little confusing to keep all of these ingredients in mind at once. To perform this delegate test successfully you need to define the mil a delegate variable point the delegate variable at the right method and then run the method through the . You can see these steps in action by using the sample code available online. check out the DelegateTestl and

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.