-
Tùy biến gọi hàm theo giá trị
Noname > 24-04-11, 01:48 PM
Modules: Use variables to call functions
Author(s)
Dev Ashish
Q) How can I call a Function by using a variable instead of Function Name?
(A) Use the Eval Function. If you pass to the Eval function a string that contains the name of a function, the Eval function returns the return value of the function. For example, Eval("Chr$(65)") returns "A".
So for example, in the following code, if you call fEval with "A" as parameter, you should get the result "Test That", else "Test This".
Mã:'*******Code Start*******
'This code was originally written by Dev Ashish
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Function fEval(status As String)
Dim strFunctionName As String
Dim x
If status = "A" Then
strFunctionName = "TestThat()"
Else
strFunctionName = "TestThis()"
End If
fEval = Eval(strFunctionName)
End Function
Function TestThis()
Debug.Print "Test This"
End Function
Function testThat()
Debug.Print "Test That"
End Function
'*******Code End********** -
RE: Tùy biến gọi hàm theo giá trị
Noname > 25-04-11, 02:36 PM
Ta có thể ứng dụng hàm này trong phần phân quyền, nếu gặp user này thì chạy hàm này,user kia thì chạy hàm kia. Hoặc các chương trình tính lương có phân biệt theo cấp bậc, phụ cấp...
Nói chung chỉ là lấy ý tưởng rồi chế biến sao cho phù hợp!