Results 1 to 4 of 4

Thread: [RESOLVED] Trouble with Func(TResult).

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Resolved [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:
    1. Property CheckStateCondition As Func(Of Boolean)

    Then, I use it like so:
    vb.net Code:
    1. If CheckStateCondition Then
    2. ' Do something
    3. End If

    There is an error on CheckStateCondition that says,
    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.
    Prefix has no suffix, but suffix has a prefix.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    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.
    Prefix has no suffix, but suffix has a prefix.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width