469,275 Members | 1,825 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

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 14306
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 discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.