Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET: Lambda expression, use assignment operator instead of equality

Tags:

vb.net

I have a method that takes an System.Action, this is what I'm trying to feed it:

Function() Me._existingImports = Me.GetImportedAds()

The thing is that it complains about the = sign since it thinks I'm trying to do a comparison, which I'm not. I want to assign the Me._existingImports the value of Me.GetImportedAds(), but VB.NET complains about DataTable not having a = operator.

How can I force it to use the assignment operator instead of the equality operator?

In C# this works perfectly fine:

() => this.existingImports = this.GetImportedAds()

For now the solution will be to use a standalone method, but that's way more code than needed.

like image 444
Kim Johansson Avatar asked Aug 09 '26 01:08

Kim Johansson


1 Answers

When using Function(), you really define an anonymous function which means you map values to values.

Therefore Function() strictly needs an expression (like x or 42 ...) as the body, which an assignment is not! (Assignments don't evaluate to values like in C-style languages in VB)

Thus what you need is not a Function() but a Sub(), which contains statements (actions) rather than values.

Sub() Me._existingImports = Me.GetImportedAds()

C# doesn't distinguish here, the (much nicer) ... => ... syntax covers it all.

like image 50
Dario Avatar answered Aug 11 '26 14:08

Dario