Click here to Skip to main content
       

C#

 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand PinmemberWidmarkRob5-May-13 0:04 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand PinmvpOriginalGriff5-May-13 0:11 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand PinmemberWidmarkRob5-May-13 0:17 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand PinmemberWidmarkRob5-May-13 0:19 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand PinmemberWidmarkRob5-May-13 0:05 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand PinmvpOriginalGriff5-May-13 0:21 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand PinmemberWidmarkRob5-May-13 0:24 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand PinmvpOriginalGriff5-May-13 0:28 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand PinmemberKevin Bewley29-Apr-13 0:30 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pinmemberharold aptroot29-Apr-13 0:38 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand PinmemberKevin Bewley29-Apr-13 0:40 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pinmemberharold aptroot29-Apr-13 0:43 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand PinmemberWidmarkRob5-May-13 0:29 
Questionwhy you can't store a lambda expression in a 'dynamic variable, or 'var variable ? [modified] PinmemberBillWoodruff27-Apr-13 18:33 
// compiler error: "Cannot convert lambda expression to type 'dynamic' because it is not a delegate
dynamic dynamicFail = (o1, e1) => { MessageBox.Show("store in 'dynamic"); };
 
// compiler error: "Cannot assign lambda expression to an implicitly-typed local variable"
var varFail = (o2, e2) => { MessageBox.Show("store in 'var"); };
However, you can assign a lambda to 'Action, or 'Func:
Action<dynamic,dynamic> myAction = (o3, e3) => { MessageBox.Show("Action works"); };
Func<dynamic, dynamic, string> myFunc = (o4, e4) => {return "Func works";};
In the MSDN docs, there is a comment on 'Func, that:
Quote:
The underlying type of any lambda expression is a Func.
And, of course, in the MSDN docs for Lambda Expressions, you are told a lambda is an anonymous function "you can use to create delegates or expression tree types."
 
I am having trouble forming some kind of useful "mental model" about exactly what a lambda is, and how it can be used (in spite of having Skeet's 2nd. edition of "C# in Depth" at hand).
 
I know we can define an Event with a lambda: so, a lambda can become an EventHandler:
button1.Click += (obj, eva) => { MessageBox.Show("button1 clicked"); };
Where this "comes home to roost" for me is when I want to pass a lambda expression as a parameter to a method. You can certainly pass an Action<dynamic,dynamic> to a method, as a parameter:
private void MethodTakesAFunc(Func<dynamic, dynamic, string> theFuncParam){}
While you can execute a passed in Func like this:
private void MethodTakesAFunc(Func<dynamic, dynamic, string> theFuncParam)
{
    MessageBox.Show(theFuncParam(null, null));
}
To me this way of getting execution by passing in null parameter stubs just seems absolutely weird: is there a simpler way ?
 
I see a similar problem with passing a lambda expression as a parameter to a method that then attempts to assign that lambda to an EventHandler.
 
Right now the only way I know to allow either an EventHandler, MouseEventHandler, or KeyPressHandler, to be passed in to a method, and then validly "wired up" to a Control's Event is something like this:
private void BindControlToClickEvent(Control theControl, dynamic theEventParam)
{
    theControl.Click += theEventParam;
}
 
// create a new EventHandler
EventHandler myClickEvent = new EventHandler((obj, evn) => { MessageBox.Show("myClickEvent");});
// bind the new EventHandler to the Click Event of button2
BindControlToClickEvent(button2, myClickEvent);
And that seems kind of "kinky."
 
I hope I have exposed enough of my confusion that diagnosis of my condition can proceed Smile | :) I'd really appreciate any guidance !
 
thanks, Bill

Humans are amphibians: half spirit, half animal; as spirits they belong to the eternal world; as animals they inhabit time. While their spirit can be directed to an eternal object, their bodies, passions, and imagination are in continual change, for to be in time, means to change. Their nearest approach to constancy is undulation: repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis


modified 28-Apr-13 1:18am.

AnswerRe: why you can't store a lambda expression in a 'dynamic variable, or 'var variable ? PinmemberSledgeHammer0127-Apr-13 18:54 
GeneralRe: why you can't store a lambda expression in a 'dynamic variable, or 'var variable ? PinmemberBillWoodruff27-Apr-13 19:41 
GeneralRe: why you can't store a lambda expression in a 'dynamic variable, or 'var variable ? [modified] PinmemberSledgeHammer0127-Apr-13 20:07 
AnswerRe: why you can't store a lambda expression in a 'dynamic variable, or 'var variable ? PinprofessionalRichard Deeming29-Apr-13 2:19 
GeneralRe: why you can't store a lambda expression in a 'dynamic variable, or 'var variable ? PinmemberBillWoodruff29-Apr-13 14:07 
QuestionExtract Date part and pass only Date Pinmembermeeram3927-Apr-13 4:24 
AnswerRe: Extract Date part and pass only Date PinmvpDave Kreskowiak27-Apr-13 5:31 
AnswerRe: Extract Date part and pass only Date Pinmemberjschell27-Apr-13 11:15 
GeneralJogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview PinmemberMember 1000184526-Apr-13 13:15 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview [modified] Pinmemberdusty_dex26-Apr-13 13:36 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview PinmemberMember 1000184526-Apr-13 15:30 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview PinmvpDave Kreskowiak26-Apr-13 17:16 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview PinmemberMember 1000184526-Apr-13 23:54 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview PinmvpDave Kreskowiak27-Apr-13 4:09 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview PinmvpRichard MacCutchan26-Apr-13 22:25 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview PinmemberMember 1000184526-Apr-13 23:45 
QuestionData in XmlFiles or in Database [modified] PinmemberFrygreen26-Apr-13 7:42 
AnswerRe: Data in XmlFiles or in Database PinmvpDave Kreskowiak26-Apr-13 7:52 
GeneralRe: Data in XmlFiles or in Database PinmemberFrygreen26-Apr-13 8:08 
GeneralRe: Data in XmlFiles or in Database PinmvpDave Kreskowiak26-Apr-13 8:22 
AnswerRe: Data in XmlFiles or in Database PinmemberJasmine250126-Apr-13 11:31 
GeneralRe: Data in XmlFiles or in Database Pinmemberjschell27-Apr-13 11:21 
GeneralRe: Data in XmlFiles or in Database PinmemberJasmine250128-Apr-13 6:26 
GeneralRe: Data in XmlFiles or in Database Pinmemberjschell29-Apr-13 7:45 
GeneralRe: Data in XmlFiles or in Database PinmemberJasmine250129-Apr-13 13:13 
GeneralRe: Data in XmlFiles or in Database Pinmemberjschell30-Apr-13 9:44