Friday, May 17, 2013

仕事の日々#3: VB.NET anonymous methods

What we lazy coders do in C# when dealing with a non-STA WinForm on another thread is usually:

Invoke((MethodInvoker) delegate
{
    txtSomeLabel.Text = "Foobar!";
});

Today I had to deal with a piece of code written in VB.NET, and here’s how it’s done:

Invoke(
   Sub()
      txtSomeLabel.Text = "Foobar!"
   End Sub
)

No casting is required as BASIC is not a strongly typed language.

It also works with anonymous methods with parameters, you just have to use Function in place of Sub.

No comments:

Post a Comment