473,811 Members | 3,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delegates and assigning multiple procedures? How?

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
15 6634
"IcedCrow" <ch********@aol .com> wrote in news:096701c37e 43$a4dc8590
$a*******@phx.g bl:
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********@rog ers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 20 '05 #11
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 EventHandlerLis t.AddHandler (object, delegate). At
least thats the way I understand it. And EventHandlerLis t 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.Combin e.

So just use AddHandler.

=)

CJ
"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message
news:OA******** ******@TK2MSFTN GP11.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.Combin e 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.Combin e 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
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********@ema il.msn.com> wrote in message
news:u1******** ******@TK2MSFTN GP11.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.Combin e 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(Addres sOf 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****@blowgoa ts.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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 EventHandlerLis t.AddHandler (object, delegate).
At least thats the way I understand it. And EventHandlerLis t 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.Combin e.

So just use AddHandler.

=)

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

message
news:OA******** ******@TK2MSFTN GP11.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.Combin e 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.Combin e 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
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****@blowgoa ts.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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
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
Hi, try this:

Dim dMyDelegate As MyDelegateSub

dMyDelegate = [Delegate].Combine(Addres sOf DelegSub1, AddressOf DelegSub2)
dMyDelegate.Inv oke()

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
1872
by: Michi Henning | last post by:
Below is a bit of code that creates a delegate. The delegate invokes a member function of a struct. The code compiles, but has surprising behavior: using System; namespace ConsoleApplication1 { public struct SimpleStruct {
3
1207
by: Robert Batt | last post by:
Hello, I'm trying to modularize the code in my forms by putting some procedures and delegates in a seperate class. However I try to put a delegate with the following header in a class. Public Sub DecimalToCurrencyString(ByVal sender As Object, ByVal cevent As ConvertEventArgs) However I get a syntax error on ConvertEventArgs, saying
4
3416
by: Jarod_24 | last post by:
What is the point with Delegates in VB.Net What can these things do that we can not allready do with the use of Interfaces, Events and Event handlers and so on... I'd like a discussion on this, and some practical examples where Delegates would be better/worse to use...
13
380
by: heddy | last post by:
Using Jesse Liberty's excellent book on C#, I am looking at chapter 12 - Delegates. Now, I was under the impression that a delegate is essentially a pointer to a member function. However, when I look at Jesse's code and explanation of it, I just don't get it. His code looks like this: #region Using directives using System;
6
2638
by: =?Utf-8?B?Sko=?= | last post by:
I have a logger component that logs to multiple sources, ie textfile, eventlog etc. and I have two methods that depending on where I call up my logger comp. one of them will be called. For ex. if I throw an exception I want to call one method and if I dont, I am just logging some info to eventlog, I will call the other. Now i'm wondering would it make sense to use delegates, one for each method to call methods in my window service? How...
7
3425
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that uses the "WithEvents" and events. There is another example on page 124 that shows how to use delegates to sort an array. I don't understand the difference between events and delegates. Are they redundant features? How do I decide which to use? ...
12
206
by: tshad | last post by:
I have a set up javascript functions that pass function pointers and I am trying to figure out how to do the same thing in C# using delegates. // We define some simple functions here function add(x,y) {return x + y;} function subtract(x,y) {return x - 1; } function multiply(x,y) {return x * 1; }
9
3120
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying to store multicast delegates in a hash table, and then fire the delegates one of two ways (after registering/ creating the delegates, etc).
2
1885
by: BobLewiston | last post by:
The concept of delegates (references to methods) per se is new to me (although I used function pointers in C years ago), and although the literature acknowledges that there are significant differences between delegates and the use of pointers to objects, I'm nonetheless a little confused by how similar the syntax used for references to methods is to that used for references to objects. Recently at a few different forums I posted a brief...
0
9727
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.