473,385 Members | 1,907 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,385 software developers and data experts.

Passing AddressOf a function to constructor

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:

AddHandler ButtonNew.Click, AddressOf My_Passed_Function

I thought I could use a delegate but that doesn't seem to be working. Am I
on the right track?

Nov 21 '05 #1
2 2287
David,
You need to define your parameter as the type of handler you are going to
use, when you call your function/constructor you use addressof, when you
call AddHandler you can use the parameter (as its already has the addressof
something.

Something like:

Public Class Widget

Public Sub New(ByVal theHandler As EventHandler)
Dim ButtonNew As New Button
AddHandler ButtonNew.Click, theHandler
End Sub

End Class

Public Sub Widget_Click(ByVal sender As Object, ByVal e As EventArgs)

End Sub

Dim aWidget As New Widget(AddressOf Widget_Click)

You can find out the type of handler (Delegate) needed by looking up the
event in the online help.

Hope this helps
Jay

"David A. Osborn" <do********@hotmail.com> wrote in message
news:lLkFd.3072$IV5.1712@attbi_s54...
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:

AddHandler ButtonNew.Click, AddressOf My_Passed_Function

I thought I could use a delegate but that doesn't seem to be working. Am I
on the right track?

Nov 21 '05 #2
David,

Why not passing the control itself so. Something as

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Button1.Text = "0"
Dim mb As New myButtonClass(Me.Button1)
End Sub
End Class
Friend Class myButtonClass
Friend Sub New(ByVal this As Control)
AddHandler this.Click, AddressOf ButtonClick
End Sub
Private Sub ButtonClick(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim mybutton As Button = DirectCast(sender, Button)
mybutton.Text = (CInt(mybutton.Text) + 1).ToString
End Sub
End Clas
///
I hope this gives some ideas

Cor
Nov 21 '05 #3

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

Similar topics

5
by: Vinay | last post by:
Hi, I have created a Context Menu class (CMenu) with relevant items. Based on a selected Item, I am taking a separate action. The following code is working fine. CMenu(sOption as string)...
7
by: Richard Grant | last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter How do I do that in VB .NET What should be the syntax for a parameter to receive the address of a...
4
by: Anthony Coelho | last post by:
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...
0
by: Eric Sabine | last post by:
OK, I'm trying to further my understanding of threading. The code below I wrote as kind of a primer to myself and maybe a template that I could use in the future. What I tried to do was pass data...
3
by: kevin | last post by:
This is a repost with an update 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...
3
by: Sam Learner | last post by:
Hello everyone, I am developping an application, I create a thread for the application because it is about to download a large file, and wanted it to do it inside of a thread... Now, the function...
10
by: ChrisB | last post by:
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(...
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...
6
by: Sergey Poberezovskiy | last post by:
I have the following code in C# that I have trouble converting to VB(2.0): private delegate void openDialog(); private void openWindowsDialog(openDialog open) { Thread thread = new Thread(new...
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
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
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.