Click here to Skip to main content
12,698,913 members (21,585 online)
   

C#

 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
WidmarkRob5-May-13 1:24
memberWidmarkRob5-May-13 1:24 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff5-May-13 1:28
mvpOriginalGriff5-May-13 1:28 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
Kevin Bewley29-Apr-13 1:30
memberKevin Bewley29-Apr-13 1:30 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot29-Apr-13 1:38
memberharold aptroot29-Apr-13 1:38 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
Kevin Bewley29-Apr-13 1:40
memberKevin Bewley29-Apr-13 1:40 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot29-Apr-13 1:43
memberharold aptroot29-Apr-13 1:43 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
WidmarkRob5-May-13 1:29
memberWidmarkRob5-May-13 1:29 
Questionwhy you can't store a lambda expression in a 'dynamic variable, or 'var variable ? Pin
BillWoodruff27-Apr-13 19:33
memberBillWoodruff27-Apr-13 19: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 ? Pin
SledgeHammer0127-Apr-13 19:54
memberSledgeHammer0127-Apr-13 19:54 
GeneralRe: why you can't store a lambda expression in a 'dynamic variable, or 'var variable ? Pin
BillWoodruff27-Apr-13 20:41
memberBillWoodruff27-Apr-13 20:41 
GeneralRe: why you can't store a lambda expression in a 'dynamic variable, or 'var variable ? Pin
SledgeHammer0127-Apr-13 21:07
memberSledgeHammer0127-Apr-13 21:07 
AnswerRe: why you can't store a lambda expression in a 'dynamic variable, or 'var variable ? Pin
Richard Deeming29-Apr-13 3:19
professionalRichard Deeming29-Apr-13 3:19 
GeneralRe: why you can't store a lambda expression in a 'dynamic variable, or 'var variable ? Pin
BillWoodruff29-Apr-13 15:07
memberBillWoodruff29-Apr-13 15:07 
QuestionExtract Date part and pass only Date Pin
meeram3927-Apr-13 5:24
membermeeram3927-Apr-13 5:24 
AnswerRe: Extract Date part and pass only Date Pin
Dave Kreskowiak27-Apr-13 6:31
mvpDave Kreskowiak27-Apr-13 6:31 
AnswerRe: Extract Date part and pass only Date Pin
jschell27-Apr-13 12:15
memberjschell27-Apr-13 12:15 
GeneralJogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
Member 1000184526-Apr-13 14:15
memberMember 1000184526-Apr-13 14:15 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
dusty_dex26-Apr-13 14:36
memberdusty_dex26-Apr-13 14:36 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
Member 1000184526-Apr-13 16:30
memberMember 1000184526-Apr-13 16:30 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
Dave Kreskowiak26-Apr-13 18:16
mvpDave Kreskowiak26-Apr-13 18:16 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
Member 1000184527-Apr-13 0:54
memberMember 1000184527-Apr-13 0:54 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
Dave Kreskowiak27-Apr-13 5:09
mvpDave Kreskowiak27-Apr-13 5:09 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
Richard MacCutchan26-Apr-13 23:25
mvpRichard MacCutchan26-Apr-13 23:25 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
Member 1000184527-Apr-13 0:45
memberMember 1000184527-Apr-13 0:45 
QuestionData in XmlFiles or in Database Pin
Frygreen26-Apr-13 8:42
memberFrygreen26-Apr-13 8:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web01 | 2.8.170118.1 | Last Updated 22 Jan 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid