Saif Khan
Saif Khan

Reputation: 18792

Trying to understand Control.BeginInvoke code over at MSDN

I am referring to this document on MSDN. I understand what ".BeginInvoke" does, however looking at the example code on the document

Delegate Sub MyDelegate(myControl As Label, myArg2 As String)

Private Sub Button_Click(sender As Object, e As EventArgs)
   Dim myArray(1) As Object

   myArray(0) = New Label()
   myArray(1) = "Enter a Value"
   myTextBox.BeginInvoke(New MyDelegate(AddressOf DelegateMethod), myArray)
End Sub 'Button_Click

Public Sub DelegateMethod(myControl As Label, myCaption As String)
   myControl.Location = New Point(16, 16)
   myControl.Size = New Size(80, 25)
   myControl.Text = myCaption
   Me.Controls.Add(myControl)
End Sub 'DelegateMethod

The delegate myDelegate (and the DelegateMethod) accepts a control and a string, but, at the .BeginInvoke, a Label control is passed and an array...

myTextBox.BeginInvoke(New MyDelegate(AddressOf DelegateMethod), myArray)

and in the "DelegateMethod" there is

 myControl.Text = myCaption

Shouldn't a string be passed instead of the array? Am I missing something?

Upvotes: 0

Views: 4884

Answers (2)

Dustin Hodges
Dustin Hodges

Reputation: 4195

BeginInvoke can accept two parameters. One is a delegate, in this case AddressOf DelegateMethod.

The other parameter is an array of parameters. DelegateMethod accepts two parameters: a label and a string. In order to pass these using begininvoke, an array of objects with two members is passed in to beinginvoke to match the parameters of the method: a label and a string.

So both the label and the string are passed in using this array

Upvotes: 3

agent-j
agent-j

Reputation: 27923

Your code is correct. The framework casts the parameters appropriately from the object array on your behalf.

Upvotes: 1

Related Questions