Jump to content
Xtreme .Net Talk

Okay... I am totally confused on Delegates


Recommended Posts

Posted

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 I'm 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 don't make sense.

 

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

 

Thanks in advance.

Posted

To be honest I'm 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 I'm afraid you'll 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.

 

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

Gamer extraordinaire. Programmer wannabe.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...