Click here to Skip to main content
15,887,351 members
Home / Discussions / C#
   

C#

 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv5-May-13 0:24
N8tiv5-May-13 0:24 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff5-May-13 0:28
mveOriginalGriff5-May-13 0:28 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
Kevin Bewley29-Apr-13 0:30
Kevin Bewley29-Apr-13 0:30 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot29-Apr-13 0:38
harold aptroot29-Apr-13 0:38 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
Kevin Bewley29-Apr-13 0:40
Kevin Bewley29-Apr-13 0:40 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot29-Apr-13 0:43
harold aptroot29-Apr-13 0:43 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv5-May-13 0:29
N8tiv5-May-13 0:29 
Questionwhy you can't store a lambda expression in a 'dynamic variable, or 'var variable ? Pin
BillWoodruff27-Apr-13 18:33
professionalBillWoodruff27-Apr-13 18:33 
C#
// 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:
XML
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:
C#
private void MethodTakesAFunc(Func<dynamic, dynamic, string> theFuncParam){}
While you can execute a passed in Func like this:
C#
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:
C#
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 18:54
SledgeHammer0127-Apr-13 18:54 
GeneralRe: why you can't store a lambda expression in a 'dynamic variable, or 'var variable ? Pin
BillWoodruff27-Apr-13 19:41
professionalBillWoodruff27-Apr-13 19:41 
GeneralRe: why you can't store a lambda expression in a 'dynamic variable, or 'var variable ? Pin
SledgeHammer0127-Apr-13 20:07
SledgeHammer0127-Apr-13 20:07 
AnswerRe: why you can't store a lambda expression in a 'dynamic variable, or 'var variable ? Pin
Richard Deeming29-Apr-13 2:19
mveRichard Deeming29-Apr-13 2:19 
GeneralRe: why you can't store a lambda expression in a 'dynamic variable, or 'var variable ? Pin
BillWoodruff29-Apr-13 14:07
professionalBillWoodruff29-Apr-13 14:07 
QuestionExtract Date part and pass only Date Pin
meeram3927-Apr-13 4:24
professionalmeeram3927-Apr-13 4:24 
AnswerRe: Extract Date part and pass only Date Pin
Dave Kreskowiak27-Apr-13 5:31
mveDave Kreskowiak27-Apr-13 5:31 
AnswerRe: Extract Date part and pass only Date Pin
jschell27-Apr-13 11:15
jschell27-Apr-13 11:15 
GeneralJogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
Member 1000184526-Apr-13 13:15
Member 1000184526-Apr-13 13:15 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
dusty_dex26-Apr-13 13:36
dusty_dex26-Apr-13 13:36 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
Member 1000184526-Apr-13 15:30
Member 1000184526-Apr-13 15:30 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
Dave Kreskowiak26-Apr-13 17:16
mveDave Kreskowiak26-Apr-13 17:16 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
Member 1000184526-Apr-13 23:54
Member 1000184526-Apr-13 23:54 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
Dave Kreskowiak27-Apr-13 4:09
mveDave Kreskowiak27-Apr-13 4:09 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
Richard MacCutchan26-Apr-13 22:25
mveRichard MacCutchan26-Apr-13 22:25 
GeneralRe: Jogar uma imagem em um picturibox, apartir de um clique em uma linha do datagridview Pin
Member 1000184526-Apr-13 23:45
Member 1000184526-Apr-13 23:45 
QuestionData in XmlFiles or in Database Pin
Frygreen26-Apr-13 7:42
Frygreen26-Apr-13 7: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.