By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,686 Members | 1,558 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,686 IT Pros & Developers. It's quick & easy.

Delegates and assigning multiple procedures? How?

P: n/a
In C# I know that you can use delegates to assing
multiple addresses of sub and functions to a delegate and
have it fire multiple procedures...

How do I do this in VB? I only know of assigning a
single method to a delegate in VB.NET. I want to use it
as in C#... to fire multiple events.

Thanks in advance!
Nov 20 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Hello,

"Iced Crow" <ch********@aol.com> schrieb:
In C# I know that you can use delegates to assing
multiple addresses of sub and functions to a delegate and
have it fire multiple procedures...

How do I do this in VB? I only know of assigning a
single method to a delegate in VB.NET. I want to use it
as in C#... to fire multiple events.


Are you sure you read this chapter of the documentation?

Events and Delegates (Visual Basic Language Concepts)
http://msdn.microsoft.com/library/en...nheritance.asp

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #2

P: n/a
Those examples only show how to add a single method to a delegate...
what method of a delegate can I call to add multiple methods? Do I have
to create an array of delegates or a collection of delegates? The only
thing close to what I need is "CreateDelegate" but that is asking for a
type...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #3

P: n/a
I glanced through it but could not find an instance of
applying multiple methods to a single delegate, only how
to add a single method to a delegate which i already know
how to do...
Nov 20 '05 #4

P: n/a
Iced Crow,
Are you talking actual delegates or are you talking events? As Herfried
pointed you at how to use AddHandler to add multiple subs to an event so
they all get called.

If you are talking actual delegates:

Have you tried the Delegate.Combine method that concatenates the invocations
lists of two delegates together?

Hope this helps
Jay

"Iced Crow" <ch********@aol.com> wrote in message
news:79****************************@phx.gbl...
In C# I know that you can use delegates to assing
multiple addresses of sub and functions to a delegate and
have it fire multiple procedures...

How do I do this in VB? I only know of assigning a
single method to a delegate in VB.NET. I want to use it
as in C#... to fire multiple events.

Thanks in advance!

Nov 20 '05 #5

P: n/a
Greetings Jay,

No I am not talking about events, I understand how to add
handlers.

I am talking delegates. A delegate object being assigned
the address of a procedure and then having it's invoke
method called to fire that procedure.

In C# I know of how to take a single delegate and assign
to it calls from multiple procedures, so that I only have
to fire off a single INVOKE method from that single
delegate to fire off the multiple methods.

With VB.NET I only know of how to assign a single method
address to a single delegate.

I understand the combine function, but I'm only working
with one delegate here so there's nothing to combine. Do
I have to create multiple delegate objects that reference
methods and then combine them all? THat seems horribly
ineffecient...

I don't know how familiar you are with the MOCs but in
the 2124 C# course they have an example with a nuclear
power plant and one invoke call to a delegate is able to
fire off multiple calls to pump procedures.

This is what I want to do with Visual Basic.NET. It has
nothing to do with events.

Thanks!
Nov 20 '05 #6

P: n/a
IcedCrow.
I understand the combine function, but I'm only working
with one delegate here so there's nothing to combine. Do
I have to create multiple delegate objects that reference
methods and then combine them all? THat seems horribly
ineffecient... How are you combining them in C#?

Is it the += syntax the same as for events?

(I don't do a lot of C#, but will look it up).

Thanks
Jay

"IcedCrow" <ch********@aol.com> wrote in message
news:00****************************@phx.gbl... Greetings Jay,

No I am not talking about events, I understand how to add
handlers.

I am talking delegates. A delegate object being assigned
the address of a procedure and then having it's invoke
method called to fire that procedure.

In C# I know of how to take a single delegate and assign
to it calls from multiple procedures, so that I only have
to fire off a single INVOKE method from that single
delegate to fire off the multiple methods.

With VB.NET I only know of how to assign a single method
address to a single delegate.

I understand the combine function, but I'm only working
with one delegate here so there's nothing to combine. Do
I have to create multiple delegate objects that reference
methods and then combine them all? THat seems horribly
ineffecient...

I don't know how familiar you are with the MOCs but in
the 2124 C# course they have an example with a nuclear
power plant and one invoke call to a delegate is able to
fire off multiple calls to pump procedures.

This is what I want to do with Visual Basic.NET. It has
nothing to do with events.

Thanks!

Nov 20 '05 #7

P: n/a
Howdy Iced,

The Help doc that Herfried gave says:

Event delegates are multicast, which means that they can hold references
to more than one event handling method. For details, see Delegate

The AddHandler statement that Jay mentions:
See VB Language/VB Language Tour/OOP in VB/Events and Delegates

The AddHandler statement is similar to the Handles clause in that both allow
you to specify an event handler that will handle an event. However, AddHandler
along with RemoveHandler provide greater flexibility than the Handles clause,
allowing you to dynamically add, remove, and change the error handler
associated with an event. And unlike Handles, AddHandler allows you to
associate multiple event handlers with a single event.

AddHandler takes two arguments: the name of an event from an event sender such
as a control, and an expression that evaluates to a delegate. You do not need
to explicitly specify the delegate class when using AddHandler, since the
AddressOf statement always returns a reference to the delegate.

The following example associates an event handler with an event raised by an
object:
AddHandler MyObject.Event1, AddressOf Me.MyEventHandler

RemoveHandler, which disconnects an event from an event handler, uses the same
syntax as AddHandler. For example:
RemoveHandler MyObject.Event1, AddressOf Me.MyEventHandler

Regards,
Fergus
MVP [Windows Start button, Shutdown dialogue]
Nov 20 '05 #8

P: n/a
IcedCrow,
I understand the combine function, but I'm only working
with one delegate here so there's nothing to combine. Do
I have to create multiple delegate objects that reference
methods and then combine them all? THat seems horribly
ineffecient... You do realize that the "+=" operator in C# when used with delegate types is
shorthand for the Delegate.Combine function?

While the "-=" operator when used with delegate types is shorthand for
Delegate.Remove function?

You can use ildasm.exe to verify the above statement.

So as I stated, have you tried the Delegate.Combine function?

Further you do realize that each time you use the AddressOf operator in
VB.NET that you are given a new Delegate?

Hope this helps
Jay

"IcedCrow" <ch********@aol.com> wrote in message
news:00****************************@phx.gbl... Greetings Jay,

No I am not talking about events, I understand how to add
handlers.

I am talking delegates. A delegate object being assigned
the address of a procedure and then having it's invoke
method called to fire that procedure.

In C# I know of how to take a single delegate and assign
to it calls from multiple procedures, so that I only have
to fire off a single INVOKE method from that single
delegate to fire off the multiple methods.

With VB.NET I only know of how to assign a single method
address to a single delegate.

I understand the combine function, but I'm only working
with one delegate here so there's nothing to combine. Do
I have to create multiple delegate objects that reference
methods and then combine them all? THat seems horribly
ineffecient...

I don't know how familiar you are with the MOCs but in
the 2124 C# course they have an example with a nuclear
power plant and one invoke call to a delegate is able to
fire off multiple calls to pump procedures.

This is what I want to do with Visual Basic.NET. It has
nothing to do with events.

Thanks!

Nov 20 '05 #9

P: n/a
yes it is +=
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:eg**************@TK2MSFTNGP12.phx.gbl...
IcedCrow.
I understand the combine function, but I'm only working
with one delegate here so there's nothing to combine. Do
I have to create multiple delegate objects that reference
methods and then combine them all? THat seems horribly
ineffecient...

How are you combining them in C#?

Is it the += syntax the same as for events?

(I don't do a lot of C#, but will look it up).

Thanks
Jay

"IcedCrow" <ch********@aol.com> wrote in message
news:00****************************@phx.gbl...
Greetings Jay,

No I am not talking about events, I understand how to add
handlers.

I am talking delegates. A delegate object being assigned
the address of a procedure and then having it's invoke
method called to fire that procedure.

In C# I know of how to take a single delegate and assign
to it calls from multiple procedures, so that I only have
to fire off a single INVOKE method from that single
delegate to fire off the multiple methods.

With VB.NET I only know of how to assign a single method
address to a single delegate.

I understand the combine function, but I'm only working
with one delegate here so there's nothing to combine. Do
I have to create multiple delegate objects that reference
methods and then combine them all? THat seems horribly
ineffecient...

I don't know how familiar you are with the MOCs but in
the 2124 C# course they have an example with a nuclear
power plant and one invoke call to a delegate is able to
fire off multiple calls to pump procedures.

This is what I want to do with Visual Basic.NET. It has
nothing to do with events.

Thanks!


Nov 20 '05 #10

P: n/a
"IcedCrow" <ch********@aol.com> wrote in news:096701c37e43$a4dc8590
$a*******@phx.gbl:
I glanced through it but could not find an instance of
applying multiple methods to a single delegate, only how
to add a single method to a delegate which i already know
how to do...


AddHandler.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 20 '05 #11

P: n/a
See, now I can comment on this one.

Alright when it comes to multicasts like this yes you want to use AddHandler
as everyone has said.

Now after a lot of research, especially yesterday, the AddHandler keyword is
simply a reference to EventHandlerList.AddHandler (object, delegate). At
least thats the way I understand it. And EventHandlerList is created at
runtime in most cases it seems. The code for its pretty straight forward
(you can find the source out there) not real complex, but when a delegate is
multicast like this, it automatically calls Delegate.Combine.

So just use AddHandler.

=)

CJ
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:OA**************@TK2MSFTNGP11.phx.gbl...
IcedCrow,
I understand the combine function, but I'm only working
with one delegate here so there's nothing to combine. Do
I have to create multiple delegate objects that reference
methods and then combine them all? THat seems horribly
ineffecient... You do realize that the "+=" operator in C# when used with delegate types

is shorthand for the Delegate.Combine function?

While the "-=" operator when used with delegate types is shorthand for
Delegate.Remove function?

You can use ildasm.exe to verify the above statement.

So as I stated, have you tried the Delegate.Combine function?

Further you do realize that each time you use the AddressOf operator in
VB.NET that you are given a new Delegate?

Hope this helps
Jay

"IcedCrow" <ch********@aol.com> wrote in message
news:00****************************@phx.gbl...
Greetings Jay,

No I am not talking about events, I understand how to add
handlers.

I am talking delegates. A delegate object being assigned
the address of a procedure and then having it's invoke
method called to fire that procedure.

In C# I know of how to take a single delegate and assign
to it calls from multiple procedures, so that I only have
to fire off a single INVOKE method from that single
delegate to fire off the multiple methods.

With VB.NET I only know of how to assign a single method
address to a single delegate.

I understand the combine function, but I'm only working
with one delegate here so there's nothing to combine. Do
I have to create multiple delegate objects that reference
methods and then combine them all? THat seems horribly
ineffecient...

I don't know how familiar you are with the MOCs but in
the 2124 C# course they have an example with a nuclear
power plant and one invoke call to a delegate is able to
fire off multiple calls to pump procedures.

This is what I want to do with Visual Basic.NET. It has
nothing to do with events.

Thanks!


Nov 20 '05 #12

P: n/a
Jay,

I completly understand where your coming from on this, and I guess I just
misunderstood what the actual question was. My apologies if I led him in
the wrong direction.

Most often, I like many people automatically associate Delegates with Events
(because its the most common application for them, unelss your doing some
stuff with multi-threading and invoking methods on a parent,sibling,child
control, but usually this is associated with events as well).

So that was my point about talking about AddHandler and hopefully giving a
little insight to this.

Thanks for the clarification and useful code by the way

-CJ

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:u1**************@TK2MSFTNGP11.phx.gbl...
CJ & Fergus,
The problem is IcedCrow is not using Events, he is using Delegates. A
delegate is a reference to a function. Although Events are implemented in
the terms of a Delegate they are distinct elements.

As far as I can tell AddHandler only works with Events and not with Delegate variables. (members declared with the Event keyword, not variables declared with a Delegate type.) I get an error about the delegate variable not being an Event of the containing class when I attempt to use AddHandler on a
Delegate variable.

I understand he has something like:

Public Delegate Sub MySub()

Public Sub DoWork()
Dim subs As MySub
' magic code to initialize the subs variable
subs.Invoke()
End Sub

Public Sub MyWork()
Debug.WriteLine("working", "MyWork")
End Sub

Public Sub MyWork1()
Debug.WriteLine("working", "MyWork1")
End Sub

Public Sub MyWork2()
Debug.WriteLine("working", "MyWork2")
End Sub

When he uses subs.Invoke in the DoWork sub, he wants all three subs MyWork, MyWork1, and MyWork2 to be called, the only way I know to do this in VB.NET is to call Delegate.Combine itself. Something like:

Public Sub DoWork()
Dim subs As MySub = AddressOf MyWork
Dim sub1 As MySub = AddressOf MyWork1
Dim sub2 As MySub = AddressOf MyWork2

subs = DirectCast([Delegate].Combine(subs, sub1), MySub)
subs = DirectCast([Delegate].Combine(subs, sub2), MySub)

subs.Invoke()

End Sub

Normally I use a Delegate variable when I have a single function to call. In the above I would probably create a helper function to hide the DirectCast
in a function, cleaning it up significantly.

Public Sub DoWork()
Dim subs As MySub

subs = MakeWork(AddressOf MyWork, AddressOf MyWork1)
subs = MakeWork(subs, AddressOf MyWork2)

subs.Invoke()

End Sub

Public Function MakeWork(ByVal a As MySub, ByVal b As MySub) As MySub
Return DirectCast([Delegate].Combine(a, b), MySub)
End Function

Hope this helps
Jay

"CJ Taylor" <no****@blowgoats.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
See, now I can comment on this one.

Alright when it comes to multicasts like this yes you want to use AddHandler
as everyone has said.

Now after a lot of research, especially yesterday, the AddHandler keyword is
simply a reference to EventHandlerList.AddHandler (object, delegate).
At least thats the way I understand it. And EventHandlerList is created at
runtime in most cases it seems. The code for its pretty straight forward (you can find the source out there) not real complex, but when a

delegate is
multicast like this, it automatically calls Delegate.Combine.

So just use AddHandler.

=)

CJ
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in

message
news:OA**************@TK2MSFTNGP11.phx.gbl...
IcedCrow,
> I understand the combine function, but I'm only working
> with one delegate here so there's nothing to combine. Do
> I have to create multiple delegate objects that reference
> methods and then combine them all? THat seems horribly
> ineffecient...
You do realize that the "+=" operator in C# when used with delegate

types
is
shorthand for the Delegate.Combine function?

While the "-=" operator when used with delegate types is shorthand for
Delegate.Remove function?

You can use ildasm.exe to verify the above statement.

So as I stated, have you tried the Delegate.Combine function?

Further you do realize that each time you use the AddressOf operator in VB.NET that you are given a new Delegate?

Hope this helps
Jay

"IcedCrow" <ch********@aol.com> wrote in message
news:00****************************@phx.gbl...
> Greetings Jay,
>
> No I am not talking about events, I understand how to add
> handlers.
>
> I am talking delegates. A delegate object being assigned
> the address of a procedure and then having it's invoke
> method called to fire that procedure.
>
> In C# I know of how to take a single delegate and assign
> to it calls from multiple procedures, so that I only have
> to fire off a single INVOKE method from that single
> delegate to fire off the multiple methods.
>
> With VB.NET I only know of how to assign a single method
> address to a single delegate.
>
> I understand the combine function, but I'm only working
> with one delegate here so there's nothing to combine. Do
> I have to create multiple delegate objects that reference
> methods and then combine them all? THat seems horribly
> ineffecient...
>
> I don't know how familiar you are with the MOCs but in
> the 2124 C# course they have an example with a nuclear
> power plant and one invoke call to a delegate is able to
> fire off multiple calls to pump procedures.
>
> This is what I want to do with Visual Basic.NET. It has
> nothing to do with events.
>
> Thanks!



Nov 20 '05 #13

P: n/a
CJ,
Yea sometimes its hard to surmise what the original or the response is
really talking about.

Hey! what I am saying is perfectly clear in my mind! ;-)

Unfortunately sometimes the OP doesn't follow what I am mumbling about...
:-(

I'm just curious to see if we have answered IcedCrow's question.

Jay

"CJ Taylor" <no****@blowgoats.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Jay,

I completly understand where your coming from on this, and I guess I just
misunderstood what the actual question was. My apologies if I led him in
the wrong direction.

Most often, I like many people automatically associate Delegates with Events (because its the most common application for them, unelss your doing some
stuff with multi-threading and invoking methods on a parent,sibling,child
control, but usually this is associated with events as well).

So that was my point about talking about AddHandler and hopefully giving a
little insight to this.

Thanks for the clarification and useful code by the way

-CJ

<<snip>>
Nov 20 '05 #14

P: n/a
Hi Iced, Jay,

Iced Crow
|| No I am not talking about events, I understand
|| how to add handlers.
|| .........
|| This is what I want to do with Visual Basic.NET.
|| It has nothing to do with events.

Fergus
|| Events blah, blah, AddHandler, blah, blah.

My apologies - brain not in tune with eyes. I didn't give the question the
attention it deserved. :-(

Regards,
Fergus
Nov 20 '05 #15

P: n/a
Hi, try this:

Dim dMyDelegate As MyDelegateSub

dMyDelegate = [Delegate].Combine(AddressOf DelegSub1, AddressOf DelegSub2)
dMyDelegate.Invoke()

Also, if you need more than two helpings of delegate, one of the overloads
of the Combine method is an array of delegates.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
"Iced Crow" <ch********@aol.com> wrote in message
news:79****************************@phx.gbl...
: In C# I know that you can use delegates to assing
: multiple addresses of sub and functions to a delegate and
: have it fire multiple procedures...
:
: How do I do this in VB? I only know of assigning a
: single method to a delegate in VB.NET. I want to use it
: as in C#... to fire multiple events.
:
: Thanks in advance!
Nov 20 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.