Wrap Sub as a function to use in lambda

I have a problem with VB9 and Moq.

I need to call a confirmation on Sub. For instance:

logger.Verify(Function(x) x.Log, Times.AtLeastOnce)

And my registrar looks like this:

Public Interface ILogger
    Sub Log()
End Interface

But with VB, this is not possible, because the Log method is Sub and therefore does not give a value.

I do not want the method to be a function.

What is the cleanest way around this limitation and is there a way to wrap Sub as a function, as shown below?

logger.Verify(Function(x) ToFunc(AddressOf x.Log), Times.AtLeastOnce)

I tried this, but I get:

Lambda parameter out of scope

+3
source share
2 answers

VB10 allows the use of Lambada Subs.

Have you tried a simple shell, for example:

Public Function Wrapper(source as Action) as Boolean  
    source.Invoke()   
    Return True 
End Function
+1
source

In 2010, if its a Sub and not a function, just replace Function with Sub.

logger.Verify( Sub (x) x.Log, Times.AtLeastOnce)

-1

Source: https://habr.com/ru/post/1738457/


All Articles