Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Error while invoking the delegate

Error while invoking the delegate

Scheduled Pinned Locked Moved Visual Basic
help
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nilish
    wrote on last edited by
    #1

    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

    C G 2 Replies Last reply
    0
    • N Nilish

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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.

      M G 2 Replies Last reply
      0
      • C Christian Graus

        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.

        M Offline
        M Offline
        Mark Churchill
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • C Christian Graus

          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.

          G Offline
          G Offline
          Gideon Engelberth
          wrote on last edited by
          #4

          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})

          1 Reply Last reply
          0
          • N Nilish

            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

            G Offline
            G Offline
            Gideon Engelberth
            wrote on last edited by
            #5

            ParamArray 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 as disablefunction(ParamArray b() as object) and called it as disablefunction(new button() {Button1, Button2}) you would get a one element array that is an array of buttons.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups