Search code examples
c#.netvb.netanonymous-methods

Annoumous Function In VB.net


I have the following line of code in c#.

Check.ThatIsNotAnEmptyString(line1, () => { throw new InvalidAddressException("An address must have a street"); });

I am having difficulty converting it to vb.net.

I used the conversion tool 'www.developerfusion.com' but it produces the following piece of code.

Check.ThatIsNotAnEmptyString(line1, Function() Throw New InvalidAddressException("An address must have a street") End Function)

It complains on the word 'Throw' saying expression expected.

Can anyone tell me if converting this to vb.net is possible.


Solution

  • You have to use Sub, since the function has no return value (like void in C#).

    Also, since the function is on one line, you don't need to have a End Sub/Function, which is only needed on multiline functions (and was added in .Net 4.0).


    So your code should read:

    Check.ThatIsNotAnEmptyString(line1, Sub() Throw New InvalidAddressException("An address must have a street"))