0

Is there some way to directly pass in a variable to a lambda method?

For example

string s = "test";
new Thread(() => MessageBox.Show(s)).Start();

Lambda expressions really make life so much easier for me, why does it seem so complicated to pass a variable in from the outside world?

Delimitry
  • 2,987
  • 4
  • 30
  • 39
CodeCamper
  • 6,609
  • 6
  • 44
  • 94
  • 5
    I don't see what the complicated part is. – Gabe Jun 06 '13 at 06:21
  • How can I accomplish this with 1 semicolon? It is complicated for just showing a `MessageBox`. It would be simple if the code I posted just worked. I am hoping there is a simple way to do this. – CodeCamper Jun 06 '13 at 06:29

5 Answers5

2

It isn't really complicated, especially if you're using the method-group syntax:

new Thread(MessageBox.Show).Start("test");
aquaraga
  • 4,138
  • 23
  • 29
  • I want to pass a variable in from the `outside world` though. I actually had a real world situation where I just wanted to have a bunch of `MessageBox`es pop up information being passed through from a rapidly fired event. I was able to accomplish it but it seems silly that I can't do this in one line of code for what it is. – CodeCamper Jun 06 '13 at 06:35
  • 2 Thumbs up for code that is giving me an error. I am sure there must be wisdom in the code somewhere but I am not yet at the level to determine it yet. – CodeCamper Jun 06 '13 at 06:42
1

Lambda expressions can take parameters as follows:

(string s) => MessageBox.Show(s);

This would change the definition of the action, so the method accepting the lambda needs to be able to accept the new delegate.

See the msdn document for further explanation.

boniestlawyer
  • 707
  • 5
  • 10
1

How can I accomplish this with 1 semicolon?

What's the problem?

new Thread(() => MessageBox.Show("test")).Start();

Upd.

the easiest way to pass something to lambda is a closures (this is how your question looks). That's why they are so convenient. By passing parameters like shown here, you're destroying all preferences of lambdas. This should be clear for you from your next question about passing two or more parameters.

Using closures, you can do this easily:

// somewhere in the code
var owner = // ...
var text = // ...
var caption = // ...

// here's the closures:
new Thread(() => MessageBox.Show(owner, text, caption)).Start();

Without them you need to get (or make) some type, which will be a container for your parameters, create instance of that type and initialize it members. Indeed, this is the work compiler does for you, when you're using closures. So, why do you prevent the compiler to do all this dirty job?

Community
  • 1
  • 1
Dennis
  • 37,026
  • 10
  • 82
  • 150
  • I want to pass a changing variable. I think user2455681 may have directly answered this. – CodeCamper Jun 06 '13 at 06:43
  • Can you pass more than one parameter without a Tuple which in my opinion takes a simple situation and makes it complicated only because I don't know how to pass more than one parameter easily. What is the proper way to do this when it is just a simple little thing you need to code only once? – CodeCamper Jun 06 '13 at 09:42
  • Yes! This indeed has helped! I really thought I was getting some sort of error when I tried exactly what you posted previously. Maybe I typed something wrong. – CodeCamper Jun 06 '13 at 10:29
1

Hi for parameterized thread write code like below

string a = "test";
new Thread((s) => { s = "newTest"; }).Start(a);
H H
  • 263,252
  • 30
  • 330
  • 514
suresh
  • 38
  • 2
  • This definitely is the most simple way to do it. Thank you! Now my next question is what is the most simple way to pass 2 variables in this same situation. – CodeCamper Jun 06 '13 at 06:44
  • @CodeCamper you can't, there are only 2 overloads of the Thread constructor (and of the start method - http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx). No arguments, one argument. For passing two "things", you can use a Tuple, or you can just use closures (your original code), which is the easier way – Lorenzo Dematté Jun 06 '13 at 07:20
  • What original code are you referring to? I am curious to see the simplest way to pass 2 or 3 variables in this new Thread statement. – CodeCamper Jun 06 '13 at 07:24
0

Yes, there are many ways to do it. Like

string s = "test";
new Thread((string parameter) => MessageBox.Show(s)).Start();

for more details see ......first 2nd third

Community
  • 1
  • 1
Atish Kumar Dipongkor
  • 10,220
  • 9
  • 49
  • 77