简体   繁体   中英

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.

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM