473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Using AddressOf... as a parameter

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
4 20108
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andy | last post by:
I am having some trouble validating XML using the XmlValidatingReader. I have created some xml and used the visual studio to generate the schema. So I am confident that the xml and schema match. ...
15
by: Patrick.O.Ige | last post by:
Hi All i'm getting error with my TreeView Menu(I want to have a button with ExpandALL and CollapseALL):- Error:- 'AddressOf' operand must be the name of a method; no parentheses are needed. I...
6
by: Simon Verona | last post by:
I would normally use code such as : Dim Customer as new Customer Dim t as new threading.thread(AddressOf Customer.DisplayCustomer) Customer.CustomerId=MyCustomerId t.start Which would create...
2
by: David A. Osborn | last post by:
I need to pass the address of a function to the contructor of a class when I create an instance of it so that in the new instance I can dynamically hookup a handler by doing the following: ...
2
by: jcomber | last post by:
Hi, I'd be grateful for some help... I'm trying to create a late bound object using: Activator.CreateInstance(Type, ConstructorParameters()) The constructor of the object takes a delegate...
4
by: kaczmar2 | last post by:
I have a custom web control that is called from an aspx page. The custom control dynamically renders out buttons via a public method (AddButton). One of the attributes of the method is a...
0
by: =?Utf-8?B?Um9i?= | last post by:
I've a requirement to monitor when certain applications are started. I'm using WMI called from VS Stusio 2005 in VB to trap Excel and Word starting. I've written the following console application...
2
by: =?Utf-8?B?UmljaA==?= | last post by:
Is there a difference between AddressOf and Delegate for assiging a function/Sub to an event? Add_Handler txt1.Click AddressOf WhatColorFont or Private Sub Delegate WhatColorFont() ....
0
by: buu | last post by:
So, I have a new thread creating something like: dim myThread as new System.Threading.Thread(AddressOf myProcedure) but, could I send an any kind of parameter instead of call to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.