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?

2
  • 5
    I don't see what the complicated part is. Commented Jun 6, 2013 at 6: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. Commented Jun 6, 2013 at 6:29

5 Answers 5

2

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

new Thread(MessageBox.Show).Start("test");
Sign up to request clarification or add additional context in comments.

2 Comments

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 MessageBoxes 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.
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.
1

Hi for parameterized thread write code like below

string a = "test";
new Thread((s) => { s = "newTest"; }).Start(a);

3 Comments

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 you can't, there are only 2 overloads of the Thread constructor (and of the start method - 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
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.
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.

Comments

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?

3 Comments

I want to pass a changing variable. I think user2455681 may have directly answered this.
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?
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.
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

1 Comment

This code gives me an error it says I cannot declare s twice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.