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 6 1695
Ahhhh!!! Sorry about the double post. I am not sure how that happened.
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.
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.
Brilliant! That worked great! I put it into our large program that way and
everything works now. Thank you so much Dustin!
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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...
|
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.
|
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()
{
|
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)
| |
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...
|
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
|
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!...
|
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,...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |