Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AddressOf operator

Status
Not open for further replies.

EnCocytus

Programmer
Dec 18, 2002
24
US
ALright, Ineed some help with the addressOf operator. I am using a .Dll file that requires a callback.
But I keep getting an error message "Invalid use of AddressOf operator". Here is an exerpt of my code.

Code:
Private Declare Function client Lib "WinSox.dll" _
    Alias "WinClient" (ByVal ip As String, ByVal port As Integer, ByVal addrFunct As Long) As Integer

Private Sub Command3_Click()

    Dim nret As Integer

    client "127.0.0.1", 8888, AddressOf showMsg

End Sub 

Public Function showMsg() As Integer

    MsgBox "showMsg()"
    showMsg = 1
    
End Function

From what I can tell, I'm using the AddressOf operator right, and I now it's not the .Dll. I've also tried using
a function like
Code:
Private Sub Command3_Click()

    Dim nret As Integer

    client "127.0.0.1", 8888, FuncAddr(AddressOf showMsg)

End Sub 

Private Function FuncAddr(ByVal pfn As Long) As Long
    FuncAddr = pfn
End Function

But I still get the error. What am I missing? Thanks in advance.

1001100 1110101 1101011 1100101
 
Strongm is correct - you can only use AddressOf with code that is contained in a Module (.bas).

The reason is if the code is in a class, the runtime can't tell which instance of the class to call. Could be instance "A" or instance "B" ... it doesn't know what you intended.

This problem is solved in .NET because the classes can have static methods, which are methods common to all instances of a particular class.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top