Results 1 to 5 of 5

Thread: Call Function By Name

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521

    Call Function By Name

    Do you have any ideas of how to call a function, such as Sin(x), by providing the string "sin" and the double x?

    Something like:

    lRet = CallFunctionByName("sin", x)

    I'm working on a program that parses functions and evaluates them, and this would make life quite a bit easier if such a thing existed. I have found CallByName, which I could possibly manipulate into doing the above, but it would be a pain (and bad code too). Any help would be appreciated.

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    how about

    iRet = SomeFunction("sin", x)

    ------------------------------------
    Public Function SomeFunction(strCommand as string, x as double) as double
    Select Ucase(strCommand)
    Case "SIN"
    SomeFunction = sin(x)
    Case "COS"
    SomeFunction = cos(x)
    End Select

    i hope this is what your looking for
    or do you want to write your OWN sin function?

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I'd use kovan's approach too !

    Code:
    Private Sub Form_Load()
        MsgBox SomeFunction("ADD", 4, 5, 6)
        MsgBox SomeFunction("SUB", 5, 77)
    End Sub
    
    Private Function SomeFunction(strCommand As String, ParamArray Args())
        Select Case UCase(strCommand)
            Case "SIN":
                SomeFunction = Sin(Args(0))
            Case "COS":
                SomeFunction = Cos(Args(0))
            Case "ADD":
                Dim i As Long
                Dim retVal
                For i = 0 To UBound(Args)
                    retVal = retVal + Args(i)
                Next i
                SomeFunction = retVal
            Case "SUB":
                SomeFunction = Args(0) - Args(1)
        End Select
    End Function
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    I already have this:
    VB Code:
    1. Public Function ExFunction(Func As String, Num As Integer) As Double
    2.     Select Case Func
    3.     Case "log"
    4.         ExFunction = Log(Num) / Log(10)
    5.     Case "sqrt", "sqr"
    6.         ExFunction = Sqr(Num)
    7.     Case "abs"
    8.         ExFunction = Abs(Num)
    9.     Case "int"
    10.         ExFunction = Int(Num)
    11.     Case "sin"
    12.         ExFunction = Sin(Num)
    13.     Case "cos"
    14.         ExFunction = Cos(Num)
    15.     Case "tan"
    16.         ExFunction = Tan(Num)
    17.     Case "asc"
    18.         ExFunction = Asc(Num)
    19.     Case "atn"
    20.         ExFunction = Atn(Num)
    21.     Case "ln"
    22.         ExFunction = Log(Num)
    23.     End Select
    24. End Function
    However, this makes it hard to make custom functions at runtime. I was hoping to have this ability. My initial idea was to put all the functions in a custom class, and the ability to actually call a function by name would have been nice...

    I only need the functions taken care of; the operators are taken care of with something else.

  5. #5
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Sedgefield
    Posts
    337

    Use the script control

    Use the microsoft script control.

    (msscript.ocx)

    It will let you write and interpret functions at run-time.

    Dan

    Outside of a dog, a book is a man's best friend.
    Inside of a dog, it's too dark to read.

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