[RESOLVED] Trouble with Func(TResult).
Ok, bear with me on this one. I am sure I am just messing up something very simple.
I have this declared:
vb.net Code:
Property CheckStateCondition As Func(Of Boolean)
Then, I use it like so:
vb.net Code:
If CheckStateCondition Then
' Do something
End If
There is an error on CheckStateCondition that says,
Quote:
Value of type 'System.Func(Of Boolean)' cannot be converted to 'Boolean'.
I have tried Google, where every link says I am declaring it correctly. I tried searching for the error message itself and got nothing useful. I even tried using Predicate(T) and got the same result. All I am trying to do is store a Boolean value from a lambda.
Thanks.
Re: Trouble with Func(TResult).
You have three options:
1. Use a field instead of a property and place parentheses on the call:
Code:
Private CheckStateCondition As Func(Of Boolean)
Code:
If CheckStateCondition() Then
2. Assign the property value to a local variable first and then use that in the If statement, which still requires parentheses on the call:
Code:
Dim stateChecker = CheckStateCondition
If stateChecker() Then
3. Call Invoke on the property value:
Code:
If CheckStateCondition.Invoke() Then
Re: Trouble with Func(TResult).
Actually, there's a fourth option too. You can also stick with the property and use double parentheses on the call:
Code:
If CheckStateCondition()() Then
The first pair of parentheses evaluates the property and the second invokes the delegate returned by the property. If you mouse over the two pairs of parentheses in the IDE you'll see that the first represents the property and the second implicitly represents the Invoke method.
Re: Trouble with Func(TResult).
I really wanted to keep it as a property, and glad I am able to. I should have know about Invoke. /facepalm
Thanks. :D