473,385 Members | 1,192 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,385 software developers and data experts.

Passing a delegate to a function

Dom
Let's say I do this:

MyObject o = new MyObject (this, DoWork);

.... where DoThis is a function in the calling class. How do I pick
this up in the constructor, MyObject?

public MyObject (Form OriginatingForm, <???ptrDoWork)
OriginatingForm.Invoke (ptrDoWork)
How do I replace <???>.

Dom

May 19 '07 #1
5 4228
Dom,

Well, you have to have a delegate defined. It looks like it has no
parameters, and no return value, so you can define a delegate like so:

public void DoSomething();

Then, your constructor would be defined as:

public MyObject(Form originatingForm, DoSomething doSomething)
{
OriginatingForm.Invoke(doSomething);
}

Although if you know the object is going to be constructed on the UI
thread, then you can just do:

public MyObject(Form originatingForm, DoSomething doSomething)
{
doSomething();
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dom" <do********@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
Let's say I do this:

MyObject o = new MyObject (this, DoWork);

... where DoThis is a function in the calling class. How do I pick
this up in the constructor, MyObject?

public MyObject (Form OriginatingForm, <???ptrDoWork)
OriginatingForm.Invoke (ptrDoWork)
How do I replace <???>.

Dom
May 19 '07 #2


HEre is a good article.

http://msdn.microsoft.com/msdnmag/issues/04/05/C20/
Nicholas,

Wouldn't it be

delegate void DoSomething();
?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:24**********************************@microsof t.com...
Dom,

Well, you have to have a delegate defined. It looks like it has no
parameters, and no return value, so you can define a delegate like so:

public void DoSomething();

Then, your constructor would be defined as:

public MyObject(Form originatingForm, DoSomething doSomething)
{
OriginatingForm.Invoke(doSomething);
}

Although if you know the object is going to be constructed on the UI
thread, then you can just do:

public MyObject(Form originatingForm, DoSomething doSomething)
{
doSomething();
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dom" <do********@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
Let's say I do this:

MyObject o = new MyObject (this, DoWork);

... where DoThis is a function in the calling class. How do I pick
this up in the constructor, MyObject?

public MyObject (Form OriginatingForm, <???ptrDoWork)
OriginatingForm.Invoke (ptrDoWork)
How do I replace <???>.

Dom

May 19 '07 #3
Yes, forgot the delegate keyword. Thanks for the catch.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"sloan" <sl***@ipass.netwrote in message
news:OL**************@TK2MSFTNGP06.phx.gbl...
>

HEre is a good article.

http://msdn.microsoft.com/msdnmag/issues/04/05/C20/
Nicholas,

Wouldn't it be

delegate void DoSomething();
?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in
message news:24**********************************@microsof t.com...
>Dom,

Well, you have to have a delegate defined. It looks like it has no
parameters, and no return value, so you can define a delegate like so:

public void DoSomething();

Then, your constructor would be defined as:

public MyObject(Form originatingForm, DoSomething doSomething)
{
OriginatingForm.Invoke(doSomething);
}

Although if you know the object is going to be constructed on the UI
thread, then you can just do:

public MyObject(Form originatingForm, DoSomething doSomething)
{
doSomething();
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dom" <do********@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegr oups.com...
Let's say I do this:

MyObject o = new MyObject (this, DoWork);

... where DoThis is a function in the calling class. How do I pick
this up in the constructor, MyObject?

public MyObject (Form OriginatingForm, <???ptrDoWork)
OriginatingForm.Invoke (ptrDoWork)
How do I replace <???>.

Dom

May 19 '07 #4
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
Well, you have to have a delegate defined. It looks like it has no
parameters, and no return value, so you can define a delegate like so:

public void DoSomething();
Correction to typo - to declare the delegate type, you need:

public delegate void DoSomething();

--
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
May 19 '07 #5
MyObject o = new MyObject (this, DoWork);

... where DoThis is a function in the calling class. How do I pick
this up in the constructor, MyObject?
OriginatingForm is redundant because the DoWork stored a reference to
OriginatingForm when it was created. Delegate's have to reference a
class instance when pointing to an instance method. Then you can
call:

((Form)DoWork.Target).Invoke(DoWork);

May 21 '07 #6

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

Similar topics

3
by: ogled | last post by:
Hi I have an odd question: is it possible to have a delegate that recieves a methodname dynamically? Like this: aClassObj.OnGo += new ANamespace.Invoke(fName); where fName would be a string...
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
1
by: Peter | last post by:
Hello, Thanks for reviewing my question. I am coming from a C++ background and would like to know how do you pass a function as a parameter to a function in C#? I am assume you must use a...
5
by: Mike | last post by:
Hi! I am trying to write a function that will span a thread for me. Eventually I want to incorporate this finction into a class: public void SpanThread(string threadName,string threadDelegate...
2
by: MrPugh | last post by:
Hi! I have a strange problem: I need to wrap a C-function in a native DLL into a C#-class. This function expects one C-function pointer out of a set of some C-functions defined in the same...
1
by: Dave Booker | last post by:
If we have: public delegate void Test(string s); public event Test testevent; public void log(string s) {...} What is the difference between: testevent += log;
1
by: User1014 | last post by:
Since you can pass a function to a ... erm...... function.... how to you use the result of a function as the argument for another function instead of passing the actual function to it. i.e. ...
18
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
6
by: Sergey Poberezovskiy | last post by:
I have the following code in C# that I have trouble converting to VB(2.0): private delegate void openDialog(); private void openWindowsDialog(openDialog open) { Thread thread = new Thread(new...
10
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.