Passing Delegates as parameters

S

Simon Woods

Hi

I am trying to get my head around delegates (and MVC)...

I have a simple app ... a view form, a popup form and a controller class

When a particular key is pressed in the view, I want to popup the popup.
Then, when a particular key is pressed in the popup I want to return the
selected value in the popup form to the view. I am using the controller
class as an intermediary between the two.

Here's a clip from the controller:

//member
SetValue _setValue;


GetValue IController.GetPopupValue
{
get
{
return value =>
{
_setValue.Invoke(value);
};
}
}

SetValue IController.SetPopupValue
{
get
{
return _setValue;
}
}

....

public delegate void GetValue(string value);
public delegate void SetValue(string value);



As you can see,

1) I have a lambda, GetPopupValue, which receives the value from the
popup (that's working fine) which invokes the delegate
2) I have a delegate called SetValue defined in the controller. It gets
passed into the view on it's construction.

public TextBox(SetValue popupValue)
{
popupValue= delegate(string value)
{
_txtMain.AppendText(value);
};
}

What I was effectively wanting was to be able to assign the action to be
taken when the delegate is passed into the view, so that when the
delegate is invoked in the controller, the action occurs in the view.

However, I'm getting a null reference exception when I try and invoke it
in the view , as I sort of expected, since the action to be taken
against that definition is defined in the view.

Can someone explain, how I should be wiring this up so that, effectively
I get remote execution of the delegate, declared and invoked in the
controller yet defined and executed in the view?

Many thx

Simon
 
F

Frank Uray

Hi Simon

I am not exactly sure what you want to do,
but here is a little delegation sample with parameter,
maybe this helps:

delegate void delegateUpdateProgress(int local_ProgressValue);

this.Invoke(new delegateUpdateProgress(this.UpdateProgress), new object[] {
'some value' });

private void UpdateProgress(int local_ProgressValue)
{ }

Or maybe you want to create a event within you class(es).

Regards
Frank Uray
 
S

Simon Woods

Thanks Frank

I'll give this some thought to see if it fits what I am after, or, more
likely, adjusts the way I'm thinking about this.

I think my key paragraph was that I wanted to "get remote execution of
the delegate, declared and invoked in the controller yet defined and
executed in the view?"

Ideally, and in principle, what I'm after is passing around bits of
code/functions to execute rather than exposing methods and passing
around data which those methods use/manipulate.

I seem to have achieved this with the lambdas for communicating between
controller and popup whereby the controller tells the popup what events
it should respond to and what to do when those events occur. Effectively
the controller has passed in some code which the popup executes when it
sees fit.

However, I can't get my head around how to wire things up so that when
something has been received from the popup back in the controller, it
can then effectively tells (I suppose - raises an event to) the view
"update yourself (however you need to) with this value".

I don't know if this can be achieved with delegates or whether as you
suggest I may need to use events.


Thanks again

Simon

Frank said:
Hi Simon

I am not exactly sure what you want to do,
but here is a little delegation sample with parameter,
maybe this helps:

delegate void delegateUpdateProgress(int local_ProgressValue);

this.Invoke(new delegateUpdateProgress(this.UpdateProgress), new object[] {
'some value' });

private void UpdateProgress(int local_ProgressValue)
{ }

Or maybe you want to create a event within you class(es).

Regards
Frank Uray


Simon Woods said:
Hi

I am trying to get my head around delegates (and MVC)...

I have a simple app ... a view form, a popup form and a controller class

When a particular key is pressed in the view, I want to popup the popup.
Then, when a particular key is pressed in the popup I want to return the
selected value in the popup form to the view. I am using the controller
class as an intermediary between the two.

Here's a clip from the controller:

//member
SetValue _setValue;


GetValue IController.GetPopupValue
{
get
{
return value =>
{
_setValue.Invoke(value);
};
}
}

SetValue IController.SetPopupValue
{
get
{
return _setValue;
}
}

....

public delegate void GetValue(string value);
public delegate void SetValue(string value);



As you can see,

1) I have a lambda, GetPopupValue, which receives the value from the
popup (that's working fine) which invokes the delegate
2) I have a delegate called SetValue defined in the controller. It gets
passed into the view on it's construction.

public TextBox(SetValue popupValue)
{
popupValue= delegate(string value)
{
_txtMain.AppendText(value);
};
}

What I was effectively wanting was to be able to assign the action to be
taken when the delegate is passed into the view, so that when the
delegate is invoked in the controller, the action occurs in the view.

However, I'm getting a null reference exception when I try and invoke it
in the view , as I sort of expected, since the action to be taken
against that definition is defined in the view.

Can someone explain, how I should be wiring this up so that, effectively
I get remote execution of the delegate, declared and invoked in the
controller yet defined and executed in the view?

Many thx

Simon
 
P

Pavel Minaev

I'll give this some thought to see if it fits what I am after, or, more
likely, adjusts the way I'm thinking about this.

I think my key paragraph was that I wanted to "get remote execution of
the delegate, declared and invoked in the controller yet defined and
executed in the view?"

Ideally, and in principle, what I'm after is passing around bits of
code/functions to execute rather than exposing methods and passing
around data which those methods use/manipulate.

That's a bit strange way to implement MVC. Normally, what Controller
passes to the view isn't just "code" - it's domain objects (Model)
from which the View then retrieves data (e.g. via data binding).
I seem to have achieved this with the lambdas for communicating between
controller and popup whereby the controller tells the popup what events
it should respond to and what to do when those events occur. Effectively
the controller has passed in some code which the popup executes when it
sees fit.

However, I can't get my head around how to wire things up so that when
something has been received from the popup back in the controller, it
can then effectively tells (I suppose - raises an event to) the view
"update yourself (however you need to) with this value".

Yeah, that's also why it wasn't such a good idea in the first place.
When you pass in an object, then you could use any of the
standard .NET patterns for change notification of properties of that
object (e.g. INotifyPropertyChanged, or ...Changed events). Then your
view would just subscribe to those. Or, better yet, if you're using
WinForms or WPF, it would do it all for you if you use data binding.
 
S

Simon Woods

Thx vm Pavel

Pavel said:
That's a bit strange way to implement MVC. Normally, what Controller
passes to the view isn't just "code" - it's domain objects (Model)
from which the View then retrieves data (e.g. via data binding).


It may well be. I suppose it is just an experiment. I wondered whether
MVC could be implemented 'functionally' rather than using OO. Is there
any chance you could comment on why that would not be appropriate? (I
accept the pragmatic argument you suggest below so there's stable,
well-tested mechanisms already in place. Are there arguments in
principle against it?) Thx

Yeah, that's also why it wasn't such a good idea in the first place.
When you pass in an object, then you could use any of the
standard .NET patterns for change notification of properties of that
object (e.g. INotifyPropertyChanged, or ...Changed events). Then your
view would just subscribe to those. Or, better yet, if you're using
WinForms or WPF, it would do it all for you if you use data binding.

Okay ... that's very helpful.

Which I guess also sort of answers my question above. One of the
benefits of using OO is that it encapsulates a wide varieties of
behaviour which I would need to specify as individual parameters when
trying to wire up the various MVC components. No?
 
P

Pavel Minaev

It may well be. I suppose it is just an experiment. I wondered whether
MVC could be implemented 'functionally' rather than using OO. Is there
any chance you could comment on why that would not be appropriate? (I
accept the pragmatic argument you suggest below so there's stable,
well-tested mechanisms already in place. Are there arguments in
principle against it?) Thx

Well, MVC is initially an OO pattern, and in most its incarnations is
centered around Observer (View subscribing to modifications of Model),
which is also an OO pattern. Also, the very idea of Model as something
having mutable state which View reflects is rather alien to FP. Of
course you can still do it without objects, by passing delegates in
_both_ directions (to View to let it get the data, and from View to
let it be notified when it should re-get the data), but that doesn't
really buy you anything - you still effectively end up having a
property (delegate that is passed to View), an event (delegate that is
passed from View), and state - that is, an object.

If you want to see an investigation of properly functional approaches
to UI, have a look at this paper:

http://www.haskell.org/jcp/hw04.pdf
 
S

Simon Woods

Pavel said:
Well, MVC is initially an OO pattern, and in most its incarnations is
centered around Observer (View subscribing to modifications of Model),
which is also an OO pattern. Also, the very idea of Model as something
having mutable state which View reflects is rather alien to FP. Of
course you can still do it without objects, by passing delegates in
_both_ directions (to View to let it get the data, and from View to
let it be notified when it should re-get the data), but that doesn't
really buy you anything - you still effectively end up having a
property (delegate that is passed to View), an event (delegate that is
passed from View), and state - that is, an object.

If you want to see an investigation of properly functional approaches
to UI, have a look at this paper:

http://www.haskell.org/jcp/hw04.pdf

That's very interesting. Many thanks, Pavel
 

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