473,545 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pass a Delegate 2 levels instead of just one

Hello,

This may be an easy question, but I cannot stumle upon the correct way of
doing this. I have a function in a base class. I then pass it to a class as
a delegate so I don't have to repeat the code within that class. But then
that class, calls another class and needs that same function back in the
parent. How do I keep passing that delegate up the chain?

I am getting errors that says:
Cannot convert type 'sampleApp.Clas s1.SampleDelega te' to
'sampleApp.Clas s2.SampleDelega te'

Here is a small code sample that doesn't compile:

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace DelegatePassing
{
public partial class Form1 : Form
{

public Form1()
{
InitializeCompo nent();

// create new Class1 and pass in our super cool delegate
Class1 myNewClass = new Class1(SuperCoo lFunction);
}

private String SuperCoolFuncti on()
{
// do some rad stuff
// ......

return "Well, that was fun!";

} // end SuperCoolFuncti on ()

} // end class Form1
} // end namespace DelegatePassing

..... next class ...........

namespace DelegatePassing
{
class Class1
{
// ready a delegate to grab this from parent class (Form1.cs)
public delegate String SampleDelegate( );
SampleDelegate DelegatedSample Function;

public Class1(SampleDe legate refToSuperCoolF unction)
{
this.DelegatedS ampleFunction = refToSuperCoolF unction; // make
the link

Class2 myNewClassAgain = new Class2(this.Del egatedSampleFun ction);

} // end CONSTRUCTOR
} //end Class1
} // end namespace DelegatePassing

..... next class .............

namespace DelegatePassing
{
class Class2
{
// ready a delegate to grab this from parent class (Form1.cs)
public delegate String SampleDelegate( );
SampleDelegate DelegatedSample Function;

public Class2(SampleDe legate refAllTheWayBac kToParent)
{
this.DelegatedS ampleFunction = refAllTheWayBac kToParent;

// print the results (should be linking all the way back to
Form1 through class1)
System.Windows. Forms.MessageBo x.Show(Delegate dSampleFunction ());

} // end CONSTRUCTOR
} // end Class2
} // end namespace DelegatePassing

Any input would greatly be appreciated.

Thanks,

Rob K
Oct 12 '06 #1
6 1695
Ahhhh!!! Sorry about the double post. I am not sure how that happened.

Oct 12 '06 #2
Hi,

It sounds to me as if you have declared your delegate twice: once in each
class. Just declare it once outside the classes.

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 12 '06 #3
IOW:

namespace DelegatePassing
{
public delegate String SampleDelegate( );
}

namespace DelegatePassing
{
class Class1
{
// ready a delegate to grab this from parent class (Form1.cs)
SampleDelegate DelegatedSample Function;

public Class1(SampleDe legate refToSuperCoolF unction)
{
this.DelegatedS ampleFunction = refToSuperCoolF unction; // make
the link

Class2 myNewClassAgain = new Class2(this.Del egatedSampleFun ction);

} // end CONSTRUCTOR

} //end Class1
} // end namespace DelegatePassing

..... next class .............

namespace DelegatePassing
{
class Class2
{
// ready a delegate to grab this from parent class (Form1.cs)
SampleDelegate DelegatedSample Function;

public Class2(SampleDe legate refAllTheWayBac kToParent)
{
this.DelegatedS ampleFunction = refAllTheWayBac kToParent;
// print the results (should be linking all the way back to
Form1 through class1)
System.Windows. Forms.MessageBo x.Show(Delegate dSampleFunction ());

} // end CONSTRUCTOR

} // end Class2
} // end namespace DelegatePassing

When you declare the delegate twice, nested in two different types, you have
created two different delegate types that aren't compatible.

Best Regards,
Dustin Campbell
Developer Express Inc.

Oct 12 '06 #4
Brilliant! That worked great! I put it into our large program that way and
everything works now. Thank you so much Dustin!
Oct 12 '06 #5
RobKinney1 <Ro********@dis cussions.micros oft.comwrote:
This may be an easy question, but I cannot stumle upon the correct way of
doing this. I have a function in a base class. I then pass it to a class as
a delegate so I don't have to repeat the code within that class. But then
that class, calls another class and needs that same function back in the
parent. How do I keep passing that delegate up the chain?
As others have said, you need to declare the delegate type just the
once.

See http://www.pobox.com/~skeet/csharp/events.html for details on the
difference between declaring a delegate type and creating an instance
of the delegate - as well as events.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 12 '06 #6
Hello Jon,

Thanks for the additional info. I see you post in here quite often and I
like your website. I use it sometimes for reference. The delegate issue
makes much more sense to me now.

Thanks again!

Rob K

"Jon Skeet [C# MVP]" wrote:
RobKinney1 <Ro********@dis cussions.micros oft.comwrote:
This may be an easy question, but I cannot stumle upon the correct way of
doing this. I have a function in a base class. I then pass it to a class as
a delegate so I don't have to repeat the code within that class. But then
that class, calls another class and needs that same function back in the
parent. How do I keep passing that delegate up the chain?

As others have said, you need to declare the delegate type just the
once.

See http://www.pobox.com/~skeet/csharp/events.html for details on the
difference between declaring a delegate type and creating an instance
of the delegate - as well as events.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 12 '06 #7

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

Similar topics

4
21746
by: yoramo | last post by:
hello can I pass a static method as a parameter to a method? if the answer is yes how do I do that ? how do I call the method ? yoramo.
1
1789
by: VM | last post by:
How can I pass a delegate to another method? In my code (win app), i update my datagrid with the datatable after the method loadAuditFileToTable is finished executing. Instead, I'd like to be able to to update the grid continuously (while the table's filling) and someone suggested I pass a callback to loadAuditFileToTable and run it every X...
8
4531
by: Phill | last post by:
All the event handlers seem to pass an Object and an EventArgs object. If the event doesn't need this info why pass them anyway? It is inefficient.
6
10817
by: Tee | last post by:
Hi, Can anyone tell me if it's possible to pass a method to another class? Example of what I would like to do: class1: public void MyMethod() {
4
2421
by: KC Eric | last post by:
Hi all, I have a dll file, it has a class, say: class Temp, this class has a function which has a delegate as a parameter, say: public void Test(GameOverHandler _overHandler)
1
273
by: RobKinney1 | last post by:
Hello, This may be an easy question, but I cannot stumle upon the correct way of doing this. I have a function in a base class. I then pass it to a class as a delegate so I don't have to repeat the code within that class. But then that class, calls another class and needs that same function back in the parent. How do I keep passing that...
24
55077
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new EventHandler(Onbutton_click); I want to pass more information related that event. & want to use that
12
11031
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed!...
2
1723
by: =?Utf-8?B?Unlhbg==?= | last post by:
I have the following code in vs2005; line number is added for convenience: 1 private void Form1_Load(object sender, EventArgs e) 2 { 3 txtMyField.Enabled = true; 4 Form2 f = new Form2(); 5 f.DoUpdate += new Form2.UpdateHandler(MyForm2_ButtonClicked); 6 } 7 private void MyForm2_ButtonClicked(object sender,...
0
7420
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...
0
7934
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...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6003
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...
1
5349
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...
0
4966
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...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1908
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
1033
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.