Delegate for a property?

T

Terry

I have the need to declare a delegate for a property. I have this small user
control that I want to "bind" to a property in another class at run time. Is
there a way to use delegates to point to a property?
 
H

Herfried K. Wagner [MVP]

Terry said:
I have the need to declare a delegate for a property. I have this small
user
control that I want to "bind" to a property in another class at run time.
Is
there a way to use delegates to point to a property?

Untested: Obtain the 'MethodInfo' describing the property and pass it to
''[Delegate].CreateDelegate'.

However, I'd consider another design approach, possibly involving an
interface or a defined custom delegate type.
 
H

Herfried K. Wagner [MVP]

Addendum:

Sample (place the code in a Windows Form containing a button):

\\\
Private Delegate Function PropertyGetIntegerDelegate() As Integer
Private Delegate Sub PropertySetIntegerDelegate(ByVal Value As Integer)

Private Sub Button1_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles Button1.Click
Dim PropertyGetDelegate As PropertyGetIntegerDelegate = _
[Delegate].CreateDelegate(GetType(PropertyGetIntegerDelegate), Me,
"get_Width")
MsgBox(Me.Width)
MsgBox(PropertyGetDelegate())
Dim PropertySetDelegate As PropertySetIntegerDelegate = _
[Delegate].CreateDelegate(GetType(PropertySetIntegerDelegate), Me,
"set_Width")
PropertySetDelegate(100)
MsgBox(Me.Width)
End Sub
///
 
T

Terry

Herfried K. Wagner said:
Addendum:

Sample (place the code in a Windows Form containing a button):

\\\
Private Delegate Function PropertyGetIntegerDelegate() As Integer
Private Delegate Sub PropertySetIntegerDelegate(ByVal Value As Integer)

Private Sub Button1_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles Button1.Click
Dim PropertyGetDelegate As PropertyGetIntegerDelegate = _
[Delegate].CreateDelegate(GetType(PropertyGetIntegerDelegate), Me,
"get_Width")
MsgBox(Me.Width)
MsgBox(PropertyGetDelegate())
Dim PropertySetDelegate As PropertySetIntegerDelegate = _
[Delegate].CreateDelegate(GetType(PropertySetIntegerDelegate), Me,
"set_Width")
PropertySetDelegate(100)
MsgBox(Me.Width)
End Sub
///

Thanks Herfried! I was considering using the VB CallByName function. Have
any comment on the relative merits of the two solutions?

Thanks again,
Terry
 
H

Herfried K. Wagner [MVP]

Terry said:
Sample (place the code in a Windows Form containing a button):

\\\
Private Delegate Function PropertyGetIntegerDelegate() As Integer
Private Delegate Sub PropertySetIntegerDelegate(ByVal Value As Integer)

Private Sub Button1_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles Button1.Click
Dim PropertyGetDelegate As PropertyGetIntegerDelegate = _
[Delegate].CreateDelegate(GetType(PropertyGetIntegerDelegate),
Me,
"get_Width")
MsgBox(Me.Width)
MsgBox(PropertyGetDelegate())
Dim PropertySetDelegate As PropertySetIntegerDelegate = _
[Delegate].CreateDelegate(GetType(PropertySetIntegerDelegate),
Me,
"set_Width")
PropertySetDelegate(100)
MsgBox(Me.Width)
End Sub
///

Thanks Herfried! I was considering using the VB CallByName function.
Have
any comment on the relative merits of the two solutions?

Good catch, I'd use 'CallByName' too because it really simplifies the access
compared to the solution I posted :). I don't know why I didn't mention
it...
 
T

Terry

Herfried K. Wagner said:
Terry said:
Sample (place the code in a Windows Form containing a button):

\\\
Private Delegate Function PropertyGetIntegerDelegate() As Integer
Private Delegate Sub PropertySetIntegerDelegate(ByVal Value As Integer)

Private Sub Button1_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles Button1.Click
Dim PropertyGetDelegate As PropertyGetIntegerDelegate = _
[Delegate].CreateDelegate(GetType(PropertyGetIntegerDelegate),
Me,
"get_Width")
MsgBox(Me.Width)
MsgBox(PropertyGetDelegate())
Dim PropertySetDelegate As PropertySetIntegerDelegate = _
[Delegate].CreateDelegate(GetType(PropertySetIntegerDelegate),
Me,
"set_Width")
PropertySetDelegate(100)
MsgBox(Me.Width)
End Sub
///

Thanks Herfried! I was considering using the VB CallByName function.
Have
any comment on the relative merits of the two solutions?

Good catch, I'd use 'CallByName' too because it really simplifies the access
compared to the solution I posted :). I don't know why I didn't mention
it...
Yes, the CallByName seems simpler, but I jsut ran a performance test doing a
million sets and gets and found that the CallByName took 51.57 seconds while
doing the same using delegates took .68 seconds! So, Im thinking that your
first solution is the one to use!

Thanks again,
Terry
 
C

Cor Ligthert[MVP]

Herfried,

I am always curious about the sense of this kind of code. A delegate to a
reference has for me always been like a mountain in the size as in Austria
in Holland, it offers us something (we can get more medails at the
Olympics), however for the rest it is only trouble.

:)

Cor

Herfried K. Wagner said:
Terry said:
I have the need to declare a delegate for a property. I have this small
user
control that I want to "bind" to a property in another class at run time.
Is
there a way to use delegates to point to a property?

Untested: Obtain the 'MethodInfo' describing the property and pass it to
''[Delegate].CreateDelegate'.

However, I'd consider another design approach, possibly involving an
interface or a defined custom delegate type.
 
H

Herfried K. Wagner [MVP]

Terry said:
Sample (place the code in a Windows Form containing a button):

\\\
Private Delegate Function PropertyGetIntegerDelegate() As Integer
Private Delegate Sub PropertySetIntegerDelegate(ByVal Value As
Integer)

Private Sub Button1_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles Button1.Click
Dim PropertyGetDelegate As PropertyGetIntegerDelegate = _
[Delegate].CreateDelegate(GetType(PropertyGetIntegerDelegate),
Me,
"get_Width")
MsgBox(Me.Width)
MsgBox(PropertyGetDelegate())
Dim PropertySetDelegate As PropertySetIntegerDelegate = _
[Delegate].CreateDelegate(GetType(PropertySetIntegerDelegate),
Me,
"set_Width")
PropertySetDelegate(100)
MsgBox(Me.Width)
End Sub
///

Thanks Herfried! I was considering using the VB CallByName function.
Have
any comment on the relative merits of the two solutions?

Good catch, I'd use 'CallByName' too because it really simplifies the
access
compared to the solution I posted :). I don't know why I didn't mention
it...

Yes, the CallByName seems simpler, but I jsut ran a performance test doing
a
million sets and gets and found that the CallByName took 51.57 seconds
while
doing the same using delegates took .68 seconds! So, Im thinking that
your
first solution is the one to use!

There is at least one level of indirection when using 'CallByName' and
'CallByName' has to evaluate parameters too, so this could be the case.
However, if you are only accessing the property rarely, I'd still use
'CallByName' ;-).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top