Delegates

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
hey guys im using this code
VB.NET:
If Me.CheckBox_SSR.Checked = True Then
            Me.ComboBox_SSR.Enabled = True
            Me.ComboBox_Spray.Enabled = False
            Me.CheckBox_Spray.Checked = False
            Me.ComboBox_fiber.Enabled = False
            Me.CheckBox_Fiber.Checked = False
            Me.ComboBox_Aroma.Enabled = False
            Me.CheckBox_Aroma.Checked = False
            Me.ComboBox_Glass.Enabled = False
            Me.CheckBox_Glass.Checked = False
            Dim executableitems_SSR As New List(Of ExecutableItem)
           [COLOR=#0000ff] executableitems_SSR.Add(New ExecutableItem With {.Text = "Scratch", .Method = AddressOf SSR_Scratches})
            executableitems_SSR.Add(New ExecutableItem With {.Text = "Stone Chip", .Method = AddressOf SSR_Stone_chips})
            executableitems_SSR.Add(New ExecutableItem With {.Text = "Walk Around", .Method = AddressOf SSR_Walk})[/COLOR]
            With ComboBox_SSR
                .DisplayMember = "Text"
                .ValueMember = "Method"
                .DataSource = executableitems_SSR
            End With
        End If
The code in blue is being used for calling methods but how can i do the following

VB.NET:
[COLOR=#0000FF]executableitems_SSR.Add(New ExecutableItem With {.Text = "Scratch", .Method = AddressOf [/COLOR][COLOR=#ff0000]SSR_Scratches(Damage_List.CheckedItems, ComboBox_Current_Car.SelectedItem)[/COLOR][COLOR=#0000ff]})[/COLOR]

i am getting this error if i do the above code
'AddressOf' operand must be the name of a method (without parentheses).

how can i Call a sub WHILE passing values to the sub il show the dataflow as such

user selects an option from a combobox which determins which sub to call then a checkedlistbox.itemchecked event passes the values to the sub and executes the sub.

need any more info just ask
 
A delegate is a reference to a method, not a method call. You don't provide the arguments until you actually execute the method. When you call Invoke or the like on the delegate, that is when you provide the arguments.
 
sorry could you give me 1 example using my code i can't figure out where i pass the information.
a radio button enables a combobox and does this
VB.NET:
Dim executableitems_SSR As New List(Of ExecutableItem)
            executableitems_SSR.Add(New ExecutableItem With {.Text = "Scratch", .Method = AddressOf SSR_Scratches})
            executableitems_SSR.Add(New ExecutableItem With {.Text = "Stone Chip", .Method = AddressOf SSR_Stone_chips})
            executableitems_SSR.Add(New ExecutableItem With {.Text = "Walk Around", .Method = AddressOf SSR_Walk})
            With ComboBox_SSR
                .DisplayMember = "Text"
                .ValueMember = "Method"
                .DataSource = executableitems_SSR
            End With

The user would select an option from the combobox and it would do the following
VB.NET:
method = DirectCast(ComboBox_SSR.SelectedValue, Action)

a checkedlistbox would be populated by now
and on the click even on the listbox it would call the method.invoke
VB.NET:
 method.Invoke()

where do i pass the arguments

if i dont put the arguments in the
VB.NET:
 executableitems_SSR.Add(New ExecutableItem With {.Text = "Scratch", .Method = AddressOf SSR_Scratches})
then i get this error
Method 'Public Sub SSR_Scratches(Damage As System.Windows.Forms.CheckedListBox.CheckedItemCollection, SelectCar As car)' does not have a signature compatible with delegate 'Delegate Sub Action()'.

and if i do put the arguments in then i get this error
'AddressOf' operand must be the name of a method (without parentheses)

sorry i know im kinda relying on you guys atm but i just dont have a clue
 
The method signature that you assign indicates a Action(Of T1, T2) delegate, eg Action(Of CheckedListBox.CheckedItemCollection, car), and not a Action delegate that has no parameters.
Since SelectedItem and SelectedValue is type Object you have to cast to get the right kind of type to operate, then call the delegated method:
Dim item = CType(ComboBox_SSR.SelectedItem, ExecutableItem)
item.Method(Damage_List.CheckedItems, ComboBox_Current_Car.SelectedItem)

or (where value here reflects the ExecutableItem.Method member)
Dim value = CType(ComboBox_SSR.SelectedValue, Action(Of CheckedListBox.CheckedItemCollection, car))
value(Damage_List.CheckedItems, ComboBox_Current_Car.SelectedItem)
 
Back
Top