Ok,
I have been toying around with this subject. I don't know how close I am
and what pitfalls I should look out for but let's see how close we are with
this:
Private WithEvents objDS As DelegateSample
Private Sub FrmTest_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
objDS = New DelegateSample
MsgBox(objDS.HelloWorld)
objDS.Close()
End Sub
Private Sub objDS_Closing(ByVal sender As Object, ByVal e As
DelegateSampleArgs) Handles objDS.Closing
e.Cancel = True
End Sub
Private Sub objDS_Closed(ByVal Sender As Object, ByVal e As
System.EventArgs) Handles objDS.Closed
MsgBox("Our object has closed")
End Sub
Private Sub objDS_WhoAreYou(ByVal sender As Object, ByVal e As
DelegateSampleWhoAreYouArgs) Handles objDS.WhoAreYou
e.WhoAmI = System.Security.Principal.WindowsIdentity.GetCurre nt.Name
End Sub
Public Class DelegateSample
Delegate Sub CanClose(ByVal sender As Object, ByVal e As
DelegateSampleArgs)
Delegate Sub DoWhoAreYou(ByVal sender As Object, ByVal e As
DelegateSampleWhoAreYouArgs)
Event WhoAreYou As DoWhoAreYou
Event Closing As CanClose
Event Closed(ByVal Sender As Object, ByVal e As System.EventArgs)
Public Sub Close()
Dim e As DelegateSampleArgs
e = New DelegateSampleArgs
RaiseEvent Closing(Me, e)
Debug.WriteLine(e.Cancel.tostring)
If e.Cancel Then RaiseEvent Closed(Me, New System.EventArgs)
End Sub
Public Function HelloWorld()
Dim e As DelegateSampleWhoAreYouArgs
e = New DelegateSampleWhoAreYouArgs
e.WhoAmI = ""
RaiseEvent WhoAreYou(Me, e)
Return e.WhoAmI
End Function
End Class
Public Class DelegateSampleArgs
Inherits System.EventArgs
Private m_Cancel As Boolean
Public Sub New()
MyBase.New()
m_Cancel = False
End Sub
Public Property Cancel() As Boolean
Get
Return m_Cancel
End Get
Set(ByVal Value As Boolean)
m_Cancel = Value
End Set
End Property
End Class
Public Class DelegateSampleWhoAreYouArgs
Inherits System.EventArgs
Private m_WhoAmI As String
Public Sub New()
MyBase.New()
End Sub
Public Property WhoAmI() As String
Get
Return m_WhoAmI
End Get
Set(ByVal Value As String)
m_WhoAmI = Value
End Set
End Property
End Class
"david" <da***@woofix.local.dom> wrote in message
news:sl******************@localhost.localdomain...
On 2005-09-24, Brad Rogers <br*************@yahoo.com> wrote: Yes I was also looking for help with this. I have studied many books and
articles on "delegates" and each time they refuse to explain in a regular
way. Im wondering if most people simply dont know delegates and ignore
the
subject completely?
This must be the most complex or most poorly explained concept in
software
engineering I have ever seen in the past 100 years.
VB.Net generally hides the use of delegates from you, so books focused
on VB.Net tend to gloss over the subject.
Another problem is that delegates are basically just type-safe function
pointers. People who have worked with function pointers in the past
usually don't need much more explanation than that.
The delegate is used to call a method or a thing with a signature that
looks
like a method, or when called it can return in a typesafe way control
that
the original method could have used but didnt. It works by handling code,
except on tuesdays, when it becomes a different kind of method. Now that
Ive explained what delegates are, lets look at the code...
Because VB.Net generally hides delegates, VB.Net books have often focus
on relatively obscure uses of them to illustrate the topic. Personally,
I found the explanation in "The C# Programming Language" to be very
straightforward and easy to learn, since it's a lot easier to describe
them in the context of C# than it is in the context of VB.Net
"AMDRIT" <am****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... I am trying to understand Delegates and where/when to use them.
I can see one potential use of a delegate (on form closing, set the
cancel
property in the event arguments.)
Does anyone have a link to a site that describes delegates using VB.Net
in
a manner that doesn't require rocket science to become enlightened.
TIA