Pass a Delegate 2 levels instead of just one

G

Guest

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(DelegatedSampleFunction());

} // end CONSTRUCTOR


} // end Class2
} // end namespace DelegatePassing

Any input would greatly be appreciated.

Thanks,

Rob K
 
D

Dustin Campbell

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
 
D

Dustin Campbell

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(DelegatedSampleFunction());

} // 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.
 
G

Guest

Brilliant! That worked great! I put it into our large program that way and
everything works now. Thank you so much Dustin!
 
J

Jon Skeet [C# MVP]

RobKinney1 said:
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.
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top