CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2001
    Posts
    1

    variable for methods

    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



  2. #2
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Re: variable for methods

    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) '??????
    '======================





    If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it

  3. #3
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: variable for methods

    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

    Nicolas Bohemier

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

Featured