Error while invoking the delegate
-
Public Class Form1
Delegate Sub d(ByVal b As Boolean, ByVal b1() As Button)
Private Sub disablefunction(ByVal val As Boolean, ByVal ParamArray b() As Button)
For i As Integer = 0 To b.Length - 1
b(i).Enabled = val
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Invoke(New d(AddressOf disablefunction), False, Button1, Button2)
End Sub
End Class
</pre>
I am getting the following error
Parameter count mismatch -
Public Class Form1
Delegate Sub d(ByVal b As Boolean, ByVal b1() As Button)
Private Sub disablefunction(ByVal val As Boolean, ByVal ParamArray b() As Button)
For i As Integer = 0 To b.Length - 1
b(i).Enabled = val
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Invoke(New d(AddressOf disablefunction), False, Button1, Button2)
End Sub
End Class
</pre>
I am getting the following error
Parameter count mismatchIt's sure VERY nasty that you're passing controls arround, but does ParamArray mean you can pass as many buttons as you like, or do you need to create the array and pas it ? Is that really how you call a delegate in VB ? It looks nasty compared to C#
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
-
It's sure VERY nasty that you're passing controls arround, but does ParamArray mean you can pass as many buttons as you like, or do you need to create the array and pas it ? Is that really how you call a delegate in VB ? It looks nasty compared to C#
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
Pretty sure ParamArray is the equivalent of the C# params keyword. Regardless, his delegate signature is d(bool, Button) and he's calling it with two Buttons...
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games. -
It's sure VERY nasty that you're passing controls arround, but does ParamArray mean you can pass as many buttons as you like, or do you need to create the array and pas it ? Is that really how you call a delegate in VB ? It looks nasty compared to C#
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
Christian Graus wrote:
Is that really how you call a delegate in VB ? It looks nasty compared to C#
Not exactly. He's doing Me.Invoke, which would call a method on the main thread. From what I know, the same thing in C# would be (note that I've also fixed his problem here): this.Invoke((d)disablefunction, false, new button[] {Button1, Button2})
-
Public Class Form1
Delegate Sub d(ByVal b As Boolean, ByVal b1() As Button)
Private Sub disablefunction(ByVal val As Boolean, ByVal ParamArray b() As Button)
For i As Integer = 0 To b.Length - 1
b(i).Enabled = val
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Invoke(New d(AddressOf disablefunction), False, Button1, Button2)
End Sub
End Class
</pre>
I am getting the following error
Parameter count mismatchParamArray only works when you are directly calling the function. The arguments passed to a ParamArray parameter will always be converted to an array at compile time. When you are using Me.Invoke, you need to make the array yourself.
Me.Invoke(New d(AddressOf disablefunction), False, new Button() {Button1, Button2})
As a bonus, if you had declared the function asdisablefunction(ParamArray b() as object)
and called it asdisablefunction(new button() {Button1, Button2})
you would get a one element array that is an array of buttons.