Results 1 to 5 of 5

Thread: [RESOLVED] Function as Parameter

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2015
    Posts
    174

    Resolved [RESOLVED] Function as Parameter

    Hello, I'm trying to use this method that I found online:
    Code:
    Function Bisect(a as Double, b As Double, f As Function(Of Double, Double)) As Double
        Dim c As Double = (a + b) / 2 
        Dim d As Double = f(c)
    
        While Math.Abs(d) >= 0.0001
            If Math.Sign(d) = Math.Sign(f(a)) Then 
               a = c
            Else
               b = c
            End If    
    
            c = (a + b) / 2
            d = f(c)
        End While
        Return c
    End Function
    But VS say Keyword does not name a type.
    I thing the signature is wrong.
    Any advice?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: Function as Parameter

    You found it online where?

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

    Re: Function as Parameter

    Either the person who posted it made a mistake typing it or you did. That should almost certainly be this:
    Code:
    Function Bisect(a as Double, b As Double, f As Func(Of Double, Double)) As Double
    An Action is a delegate for a Sub, i.e. a method that doesn't return a value, and a Func is a delegate for a Function, i.e. a method that does return a value. Both can have zero to 16 parameters. I suggest that you read the documentation for each.
    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
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: Function as Parameter

    Quote Originally Posted by hannibal smith View Post
    Hello, I'm trying to use this method that I found online:
    Code:
    Function Bisect(a as Double, b As Double, f As Function(Of Double, Double)) As Double
        Dim c As Double = (a + b) / 2 
        Dim d As Double = f(c)
    
        While Math.Abs(d) >= 0.0001
            If Math.Sign(d) = Math.Sign(f(a)) Then 
               a = c
            Else
               b = c
            End If    
    
            c = (a + b) / 2
            d = f(c)
        End While
        Return c
    End Function
    But VS say Keyword does not name a type.
    I thing the signature is wrong.
    Any advice?
    I suspect the declaration should be
    Code:
       Function Bisect(a As Double, b As Double, f As Func(Of Double, Double)) As Double
    Edit: jmcilhinney beat me to it, my own fault for having too many tabs open...

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2015
    Posts
    174

    Re: Function as Parameter

    Quote Originally Posted by jmcilhinney View Post
    Either the person who posted it made a mistake typing it or you did. That should almost certainly be this:
    Code:
    Function Bisect(a as Double, b As Double, f As Func(Of Double, Double)) As Double
    An Action is a delegate for a Sub, i.e. a method that doesn't return a value, and a Func is a delegate for a Function, i.e. a method that does return a value. Both can have zero to 16 parameters. I suggest that you read the documentation for each.
    Thanks. Now it work.

Tags for this Thread

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