Not sure how this c# event handling with lambdas work, can someone explain it to me please?











up vote
-3
down vote

favorite












I was looking for a way to convert vb event handling to c# since i am implementing something and, after reading this post ( How do I Convert from Anonymous Event Handlers in C# to VB.Net), i tried to copy the lambda structure, and somehow it worked, but i have NO IDEA. How it does, can someonoe please explain it to me?
Or at least guide me to a link for a tutorial or something that helps me understand whats going on in here...
I mean, i can get out with this since it, somehow, worked, but i want to understand whats going on rather than just, "get the job done".



Class.Event += Class.Delegate((sometext)) => {eventhandlemethod(sometext);});
eventhandlemethod(string s)
{
MessageBox.Show(s);
}


Thanks in advance!










share|improve this question


















  • 1




    Not sure what you are unclear about?
    – Dave
    Nov 7 at 15:18










  • Lambdas as simply a way to make Anonymous Functions. Nothing more, nothing less. However this does seem like a odd Event. Normally the arguments for events are "object sender" and "SpecificEventArgumentSubclass arg". This one seemns to take a string instead?
    – Christopher
    Nov 7 at 15:18












  • Yeah, this event was made by the guy before me, and im having some problems getting how he had things done... Part of my question is that this code didnt work : 'code'Class.Event + Class.Delegate(eventhandlemethod()); eventhandlemethod(object sender, EventArgs e) { MessageBox.Show("something here that gives me an alert"); }'code'
    – Alejandro
    Nov 7 at 15:21












  • Your problem does not seem to stem from the anonymous function, but from a custom event someone else created.
    – Andreas
    Nov 7 at 15:44















up vote
-3
down vote

favorite












I was looking for a way to convert vb event handling to c# since i am implementing something and, after reading this post ( How do I Convert from Anonymous Event Handlers in C# to VB.Net), i tried to copy the lambda structure, and somehow it worked, but i have NO IDEA. How it does, can someonoe please explain it to me?
Or at least guide me to a link for a tutorial or something that helps me understand whats going on in here...
I mean, i can get out with this since it, somehow, worked, but i want to understand whats going on rather than just, "get the job done".



Class.Event += Class.Delegate((sometext)) => {eventhandlemethod(sometext);});
eventhandlemethod(string s)
{
MessageBox.Show(s);
}


Thanks in advance!










share|improve this question


















  • 1




    Not sure what you are unclear about?
    – Dave
    Nov 7 at 15:18










  • Lambdas as simply a way to make Anonymous Functions. Nothing more, nothing less. However this does seem like a odd Event. Normally the arguments for events are "object sender" and "SpecificEventArgumentSubclass arg". This one seemns to take a string instead?
    – Christopher
    Nov 7 at 15:18












  • Yeah, this event was made by the guy before me, and im having some problems getting how he had things done... Part of my question is that this code didnt work : 'code'Class.Event + Class.Delegate(eventhandlemethod()); eventhandlemethod(object sender, EventArgs e) { MessageBox.Show("something here that gives me an alert"); }'code'
    – Alejandro
    Nov 7 at 15:21












  • Your problem does not seem to stem from the anonymous function, but from a custom event someone else created.
    – Andreas
    Nov 7 at 15:44













up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











I was looking for a way to convert vb event handling to c# since i am implementing something and, after reading this post ( How do I Convert from Anonymous Event Handlers in C# to VB.Net), i tried to copy the lambda structure, and somehow it worked, but i have NO IDEA. How it does, can someonoe please explain it to me?
Or at least guide me to a link for a tutorial or something that helps me understand whats going on in here...
I mean, i can get out with this since it, somehow, worked, but i want to understand whats going on rather than just, "get the job done".



Class.Event += Class.Delegate((sometext)) => {eventhandlemethod(sometext);});
eventhandlemethod(string s)
{
MessageBox.Show(s);
}


Thanks in advance!










share|improve this question













I was looking for a way to convert vb event handling to c# since i am implementing something and, after reading this post ( How do I Convert from Anonymous Event Handlers in C# to VB.Net), i tried to copy the lambda structure, and somehow it worked, but i have NO IDEA. How it does, can someonoe please explain it to me?
Or at least guide me to a link for a tutorial or something that helps me understand whats going on in here...
I mean, i can get out with this since it, somehow, worked, but i want to understand whats going on rather than just, "get the job done".



Class.Event += Class.Delegate((sometext)) => {eventhandlemethod(sometext);});
eventhandlemethod(string s)
{
MessageBox.Show(s);
}


Thanks in advance!







c#






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 15:15









Alejandro

144




144








  • 1




    Not sure what you are unclear about?
    – Dave
    Nov 7 at 15:18










  • Lambdas as simply a way to make Anonymous Functions. Nothing more, nothing less. However this does seem like a odd Event. Normally the arguments for events are "object sender" and "SpecificEventArgumentSubclass arg". This one seemns to take a string instead?
    – Christopher
    Nov 7 at 15:18












  • Yeah, this event was made by the guy before me, and im having some problems getting how he had things done... Part of my question is that this code didnt work : 'code'Class.Event + Class.Delegate(eventhandlemethod()); eventhandlemethod(object sender, EventArgs e) { MessageBox.Show("something here that gives me an alert"); }'code'
    – Alejandro
    Nov 7 at 15:21












  • Your problem does not seem to stem from the anonymous function, but from a custom event someone else created.
    – Andreas
    Nov 7 at 15:44














  • 1




    Not sure what you are unclear about?
    – Dave
    Nov 7 at 15:18










  • Lambdas as simply a way to make Anonymous Functions. Nothing more, nothing less. However this does seem like a odd Event. Normally the arguments for events are "object sender" and "SpecificEventArgumentSubclass arg". This one seemns to take a string instead?
    – Christopher
    Nov 7 at 15:18












  • Yeah, this event was made by the guy before me, and im having some problems getting how he had things done... Part of my question is that this code didnt work : 'code'Class.Event + Class.Delegate(eventhandlemethod()); eventhandlemethod(object sender, EventArgs e) { MessageBox.Show("something here that gives me an alert"); }'code'
    – Alejandro
    Nov 7 at 15:21












  • Your problem does not seem to stem from the anonymous function, but from a custom event someone else created.
    – Andreas
    Nov 7 at 15:44








1




1




Not sure what you are unclear about?
– Dave
Nov 7 at 15:18




Not sure what you are unclear about?
– Dave
Nov 7 at 15:18












Lambdas as simply a way to make Anonymous Functions. Nothing more, nothing less. However this does seem like a odd Event. Normally the arguments for events are "object sender" and "SpecificEventArgumentSubclass arg". This one seemns to take a string instead?
– Christopher
Nov 7 at 15:18






Lambdas as simply a way to make Anonymous Functions. Nothing more, nothing less. However this does seem like a odd Event. Normally the arguments for events are "object sender" and "SpecificEventArgumentSubclass arg". This one seemns to take a string instead?
– Christopher
Nov 7 at 15:18














Yeah, this event was made by the guy before me, and im having some problems getting how he had things done... Part of my question is that this code didnt work : 'code'Class.Event + Class.Delegate(eventhandlemethod()); eventhandlemethod(object sender, EventArgs e) { MessageBox.Show("something here that gives me an alert"); }'code'
– Alejandro
Nov 7 at 15:21






Yeah, this event was made by the guy before me, and im having some problems getting how he had things done... Part of my question is that this code didnt work : 'code'Class.Event + Class.Delegate(eventhandlemethod()); eventhandlemethod(object sender, EventArgs e) { MessageBox.Show("something here that gives me an alert"); }'code'
– Alejandro
Nov 7 at 15:21














Your problem does not seem to stem from the anonymous function, but from a custom event someone else created.
– Andreas
Nov 7 at 15:44




Your problem does not seem to stem from the anonymous function, but from a custom event someone else created.
– Andreas
Nov 7 at 15:44












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










I am not familiar with VB.NET but an anonymous Function or Action as it would be in the case of an Eventhandler is basically just a short form of writing.



But what is the line



Class.Event += Class.Delegate((sometext) => {eventhandlemethod(sometext);});


telling you?




  1. You have an Event, and you add the handler (obviously).


  2. You have added an anonymous method as Eventhandler and created a Delegate from it.



    Class.Delegate(<anoymous method, matching the siganture of the handler>);



  3. Your anonymous method looks like this



    (sometext) => {eventhandlemethod(sometext);} 



This anonymous method (short lamba) consists of two parts



siganture => body


In Your case the signature gets eh "sometext". Derived from the rest of your code, this seems to be a string. And since eventhandlers always have void as return value, your method, would you have written a normal method, would look like this:



private void (string sometext)
{
}


which is basically, what your eventhandlemethod looks like. You could write this lambda like this as well.



(string sometext) => {eventhandlemethod(sometext);}


This shows you the expected intput type. But you can ommit this usually.



So just as goodie: you could have written





  1. with a "normal" method, since you already have it



    Class.Event += Class.Delegate(eventhandlemethod);



  2. with a lambda



    Class.Event += Class.Delegate(sometext => MessageBox.Show(sometext));



if you have multiple input variables, put them in brackets



(string s, int i, bool b) => Console.WriteLine($"{s}:{i}:{b}");


and of course, you can ommit the types as well leaving you:



(s, i, b) => Console.WriteLine($"{s}:{i}:{b}");


I hope i could clarify it a bit for you.






share|improve this answer























  • Yes, its a nice explanation, thanks, and i will be sure to use this answer as future reference!
    – Alejandro
    Nov 7 at 15:46










  • Thank you for the feedback. Always glad to help.
    – Andreas
    Nov 7 at 15:47











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53192340%2fnot-sure-how-this-c-sharp-event-handling-with-lambdas-work-can-someone-explain%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










I am not familiar with VB.NET but an anonymous Function or Action as it would be in the case of an Eventhandler is basically just a short form of writing.



But what is the line



Class.Event += Class.Delegate((sometext) => {eventhandlemethod(sometext);});


telling you?




  1. You have an Event, and you add the handler (obviously).


  2. You have added an anonymous method as Eventhandler and created a Delegate from it.



    Class.Delegate(<anoymous method, matching the siganture of the handler>);



  3. Your anonymous method looks like this



    (sometext) => {eventhandlemethod(sometext);} 



This anonymous method (short lamba) consists of two parts



siganture => body


In Your case the signature gets eh "sometext". Derived from the rest of your code, this seems to be a string. And since eventhandlers always have void as return value, your method, would you have written a normal method, would look like this:



private void (string sometext)
{
}


which is basically, what your eventhandlemethod looks like. You could write this lambda like this as well.



(string sometext) => {eventhandlemethod(sometext);}


This shows you the expected intput type. But you can ommit this usually.



So just as goodie: you could have written





  1. with a "normal" method, since you already have it



    Class.Event += Class.Delegate(eventhandlemethod);



  2. with a lambda



    Class.Event += Class.Delegate(sometext => MessageBox.Show(sometext));



if you have multiple input variables, put them in brackets



(string s, int i, bool b) => Console.WriteLine($"{s}:{i}:{b}");


and of course, you can ommit the types as well leaving you:



(s, i, b) => Console.WriteLine($"{s}:{i}:{b}");


I hope i could clarify it a bit for you.






share|improve this answer























  • Yes, its a nice explanation, thanks, and i will be sure to use this answer as future reference!
    – Alejandro
    Nov 7 at 15:46










  • Thank you for the feedback. Always glad to help.
    – Andreas
    Nov 7 at 15:47















up vote
1
down vote



accepted










I am not familiar with VB.NET but an anonymous Function or Action as it would be in the case of an Eventhandler is basically just a short form of writing.



But what is the line



Class.Event += Class.Delegate((sometext) => {eventhandlemethod(sometext);});


telling you?




  1. You have an Event, and you add the handler (obviously).


  2. You have added an anonymous method as Eventhandler and created a Delegate from it.



    Class.Delegate(<anoymous method, matching the siganture of the handler>);



  3. Your anonymous method looks like this



    (sometext) => {eventhandlemethod(sometext);} 



This anonymous method (short lamba) consists of two parts



siganture => body


In Your case the signature gets eh "sometext". Derived from the rest of your code, this seems to be a string. And since eventhandlers always have void as return value, your method, would you have written a normal method, would look like this:



private void (string sometext)
{
}


which is basically, what your eventhandlemethod looks like. You could write this lambda like this as well.



(string sometext) => {eventhandlemethod(sometext);}


This shows you the expected intput type. But you can ommit this usually.



So just as goodie: you could have written





  1. with a "normal" method, since you already have it



    Class.Event += Class.Delegate(eventhandlemethod);



  2. with a lambda



    Class.Event += Class.Delegate(sometext => MessageBox.Show(sometext));



if you have multiple input variables, put them in brackets



(string s, int i, bool b) => Console.WriteLine($"{s}:{i}:{b}");


and of course, you can ommit the types as well leaving you:



(s, i, b) => Console.WriteLine($"{s}:{i}:{b}");


I hope i could clarify it a bit for you.






share|improve this answer























  • Yes, its a nice explanation, thanks, and i will be sure to use this answer as future reference!
    – Alejandro
    Nov 7 at 15:46










  • Thank you for the feedback. Always glad to help.
    – Andreas
    Nov 7 at 15:47













up vote
1
down vote



accepted







up vote
1
down vote



accepted






I am not familiar with VB.NET but an anonymous Function or Action as it would be in the case of an Eventhandler is basically just a short form of writing.



But what is the line



Class.Event += Class.Delegate((sometext) => {eventhandlemethod(sometext);});


telling you?




  1. You have an Event, and you add the handler (obviously).


  2. You have added an anonymous method as Eventhandler and created a Delegate from it.



    Class.Delegate(<anoymous method, matching the siganture of the handler>);



  3. Your anonymous method looks like this



    (sometext) => {eventhandlemethod(sometext);} 



This anonymous method (short lamba) consists of two parts



siganture => body


In Your case the signature gets eh "sometext". Derived from the rest of your code, this seems to be a string. And since eventhandlers always have void as return value, your method, would you have written a normal method, would look like this:



private void (string sometext)
{
}


which is basically, what your eventhandlemethod looks like. You could write this lambda like this as well.



(string sometext) => {eventhandlemethod(sometext);}


This shows you the expected intput type. But you can ommit this usually.



So just as goodie: you could have written





  1. with a "normal" method, since you already have it



    Class.Event += Class.Delegate(eventhandlemethod);



  2. with a lambda



    Class.Event += Class.Delegate(sometext => MessageBox.Show(sometext));



if you have multiple input variables, put them in brackets



(string s, int i, bool b) => Console.WriteLine($"{s}:{i}:{b}");


and of course, you can ommit the types as well leaving you:



(s, i, b) => Console.WriteLine($"{s}:{i}:{b}");


I hope i could clarify it a bit for you.






share|improve this answer














I am not familiar with VB.NET but an anonymous Function or Action as it would be in the case of an Eventhandler is basically just a short form of writing.



But what is the line



Class.Event += Class.Delegate((sometext) => {eventhandlemethod(sometext);});


telling you?




  1. You have an Event, and you add the handler (obviously).


  2. You have added an anonymous method as Eventhandler and created a Delegate from it.



    Class.Delegate(<anoymous method, matching the siganture of the handler>);



  3. Your anonymous method looks like this



    (sometext) => {eventhandlemethod(sometext);} 



This anonymous method (short lamba) consists of two parts



siganture => body


In Your case the signature gets eh "sometext". Derived from the rest of your code, this seems to be a string. And since eventhandlers always have void as return value, your method, would you have written a normal method, would look like this:



private void (string sometext)
{
}


which is basically, what your eventhandlemethod looks like. You could write this lambda like this as well.



(string sometext) => {eventhandlemethod(sometext);}


This shows you the expected intput type. But you can ommit this usually.



So just as goodie: you could have written





  1. with a "normal" method, since you already have it



    Class.Event += Class.Delegate(eventhandlemethod);



  2. with a lambda



    Class.Event += Class.Delegate(sometext => MessageBox.Show(sometext));



if you have multiple input variables, put them in brackets



(string s, int i, bool b) => Console.WriteLine($"{s}:{i}:{b}");


and of course, you can ommit the types as well leaving you:



(s, i, b) => Console.WriteLine($"{s}:{i}:{b}");


I hope i could clarify it a bit for you.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 7 at 15:49

























answered Nov 7 at 15:38









Andreas

502211




502211












  • Yes, its a nice explanation, thanks, and i will be sure to use this answer as future reference!
    – Alejandro
    Nov 7 at 15:46










  • Thank you for the feedback. Always glad to help.
    – Andreas
    Nov 7 at 15:47


















  • Yes, its a nice explanation, thanks, and i will be sure to use this answer as future reference!
    – Alejandro
    Nov 7 at 15:46










  • Thank you for the feedback. Always glad to help.
    – Andreas
    Nov 7 at 15:47
















Yes, its a nice explanation, thanks, and i will be sure to use this answer as future reference!
– Alejandro
Nov 7 at 15:46




Yes, its a nice explanation, thanks, and i will be sure to use this answer as future reference!
– Alejandro
Nov 7 at 15:46












Thank you for the feedback. Always glad to help.
– Andreas
Nov 7 at 15:47




Thank you for the feedback. Always glad to help.
– Andreas
Nov 7 at 15:47


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53192340%2fnot-sure-how-this-c-sharp-event-handling-with-lambdas-work-can-someone-explain%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini