본문 바로가기
Visual Basic .NET/Event Handler

What is the delegate in VB.NET

by edupicker(체르니) 2008. 7. 2.


 I'm learning VB.NET at KH Education Institute at Seoul, Korea.
Now, our teacher is teaching Form Controls but I already knew how to use those.  
So I made a program like TCP/IP networking, Windows Services in our class.
I hope I'd like to teach student, and they will be a good programmer individually, if they want to be. 

Well, I remember when I learned how to use pointer In C, C++.
Definition of pointer is difficult to understand. In VB.NET, I think Byref Keywords is similar.

Now I introduce another like pointer in C language, it is delegate.
A delegate can be defined as a type safe function pointer. It encapsulates the memory address of a function in your code. Whenever you create or use an event in code, you are using a delegate. When the event is thrown, the framework examines the delegate behind the event and then calls the function that the delegate points to.

The following program, for example, declare two delegates for a subroutine ,a function and define a subroutine, a function for the delegates.

Delegate Sub StringMethod(ByVal Str As String),
Delegate Function Numbers(ByVal dblNum1 As Double, _
    ByVal dblNum2 As Double) As Double
 
Well, I think you'd better understand refering full source code.

source code :

사용자 삽입 이미지

execution result :
사용자 삽입 이미지

the compiled environment is Langauge : VB2005, OS : Windows 2003 Server.

That's all, bye.

'Visual Basic .NET > Event Handler' 카테고리의 다른 글

how to set Event Handler at runtime  (0) 2008.06.24