|
-
Jun 19th, 2001, 10:24 PM
#1
Thread Starter
Fanatic Member
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.
-
Jun 19th, 2001, 10:57 PM
#2
Frenzied Member
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?
-
Jun 20th, 2001, 03:19 AM
#3
Retired VBF Adm1nistrator
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]
-
Jun 20th, 2001, 06:43 AM
#4
Thread Starter
Fanatic Member
I already have this:
VB Code:
Public Function ExFunction(Func As String, Num As Integer) As Double
Select Case Func
Case "log"
ExFunction = Log(Num) / Log(10)
Case "sqrt", "sqr"
ExFunction = Sqr(Num)
Case "abs"
ExFunction = Abs(Num)
Case "int"
ExFunction = Int(Num)
Case "sin"
ExFunction = Sin(Num)
Case "cos"
ExFunction = Cos(Num)
Case "tan"
ExFunction = Tan(Num)
Case "asc"
ExFunction = Asc(Num)
Case "atn"
ExFunction = Atn(Num)
Case "ln"
ExFunction = Log(Num)
End Select
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.
-
Jun 20th, 2001, 07:14 AM
#5
Hyperactive Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|