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
![Smile :-) :-)](/styles/default/custom/smilies/smile.gif)
. 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!