Passing a delegate to a function

  • Thread starter Thread starter Dom
  • Start date Start date
D

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
 
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();
}
 
HEre is a good article.

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


Nicholas,

Wouldn't it be

delegate void DoSomething();
?





Nicholas Paldino said:
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]
- (e-mail address removed)

Dom said:
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
 
Yes, forgot the delegate keyword. Thanks for the catch.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

sloan said:
HEre is a good article.

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


Nicholas,

Wouldn't it be

delegate void DoSomething();
?





in
message news:[email protected]...
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]
- (e-mail address removed)

Dom said:
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
 
Nicholas Paldino said:
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();
 
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);
 

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

Back
Top