Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 2.0 generics: How to create an Action object with zero parameters

First of all, I'm using VS2005 and C# 2.0.

I'm trying to set a combobox' Text property from inside the SelectedIndexChanged event. From another thread here on StackOverflow this was proposed done the following way:

BeginInvoke(new Action(() => someCombobox.Text = "x" )); 

Now, first of all this returns a compiler error for me. I believe that is because the Action object behaves differently in the two language specifications. In C# 2.0, the Action object seems to need the <T> structure in all declarations. Maybe I'm wrong, but I'd like to have that clarified.

What does work is the following:

BeginInvoke(new Action<string>( delegate { someCombobox.Text = "x"; }), new object[] { "" });

However, it just seems very weird to me that I have to define the Action object with a type parameter (especially since I'm not intending to pass any parameters)! Somehow removing this parameter would also make the empty new object[] obsolete, which is what I want.

Can anyone help me simplify the above call?

Finally, is it guaranteed that BeginInvoke will finish after the SelectedIndexChanged and thus update the combobox' Text property with the correct text?

I'd really appreciate to learn the answers to these questions.

like image 652
Pedery Avatar asked Jul 19 '09 04:07

Pedery


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

You could define your own Action delegate.

delegate void Action()

I can't see the object on which you're calling BeginInvoke, but if it is a UI control created on the same thread as the combobox, the delegate you pass is guaranteed to be invoked some time after the SelectedIndexChanged event handler completes.

like image 167
Senthil Kumar Avatar answered Sep 30 '22 00:09

Senthil Kumar