Click to See Complete Forum and Search --> : variable for methods


zhining
October 17th, 2001, 04:02 PM
hello all,

Is it possible to use a variable for methods?

In the following example, add_two() and substract_two() are two methods, but I do not know which one will be called during coding.

======================
Set m_obj = AdvCreateObject("MyObject.myObject")

var_method = "add_two"
result = m_obj.var_method(11, 22) '??????

var_method = "substract_two"
result = m_obj.var_method(11, 22) '??????

======================


thanks!
_Zhining Zhang

srinika
October 17th, 2001, 10:23 PM
As far as I know this is IMPOSSIBLE
But u can achieve the same by having an argument in the var_method to get the method name and inside the code deal with that accordingly

' In the class:
public Function var_method(s as string , n1 as Integer , n2 as Integer ) as Integer

If s = "add_two" then
var_method = n1 + n2
ElseIf s = "substract_two" then
var_method = n1 - n2
else
' .....
Endif

' The Method has to be called as
'======================
set m_obj = AdvCreateObject("MyObject.myObject")

result = m_obj.var_method("add_two", 11, 22)

result = m_obj.var_method("substract_two"
, 17, 34) '??????
'======================

Boumxyz2
October 18th, 2001, 08:30 AM
This is a feature of languages like Lisps with which you can actually delay the evaluation of an expression till you tell him to do it...

VB isn't like that and you should do as the other guy says.. Pass a string parameter and check for that parameter to do the right call you wanna make.

Nic