473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.Class1.SampleDelegate' to
'sampleApp.Class2.SampleDelegate'

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DelegatePassing
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();

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

private String SuperCoolFunction()
{
// do some rad stuff
// ......

return "Well, that was fun!";

} // end SuperCoolFunction ()

} // 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 DelegatedSampleFunction;

public Class1(SampleDelegate refToSuperCoolFunction)
{
this.DelegatedSampleFunction = refToSuperCoolFunction; // make
the link

Class2 myNewClassAgain = new Class2(this.DelegatedSampleFunction);

} // 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 DelegatedSampleFunction;

public Class2(SampleDelegate refAllTheWayBackToParent)
{
this.DelegatedSampleFunction = refAllTheWayBackToParent;

// print the results (should be linking all the way back to
Form1 through class1)
System.Windows.Forms.MessageBox.Show(DelegatedSamp leFunction());

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

Any input would greatly be appreciated.

Thanks,

Rob K
Oct 12 '06 #1
6 1681
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 DelegatedSampleFunction;

public Class1(SampleDelegate refToSuperCoolFunction)
{
this.DelegatedSampleFunction = refToSuperCoolFunction; // make
the link

Class2 myNewClassAgain = new Class2(this.DelegatedSampleFunction);

} // 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 DelegatedSampleFunction;

public Class2(SampleDelegate refAllTheWayBackToParent)
{
this.DelegatedSampleFunction = refAllTheWayBackToParent;
// print the results (should be linking all the way back to
Form1 through class1)
System.Windows.Forms.MessageBox.Show(DelegatedSamp leFunction());

} // 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********@discussions.microsoft.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.com>
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********@discussions.microsoft.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.com>
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
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
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...
8
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
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
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
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...
24
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...
12
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....
2
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...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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...
0
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...
0
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,...

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.