472,351 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Passing function pointer in VB.Net

Coming from a C/C++ background, how would I pass a function pointer to a
function? I want to write a function that handles certain thread spawning.
Here's what I'm trying to invision:

function( thesub as <functionptr?> )
dim t as new system.threading.thread( _
new system.threading.threadstart( Addressof thesub ))
....

How can I get something like that going in VB.Net?
Nov 21 '05 #1
10 14853
Use delegates. Delegates are the equivalent of function pointers in .NET.

Regards,

Ron

"ChrisB" <Ch****@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
Coming from a C/C++ background, how would I pass a function pointer to a
function? I want to write a function that handles certain thread spawning.
Here's what I'm trying to invision:

function( thesub as <functionptr?> )
dim t as new system.threading.thread( _
new system.threading.threadstart( Addressof thesub ))
...

How can I get something like that going in VB.Net?

Nov 21 '05 #2
"Ron Bremmers" <r.********@home.nl> schrieb:
Use delegates. Delegates are the equivalent of function pointers in .NET.


Documentation:

Visual Basic Language Concepts -- Events and Delegates
<URL:http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconEventsDelegatesInheritance.asp>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
I tried using delegates but I don't know how. I tried something like this:

delegate function fpMyfunc()
(I also tried delegate sub fpMyfunc())

Then:
function( thesub as fpMyfunc )
dim t as new system.threading.thread( _
new system.threading.threadstart( Addressof thesub ))

But this did not work. VB.Net complained about the "thesub" after the
Addressof.. saying that I do not need ()'s after the sub name, as if it did
not recognize "thesub" as being a subroutine.
"Ron Bremmers" wrote:
Use delegates. Delegates are the equivalent of function pointers in .NET.

Regards,

Ron

"ChrisB" <Ch****@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
Coming from a C/C++ background, how would I pass a function pointer to a
function? I want to write a function that handles certain thread spawning.
Here's what I'm trying to invision:

function( thesub as <functionptr?> )
dim t as new system.threading.thread( _
new system.threading.threadstart( Addressof thesub ))
...

How can I get something like that going in VB.Net?


Nov 21 '05 #4
So.. any help here? Anyone else got delegates working as a parameter for a
function to be used with an AddressOf operator?

"ChrisB" wrote:
I tried using delegates but I don't know how. I tried something like this:

delegate function fpMyfunc()
(I also tried delegate sub fpMyfunc())

Then:
function( thesub as fpMyfunc )
dim t as new system.threading.thread( _
new system.threading.threadstart( Addressof thesub ))

But this did not work. VB.Net complained about the "thesub" after the
Addressof.. saying that I do not need ()'s after the sub name, as if it did
not recognize "thesub" as being a subroutine.
"Ron Bremmers" wrote:
Use delegates. Delegates are the equivalent of function pointers in .NET.

Regards,

Ron

"ChrisB" <Ch****@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
Coming from a C/C++ background, how would I pass a function pointer to a
function? I want to write a function that handles certain thread spawning.
Here's what I'm trying to invision:

function( thesub as <functionptr?> )
dim t as new system.threading.thread( _
new system.threading.threadstart( Addressof thesub ))
...

How can I get something like that going in VB.Net?


Nov 21 '05 #5
So.. any help here? Anyone else got delegates working as a parameter for a
function to be used with an AddressOf operator?

"ChrisB" wrote:
I tried using delegates but I don't know how. I tried something like this:

delegate function fpMyfunc()
(I also tried delegate sub fpMyfunc())

Then:
function( thesub as fpMyfunc )
dim t as new system.threading.thread( _
new system.threading.threadstart( Addressof thesub ))

But this did not work. VB.Net complained about the "thesub" after the
Addressof.. saying that I do not need ()'s after the sub name, as if it did
not recognize "thesub" as being a subroutine.
"Ron Bremmers" wrote:
Use delegates. Delegates are the equivalent of function pointers in .NET.

Regards,

Ron

"ChrisB" <Ch****@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
Coming from a C/C++ background, how would I pass a function pointer to a
function? I want to write a function that handles certain thread spawning.
Here's what I'm trying to invision:

function( thesub as <functionptr?> )
dim t as new system.threading.thread( _
new system.threading.threadstart( Addressof thesub ))
...

How can I get something like that going in VB.Net?


Nov 21 '05 #6
ThreadStart wants the name of the actual method that's being executed
because it's actually creating a delegate as well, and I don't think you
can have a delegate of a delegate. Here's what I came up with that
might meet your needs:

Private Sub ThisTest()
'Do Something
End Sub

Private Function StartAThread(ByVal thisSub As Threading.ThreadStart)
Dim t as New Threading.Thread(thisSub)
End Function

Private Sub MyTest()
Dim ts as New Threading.ThreadStart(AddressOf ThisTest)
SomeName(ts)
End Sub

'ThisTest' is the routine you are wanting to run on a separate thread.
'StartAThread' is the routine that actually creates the new thread an
excutes the passed method to it.
'MyTest' is the routine that is running a finds a method that needs to
be spawned on a separate thread.

All you have to do is create the ThreadStart object and pass that to the
'StartAThread' function and everything should work.

Hopefully this helps,
Brian Swanson

"ChrisB" <Ch****@discussions.microsoft.com> wrote in message
news:Ch****@discussions.microsoft.com:
So.. any help here? Anyone else got delegates working as a parameter for a

function to be used with an AddressOf operator?

"ChrisB" wrote:
I tried using delegates but I don't know how. I tried something like
this:

delegate function fpMyfunc()
(I also tried delegate sub fpMyfunc())

Then:
function( thesub as fpMyfunc )
dim t as new system.threading.thread( _
new system.threading.threadstart( Addressof thesub ))

But this did not work. VB.Net complained about the "thesub" after the
Addressof.. saying that I do not need ()'s after the sub name, as if it
did
not recognize "thesub" as being a subroutine.
"Ron Bremmers" wrote:
Use delegates. Delegates are the equivalent of function pointers in
.NET.

Regards,

Ron

"ChrisB" <Ch****@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
> Coming from a C/C++ background, how would I pass a function pointer
> to a
> function? I want to write a function that handles certain thread
> spawning.
> Here's what I'm trying to invision:
>
> function( thesub as <functionptr?> )
> dim t as new system.threading.thread( _
> new system.threading.threadstart( Addressof thesub ))
> ...
>
> How can I get something like that going in VB.Net?


Nov 21 '05 #7
ThreadStart wants the name of the actual method that's being executed
because it's actually creating a delegate as well, and I don't think you
can have a delegate of a delegate. Here's what I came up with that
might meet your needs:

Private Sub ThisTest()
'Do Something
End Sub

Private Function StartAThread(ByVal thisSub As Threading.ThreadStart)
Dim t as New Threading.Thread(thisSub)
End Function

Private Sub MyTest()
Dim ts as New Threading.ThreadStart(AddressOf ThisTest)
SomeName(ts)
End Sub

'ThisTest' is the routine you are wanting to run on a separate thread.
'StartAThread' is the routine that actually creates the new thread an
excutes the passed method to it.
'MyTest' is the routine that is running a finds a method that needs to
be spawned on a separate thread.

All you have to do is create the ThreadStart object and pass that to the
'StartAThread' function and everything should work.

Hopefully this helps,
Brian Swanson

"ChrisB" <Ch****@discussions.microsoft.com> wrote in message
news:Ch****@discussions.microsoft.com:
So.. any help here? Anyone else got delegates working as a parameter for a

function to be used with an AddressOf operator?

"ChrisB" wrote:
I tried using delegates but I don't know how. I tried something like
this:

delegate function fpMyfunc()
(I also tried delegate sub fpMyfunc())

Then:
function( thesub as fpMyfunc )
dim t as new system.threading.thread( _
new system.threading.threadstart( Addressof thesub ))

But this did not work. VB.Net complained about the "thesub" after the
Addressof.. saying that I do not need ()'s after the sub name, as if it
did
not recognize "thesub" as being a subroutine.
"Ron Bremmers" wrote:
Use delegates. Delegates are the equivalent of function pointers in
.NET.

Regards,

Ron

"ChrisB" <Ch****@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
> Coming from a C/C++ background, how would I pass a function pointer
> to a
> function? I want to write a function that handles certain thread
> spawning.
> Here's what I'm trying to invision:
>
> function( thesub as <functionptr?> )
> dim t as new system.threading.thread( _
> new system.threading.threadstart( Addressof thesub ))
> ...
>
> How can I get something like that going in VB.Net?


Nov 21 '05 #8
ChrisB.
Think of "AddressOf" as New for Delegates, you only need to use it when you
want to create a new delegate, not when you already have an existing
Delegate.

Define your function as taking a ThreadStart parameter, when you call your
function use AddressOf, when you create the Thread itself you do not need
AddressOf as you already have a Delegate.

Something like:

Imports System.Threading

Private Sub Worker()
' do work here...
End Sub

Private Sub MoreWork()
' do more work here...
End Sub

Private Sub SubmitWork(ByVal worker As ThreadStart)
Dim theThread As New Thread(worker)
theThread.Start()
End Sub

Public Sub Main()
SubmitWork(AddressOf Worker)
SubmitWork(AddressOf MoreWork)
End Sub

Notice that when we call SubmitWork we use AddressOf to create a new
Delegate. Then when we create a new Thread within SubmitWork, we simply pass
that Delegate directly to the constructor.

Hope this helps
Jay

"ChrisB" <Ch****@discussions.microsoft.com> wrote in message
news:D2**********************************@microsof t.com...
So.. any help here? Anyone else got delegates working as a parameter for a
function to be used with an AddressOf operator?

"ChrisB" wrote:
I tried using delegates but I don't know how. I tried something like
this:

delegate function fpMyfunc()
(I also tried delegate sub fpMyfunc())

Then:
function( thesub as fpMyfunc )
dim t as new system.threading.thread( _
new system.threading.threadstart( Addressof thesub ))

But this did not work. VB.Net complained about the "thesub" after the
Addressof.. saying that I do not need ()'s after the sub name, as if it
did
not recognize "thesub" as being a subroutine.
"Ron Bremmers" wrote:
> Use delegates. Delegates are the equivalent of function pointers in
> .NET.
>
>
>
> Regards,
>
>
>
> Ron
>
>
>
> "ChrisB" <Ch****@discussions.microsoft.com> wrote in message
> news:87**********************************@microsof t.com...
> > Coming from a C/C++ background, how would I pass a function pointer
> > to a
> > function? I want to write a function that handles certain thread
> > spawning.
> > Here's what I'm trying to invision:
> >
> > function( thesub as <functionptr?> )
> > dim t as new system.threading.thread( _
> > new system.threading.threadstart( Addressof thesub ))
> > ...
> >
> > How can I get something like that going in VB.Net?
>
>
>

Nov 21 '05 #9
ChrisB.
Think of "AddressOf" as New for Delegates, you only need to use it when you
want to create a new delegate, not when you already have an existing
Delegate.

Define your function as taking a ThreadStart parameter, when you call your
function use AddressOf, when you create the Thread itself you do not need
AddressOf as you already have a Delegate.

Something like:

Imports System.Threading

Private Sub Worker()
' do work here...
End Sub

Private Sub MoreWork()
' do more work here...
End Sub

Private Sub SubmitWork(ByVal worker As ThreadStart)
Dim theThread As New Thread(worker)
theThread.Start()
End Sub

Public Sub Main()
SubmitWork(AddressOf Worker)
SubmitWork(AddressOf MoreWork)
End Sub

Notice that when we call SubmitWork we use AddressOf to create a new
Delegate. Then when we create a new Thread within SubmitWork, we simply pass
that Delegate directly to the constructor.

Hope this helps
Jay

"ChrisB" <Ch****@discussions.microsoft.com> wrote in message
news:D2**********************************@microsof t.com...
So.. any help here? Anyone else got delegates working as a parameter for a
function to be used with an AddressOf operator?

"ChrisB" wrote:
I tried using delegates but I don't know how. I tried something like
this:

delegate function fpMyfunc()
(I also tried delegate sub fpMyfunc())

Then:
function( thesub as fpMyfunc )
dim t as new system.threading.thread( _
new system.threading.threadstart( Addressof thesub ))

But this did not work. VB.Net complained about the "thesub" after the
Addressof.. saying that I do not need ()'s after the sub name, as if it
did
not recognize "thesub" as being a subroutine.
"Ron Bremmers" wrote:
> Use delegates. Delegates are the equivalent of function pointers in
> .NET.
>
>
>
> Regards,
>
>
>
> Ron
>
>
>
> "ChrisB" <Ch****@discussions.microsoft.com> wrote in message
> news:87**********************************@microsof t.com...
> > Coming from a C/C++ background, how would I pass a function pointer
> > to a
> > function? I want to write a function that handles certain thread
> > spawning.
> > Here's what I'm trying to invision:
> >
> > function( thesub as <functionptr?> )
> > dim t as new system.threading.thread( _
> > new system.threading.threadstart( Addressof thesub ))
> > ...
> >
> > How can I get something like that going in VB.Net?
>
>
>

Nov 21 '05 #10
Aha. OK thank you very much, Brian and Jay. This sounds like what I need to
know. I will try it.
"Jay B. Harlow [MVP - Outlook]" wrote:
ChrisB.
Think of "AddressOf" as New for Delegates, you only need to use it when you
want to create a new delegate, not when you already have an existing
Delegate.

Define your function as taking a ThreadStart parameter, when you call your
function use AddressOf, when you create the Thread itself you do not need
AddressOf as you already have a Delegate.

Something like:

Imports System.Threading

Private Sub Worker()
' do work here...
End Sub

Private Sub MoreWork()
' do more work here...
End Sub

Private Sub SubmitWork(ByVal worker As ThreadStart)
Dim theThread As New Thread(worker)
theThread.Start()
End Sub

Public Sub Main()
SubmitWork(AddressOf Worker)
SubmitWork(AddressOf MoreWork)
End Sub

Notice that when we call SubmitWork we use AddressOf to create a new
Delegate. Then when we create a new Thread within SubmitWork, we simply pass
that Delegate directly to the constructor.

Hope this helps
Jay

Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If...
1
by: kevin | last post by:
Does any one know how to pass a function pointer as a function parameter from VB .NET to a C dll? Currently I'm passing it this way Public...
0
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed...
4
by: peter | last post by:
I can't get this code to link properly. I got two error during link: error LNK2028: unresolved token (0A00000C) "extern "C" int __stdcall...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.