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
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