Results 1 to 7 of 7

Thread: AddressOf - With a Variable?

  1. #1

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    AddressOf - With a Variable?

    I am using Visual Studio 2005.

    I want to get a function reference like what the AddressOf statement returns but my function name will be stored in a variable. Does anybody know a way of doing this?
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  2. #2
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: AddressOf - With a Variable?

    At first sight it looks like you need to read about Reflection.
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  3. #3
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: AddressOf - With a Variable?

    You need to declare a Delegate

    A delegate is a type of variable that can hold a function (so long as the signature of that function matches the signature of the delegate declaration).

  4. #4
    Registered User Vektor's Avatar
    Join Date
    Mar 2006
    Location
    Australia
    Posts
    34

    Re: AddressOf - With a Variable?

    Here is a simple delegate example

    VB Code:
    1. Public Class Form1
    2.  
    3.    Private Delegate Sub MyDelete(ByVal showText As String)
    4.  
    5.    Private Sub Form1_Load(ByVal sender As System.Object, _
    6.       ByVal e As System.EventArgs) Handles MyBase.Load
    7.       Dim oDelegateObject As New MyDelete(AddressOf MyDelegateCallback)
    8.  
    9.       oDelegateObject.Invoke("I am using a delegate!")
    10.    End Sub
    11.  
    12.    Private Sub MyDelegateCallback(ByVal showText As String)
    13.       MessageBox.Show(showText.Trim())
    14.    End Sub
    15.  
    16. End Class

  5. #5

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: AddressOf - With a Variable?

    Vektor

    That is not quite what I'm after. Say I had several functions that had the same interface as the delegate. How could I specify the function name as a string variable?

    Perhaps if I gave a psudo-code example to explain what I mean:
    VB Code:
    1. Private Delegate Sub MyDelete(ByVal showText As String)
    2.  
    3.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.       Dim strFunctionName as String
    5.       Dim oDelegateObject As MyDelete
    6.      
    7.       strFunctionName = "MyDelegateCallback"
    8.  
    9.       oDelegateObject = New MyDelete(AddressOf strFunctionName)
    10.  
    11.       oDelegateObject.Invoke("I am using a delegate!")
    12.    End Sub
    13.  
    14.    Private Sub MyDelegateCallback(ByVal showText As String)
    15.       MessageBox.Show(showText.Trim())
    16.    End Sub

    NOTE: I need the function name to be a string variable because I will be getting it from an XML file.
    Last edited by simonm; Mar 17th, 2006 at 03:47 AM.
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: AddressOf - With a Variable?

    Here's a simple example using a MethodInvoker delegate. You can quite easily substitute your own.
    VB Code:
    1. Dim methodName As String
    2.         Dim invoker As MethodInvoker = DirectCast([Delegate].CreateDelegate(GetType(MethodInvoker), Me.GetType(), methodName), MethodInvoker)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: AddressOf - With a Variable?

    That looks like the one! Thanks.
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

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