473,651 Members | 3,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delegate question

Alright,

I probably wont be able to get this question answered, but maybe someone has
even more experience than me with delegates... Wow... that sounded
fecisious. =)

So, I'm trying to dynamically create a delegate where the parameters match
on the base level (position is the same, and there base types are the same.)
i.e.

I have an event defined as

Public event myEvent (sender as object, e as MyEventArgs)

now myEventArgs is inherited from System.EventArg s

so I have a method I'm trying to hook to through a delegate... that is
defined as

public sub mySub(sender as object, e as System.EventArg s)

Ok, so signitures don't match, which shouldn't allow a delegate to be
created because it breaks the contract (even though its inherited).

So I try creating a new delegate using System.Delegate .CreateDelegate (Type,
target, method)

so I have this as of right now

delTemp = System.Delegate .CreateDelegate (GetType(System .EventHandler),
objTarget, methInfo.Name)

evtInfo.AddEven tHandler(objHol der, delTemp)

now, this works for creating the delegate, but when I add the hander I get
an error that says "Cannot Bind to Method"

Useless.

So I tried to add in the code

evtInfo.AddEven tHandler(objHol der, ctype(delType,
gettype(evtInfo .EventHandlerTy pe)) to convert the delegate to the target
method delegate... No dice...

I get an error on the Ctype... So I'm not sure what to do besides define
events as basic System.Object, System.EventArg s and just pass the data whole
into it.. which may be the best way, but was seeing if anyone else undertood
my plight.

Thanks

CJ


Nov 20 '05 #1
1 2735
I'm the delegate master. Über Delegate Geek.

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

Please respond to the newsgroup,
so all can benefit

"Chaos, Panic, Disorder, my work here is done"
"CJ Taylor" <no****@blowgoa ts.com> wrote in message
news:vn******** ****@corp.super news.com...
: Alright,
:
: I probably wont be able to get this question answered, but maybe someone
has
: even more experience than me with delegates... Wow... that sounded
: fecisious. =)
:
: So, I'm trying to dynamically create a delegate where the parameters match
: on the base level (position is the same, and there base types are the
same.)
: i.e.
:
: I have an event defined as
:
: Public event myEvent (sender as object, e as MyEventArgs)
:
: now myEventArgs is inherited from System.EventArg s
:
: so I have a method I'm trying to hook to through a delegate... that is
: defined as
:
: public sub mySub(sender as object, e as System.EventArg s)
:
: Ok, so signitures don't match, which shouldn't allow a delegate to be
: created because it breaks the contract (even though its inherited).
:
: So I try creating a new delegate using
System.Delegate .CreateDelegate (Type,
: target, method)
:
: so I have this as of right now
:
: delTemp = System.Delegate .CreateDelegate (GetType(System .EventHandler),
: objTarget, methInfo.Name)
:
: evtInfo.AddEven tHandler(objHol der, delTemp)
:
:
:
: now, this works for creating the delegate, but when I add the hander I get
: an error that says "Cannot Bind to Method"
:
: Useless.
:
: So I tried to add in the code
:
: evtInfo.AddEven tHandler(objHol der, ctype(delType,
: gettype(evtInfo .EventHandlerTy pe)) to convert the delegate to the target
: method delegate... No dice...
:
: I get an error on the Ctype... So I'm not sure what to do besides define
: events as basic System.Object, System.EventArg s and just pass the data
whole
: into it.. which may be the best way, but was seeing if anyone else
undertood
: my plight.
:
:
:
: Thanks
:
: CJ
:
:
:
:
Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3406
by: sg71.cherub | last post by:
Here I am meeting a problem. I am transfering unmanaged C/C++ codes to managed C# codes. For the general C/C++ function pointer, it is easy to implement its function by C# delegate. e.g. Declare (C/C++): double (* f) (double * x, void * parms); Declare (C#): public delegate double f (double x, object parms); And the ways to use them are logically similiar.
4
2007
by: ^MisterJingo^ | last post by:
Hi all, I've been trying to get my head around delegates. The book i'm using had a single example, not much explaination, and didn't show how to set up a delegate and pass variables in and out of the functions it refers to. So I've been playing around and came up with he following code. I know it doesn't do much, but I just wanted to fit together delegates and parameter passing: //////////////// Code start
1
1815
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the code, as there's simply too much, but here's the general gist: I have a connection object which connects to a custom back-end server of one type or another. Clients of this connection object send requests via a method (e.g....
3
3278
by: Peter Duniho | last post by:
I was reminded in a recent post (http://groups.google.com/group/microsoft.public.dotnet.framework/msg/e649e992db857691?dmode=source) that in C# one can use anonymous delegates without explicitly creating a new delegate instance. However, I still run into situations in which the compiler complains. For example, I get a compiler error here: Invoke(delegate() { /* some code */ });
4
1780
by: Bob Cramer | last post by:
I don't have a copy of Reflector handy :-( so I'd appreciate if someone could confirm (or clarify) the following. In consideration of the following delegate declaration... delegate string MyLovelyDelegate(int parm1, string parm2); 1. Is it true that, in the output assembly, a new class will be created that inherits from System.MulticastDelegate - and the name of that class is
2
427
by: Tony Johansson | last post by:
Hello!! Below is a program copied from a book. The program is easy but there is one thing about the program that I would like to have some more info about. Assume that you have for example 10 cars in the array gameCars. Assume also that you want to subscribe to an event for some cars located in the array gameCars.
6
1396
by: damiensawyer | last post by:
Hi, Can someone please explain to me something about delegates? My understanding is as follows. A delegate is basically an object that can hold a reference to a "method" somewhere. That is, it's essentially a pointer to a piece of code somewhere else in memory. It therefore (to me anyway) makes sense to define delegates with their signatures and be able to use those signatures at different points in
8
4373
by: Advait Mohan Raut | last post by:
Hello, I am using VC# 2005 ; C# 2.0 I am working on the performance measurement tool. For that I need my code to call user defined method along with its parameters. For that should I use a generic delegate with variable length argument or something else ? if it is then how to use it ? AA advait
3
1513
by: puzzlecracker | last post by:
Sort of new to internal of delegates. My question is when you create a delegate instance, does the surrounding block must be able to see/access method you want delegate to take on. Here is concrete example: namespace Test public delegate string MyDelegate (int x); class A {
0
8361
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
8807
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
8701
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...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7299
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...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5615
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();...
1
2701
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
1
1912
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.