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! 15 6591
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
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!
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...
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!
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!
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!
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]
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!
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!
"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/
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!
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!
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>>
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
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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
| |