By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,286 Members | 1,632 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,286 IT Pros & Developers. It's quick & easy.

Using AddressOf... as a parameter

P: n/a
Hello Guru's!

I am trying to pass a function pointer as a parameter to a method and can't
seem to get it working. Basically I want to do the exact same thing that the
System.Threading.Thread constructor does in VB.NET within my own class. In
the Thread constructor, you can specify the function to run instead of
creating a ThreadStart object by using the AddressOf keyword such as; Dim
oThread As New Threading.Thread(AddressOf foo.Run)

I have a method that I would like to do the exact same thing as, but what do
you declare the type as? I've tried a delegate, but that's not working for
me. For example, say you have the following method:

Public foo(mydelegate as System.Delegate)

If you call this method passing a function pointer such as
myClass.foo(AddressOf myfunction.DoSomething) you get the following error:

'AddressOf' expression cannot be converted to 'System.Delegate' because
'System.Delegate' is not a delegate type.

How's that for a error? System.Delegate is not a delegate type? Are you as
confused on this as I am? So to wrap, does anyone know how I can pass a
function pointer into a method as a parameter just like the Threading.Thread
constructor allows?

Cheers,
Anthony
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,

Create a delegate function. Have the delegate function as the type
of the parameter passed in to the procedure. Here is an quick example.

Delegate Function MyFooCallBack(ByVal MyArugments As Integer) As Integer

Public foo(mydelegate as MyFooCallback)

http://www.elitevb.com/content/01,0075,01/04.aspx
Ken
----------------------------

"Anthony Coelho" <al******@sbcglobal.net> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Hello Guru's!

I am trying to pass a function pointer as a parameter to a method and
can't
seem to get it working. Basically I want to do the exact same thing that
the
System.Threading.Thread constructor does in VB.NET within my own class. In
the Thread constructor, you can specify the function to run instead of
creating a ThreadStart object by using the AddressOf keyword such as; Dim
oThread As New Threading.Thread(AddressOf foo.Run)

I have a method that I would like to do the exact same thing as, but what
do
you declare the type as? I've tried a delegate, but that's not working for
me. For example, say you have the following method:

Public foo(mydelegate as System.Delegate)

If you call this method passing a function pointer such as
myClass.foo(AddressOf myfunction.DoSomething) you get the following error:

'AddressOf' expression cannot be converted to 'System.Delegate' because
'System.Delegate' is not a delegate type.

How's that for a error? System.Delegate is not a delegate type? Are you as
confused on this as I am? So to wrap, does anyone know how I can pass a
function pointer into a method as a parameter just like the
Threading.Thread
constructor allows?

Cheers,
Anthony

Nov 20 '05 #2

P: n/a
In article <#q*************@TK2MSFTNGP09.phx.gbl>,
al******@sbcglobal.net says...
Hello Guru's!

I am trying to pass a function pointer as a parameter to a method and can't
seem to get it working. Basically I want to do the exact same thing that the
System.Threading.Thread constructor does in VB.NET within my own class. In
the Thread constructor, you can specify the function to run instead of
creating a ThreadStart object by using the AddressOf keyword such as; Dim
oThread As New Threading.Thread(AddressOf foo.Run)

I have a method that I would like to do the exact same thing as, but what do
you declare the type as? I've tried a delegate, but that's not working for
me.


First, define the delegate that matches the signature of what you want
to call:

Public Delegate Sub ShowStatusDelegate(newStatus As String)

This defines a delegate called "ShowStatusUpdate". It's signature is
basically:
1) A Sub
2) Accepts one string parameter

Now you need to define the actual procedure that you want pass via
AddressOf. It must match the signature above in your delegate (sub with
one string parameter)

Public Sub ShowStatus(data As String)
statusLable.Text = data
End Sub

Now, the method that accepts your 'AddressOf' parameter needs to accept
a type of "ShowStatusDelegate":

Public Sub foo(func As ShowStatusDelegate)
...
End Sub

Now you can call:

foo(AddressOf Me.ShowStatus)

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 20 '05 #3

P: n/a
Hi,

Create a delegate function. Have the delegate function as the type
of the parameter passed in to the procedure. Here is an quick example.

Delegate Function MyFooCallBack(ByVal MyArugments As Integer) As Integer

Public foo(mydelegate as MyFooCallback)

http://www.elitevb.com/content/01,0075,01/04.aspx
Ken
----------------------------

"Anthony Coelho" <al******@sbcglobal.net> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Hello Guru's!

I am trying to pass a function pointer as a parameter to a method and
can't
seem to get it working. Basically I want to do the exact same thing that
the
System.Threading.Thread constructor does in VB.NET within my own class. In
the Thread constructor, you can specify the function to run instead of
creating a ThreadStart object by using the AddressOf keyword such as; Dim
oThread As New Threading.Thread(AddressOf foo.Run)

I have a method that I would like to do the exact same thing as, but what
do
you declare the type as? I've tried a delegate, but that's not working for
me. For example, say you have the following method:

Public foo(mydelegate as System.Delegate)

If you call this method passing a function pointer such as
myClass.foo(AddressOf myfunction.DoSomething) you get the following error:

'AddressOf' expression cannot be converted to 'System.Delegate' because
'System.Delegate' is not a delegate type.

How's that for a error? System.Delegate is not a delegate type? Are you as
confused on this as I am? So to wrap, does anyone know how I can pass a
function pointer into a method as a parameter just like the
Threading.Thread
constructor allows?

Cheers,
Anthony

Nov 20 '05 #4

P: n/a
In article <#q*************@TK2MSFTNGP09.phx.gbl>,
al******@sbcglobal.net says...
Hello Guru's!

I am trying to pass a function pointer as a parameter to a method and can't
seem to get it working. Basically I want to do the exact same thing that the
System.Threading.Thread constructor does in VB.NET within my own class. In
the Thread constructor, you can specify the function to run instead of
creating a ThreadStart object by using the AddressOf keyword such as; Dim
oThread As New Threading.Thread(AddressOf foo.Run)

I have a method that I would like to do the exact same thing as, but what do
you declare the type as? I've tried a delegate, but that's not working for
me.


First, define the delegate that matches the signature of what you want
to call:

Public Delegate Sub ShowStatusDelegate(newStatus As String)

This defines a delegate called "ShowStatusUpdate". It's signature is
basically:
1) A Sub
2) Accepts one string parameter

Now you need to define the actual procedure that you want pass via
AddressOf. It must match the signature above in your delegate (sub with
one string parameter)

Public Sub ShowStatus(data As String)
statusLable.Text = data
End Sub

Now, the method that accepts your 'AddressOf' parameter needs to accept
a type of "ShowStatusDelegate":

Public Sub foo(func As ShowStatusDelegate)
...
End Sub

Now you can call:

foo(AddressOf Me.ShowStatus)

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.