Example for CallByName
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Example for CallByName

  1. #1
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180

    Example for CallByName

    Hi guys

    Could you give me a code example for CallByName please ?
    I know this appears in the Most FAQ section, but nowhere I could find an example.

    Thanks

    Suhaib

  2. #2
    Join Date
    Jul 2002
    Location
    India
    Posts
    505
    A few examples

    Call the setfocus method of a textbox named text2
    Code:
    CallByName Text2, "setfocus", VbMethod
    Set the Enabled property of the textbox
    Code:
    CallByName Text2, "Enabled", VbLet, False
    Get the text of the textbox
    Code:
    MsgBox CallByName(Text2, "Text", VbGet)
    Hope this helps
    Satish

  3. #3
    Join Date
    Jan 2003
    Location
    Cambridge, UK
    Posts
    752
    From MSDN

    In the following example, the first line uses CallByName to set the MousePointer property of a text box, the second line gets the value of the MousePointer property, and the third line invokes the Move method to move the text box:

    CallByName Text1, "MousePointer", vbLet, vbCrosshair
    Result = CallByName (Text1, "MousePointer", vbGet)
    CallByName Text1, "Move", vbMethod, 100, 100
    Best wishes,
    Cheers,

    Alex
    Please rate this post if you find it helpful

  4. #4
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180
    Thank you guys,

    Now I understand the purpose of CallByName.
    Also, I would like to know if there is a way to call user-defined functions in a similar fashion. If yes, kindly enlighten me on it.


    Suhaib

  5. #5
    Join Date
    Nov 2002
    Location
    Baby Land
    Posts
    646
    Wraps the user function as public function in a Class Module then instantiate the class in a variable then use callbyname

    dim c as class1
    set c=new class1

    callbyname c,"FunctionName",vbmethod, Param1, Param2,....

    set c=nothing

  6. #6
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180
    Hi Luthv,

    I tried your suggestion.

    Following is my class file :-
    =======================
    Public Function mA(a As Integer, b As Integer, ByRef res As Integer)
    res = a + b
    End Function

    Public Function mB(a As Integer, b As Integer, ByRef res As Integer)
    res = a - b
    End Function


    And my VB program is as follows :-
    =======================
    Private Sub Command1_Click()
    Dim c As prjCBN.clsCBN, sum%, dif%
    Set c = New prjCBN.clsCBN
    CallByName c, "mA", VbMethod, 2, 4, sum
    MsgBox "sum : " & sum
    CallByName c, "mB", VbMethod, 32, 7, dif
    MsgBox "dif : " & dif
    Set c = Nothing
    End Sub


    It works well as expected. BUT, what if my mA or mB method were to return a value instead of assigning the result to a ByRef variable ? How would I be able to handle the return value using CallByName ?
    In the class file I made the function return a value, and then in VB code tried something like -

    x= CallByName(c, "mA", VbMethod, 2, 4)

    BUT, it displayed a '..does not support..' error

    Please help.


    Thank you

    Suhaib

  7. #7
    Join Date
    Nov 2002
    Location
    Baby Land
    Posts
    646
    First modify the class function by removing the byref param
    and set the return value for the function :

    Public Function mA(a As Integer, b As Integer)
    mA= a + b
    End Function

    Call it in the form

    Dim c As Class1, x
    Set c = New Class1
    x = CallByName(c, "mA", VbMethod, 2, 4)
    MsgBox x

    Set c = Nothing

  8. #8
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180
    thanks dude, now it works

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center