473,379 Members | 1,386 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,379 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 15115
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 in my code I write: int cmp_fcn(...); int...
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 Delegate Sub DSCUserInterruptFunction() 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 function which then invokes it. Currently Im able to...
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 EnumFontFamiliesExW(struct HDC__ *,struct tagLOGFONTW...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.