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!
c#
add a comment |
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!
c#
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
add a comment |
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!
c#
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#
c#
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
add a comment |
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
add a comment |
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?
- You have an Event, and you add the handler (obviously).
You have added an anonymous method as Eventhandler and created a Delegate from it.
Class.Delegate(<anoymous method, matching the siganture of the handler>);
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
with a "normal" method, since you already have it
Class.Event += Class.Delegate(eventhandlemethod);
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.
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
add a comment |
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?
- You have an Event, and you add the handler (obviously).
You have added an anonymous method as Eventhandler and created a Delegate from it.
Class.Delegate(<anoymous method, matching the siganture of the handler>);
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
with a "normal" method, since you already have it
Class.Event += Class.Delegate(eventhandlemethod);
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.
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
add a comment |
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?
- You have an Event, and you add the handler (obviously).
You have added an anonymous method as Eventhandler and created a Delegate from it.
Class.Delegate(<anoymous method, matching the siganture of the handler>);
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
with a "normal" method, since you already have it
Class.Event += Class.Delegate(eventhandlemethod);
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.
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
add a comment |
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?
- You have an Event, and you add the handler (obviously).
You have added an anonymous method as Eventhandler and created a Delegate from it.
Class.Delegate(<anoymous method, matching the siganture of the handler>);
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
with a "normal" method, since you already have it
Class.Event += Class.Delegate(eventhandlemethod);
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.
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?
- You have an Event, and you add the handler (obviously).
You have added an anonymous method as Eventhandler and created a Delegate from it.
Class.Delegate(<anoymous method, matching the siganture of the handler>);
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
with a "normal" method, since you already have it
Class.Event += Class.Delegate(eventhandlemethod);
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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