|
-
Mar 16th, 2006, 06:45 AM
#1
Thread Starter
Fanatic Member
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. 
-
Mar 16th, 2006, 09:43 AM
#2
Fanatic Member
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 ...
-
Mar 16th, 2006, 10:17 AM
#3
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).
-
Mar 16th, 2006, 11:10 AM
#4
Registered User
Re: AddressOf - With a Variable?
Here is a simple delegate example
VB Code:
Public Class Form1
Private Delegate Sub MyDelete(ByVal showText As String)
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim oDelegateObject As New MyDelete(AddressOf MyDelegateCallback)
oDelegateObject.Invoke("I am using a delegate!")
End Sub
Private Sub MyDelegateCallback(ByVal showText As String)
MessageBox.Show(showText.Trim())
End Sub
End Class
-
Mar 17th, 2006, 03:41 AM
#5
Thread Starter
Fanatic Member
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:
Private Delegate Sub MyDelete(ByVal showText As String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strFunctionName as String
Dim oDelegateObject As MyDelete
strFunctionName = "MyDelegateCallback"
oDelegateObject = New MyDelete(AddressOf strFunctionName)
oDelegateObject.Invoke("I am using a delegate!")
End Sub
Private Sub MyDelegateCallback(ByVal showText As String)
MessageBox.Show(showText.Trim())
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. 
-
Mar 17th, 2006, 04:09 AM
#6
Re: AddressOf - With a Variable?
Here's a simple example using a MethodInvoker delegate. You can quite easily substitute your own.
VB Code:
Dim methodName As String
Dim invoker As MethodInvoker = DirectCast([Delegate].CreateDelegate(GetType(MethodInvoker), Me.GetType(), methodName), MethodInvoker)
-
Mar 17th, 2006, 04:14 AM
#7
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|