Okay... I am totally confused on Delegates

  • Thread starter Thread starter pch
  • Start date Start date
P

pch

Guest
I have an application that was originally writen in VB 6.0 that uses a DLL implemented in C++ 6.0. I use callback functions to return data from the DLL back to the VB environment. I also have a class implemented in the VB side that acts primarily as a wrapper for the code in the DLL.

I am trying to convert the VB 6.0 side to .NET and having a real headache with the Delegate functions.

Basically the layout is as follows;

1) The VB app creates an instance of the VB class. In the VB class is a method that sets the callback address of a function defined in a BAS module (because the procname for AddressOf calls cannot reside in a class module) So, the class initialization calls something like;

InDLLSaveCallBackAddr(AddressOf SomeFunctionInBASModule)

2) A global variable of type MyClass exists in the application which points to the class instance Im talking about above.
When the DLL uses the callback, the function in the BAS module is called, which in turn uses reference to the class to get back inside it. IE;

In BAS Module

Sub MyCallBack(psStr as String)

mObj.ClassFunc(psStr) mObj is pointer to my class

End Sub


My problem is that in the conversion to .DOT, the AddressOf statement apparently has to be a delegate function, and either I have become really stupid, or these thingys just dont make sense.

If anyone can shed any words of wisdom on this while I still have some hair left, Id really appreciate it.

Thanks in advance.
 
To be honest Im not advanced enough to understand what you are doing, but I do know how to use delegates a little. I wrote up a sample console application that uses a delegate and hopefully it will help you enough to accomplish your task. :) If not Im afraid youll have to wait for one of the local gurus to help you out.

Also, delegates are classes in .NET. Being aware of that may help you understand them a little bit more.

Code:
Module Module1
    Declare our delegate class.
    Delegate Function PrintThis(ByVal somestring As String) As String

    Sub Main()
        Instantiate the class with the method we want to point to.
        Dim sc As New SomeClass()

        Instantiate the delegate class that points to the method
        in the class we instantiated above.
        Dim s As PrintThis = New PrintThis(AddressOf sc.PrintThis)

        Call the function.
        s("hello there")

        Call the function and return it to a string.
        Dim anotherString As String = s("hello there again")

        Print the returned string.
        Console.WriteLine("From Main: " + anotherString)

        Console.ReadLine()
    End Sub
End Module

Public Class SomeClass
    Function PrintThis(ByVal someString As String) As String
        Console.WriteLine("From SomeClass: " + someString)
        Return someString
    End Function
End Class
 
You dont actually use the delegate once you declare it. Its sole reason for existance (when it comes to using Win32 API callbacks) is for .NET to be able to expose the function to unmanaged code in a familiar format.
 
Back
Top