Matthew Vines
Matthew Vines

Reputation: 27561

VB.net newbie trying to convert some c# code

I've been having trouble converting the following fairly straight forward c# code into vb.net 4.0, which I understand has anonymous delegates. I just havn't been able to figure it out yet.

_combo.DataBound += (sender, args) =>
                                    {
                                        var item = _combo.FindItemByValue(values[0].ToString());
                                        if (item != null)
                                            item.Selected = true;
                                    };

I have tried the following

_combo.DataBound += Function(sender, args)
                                        Dim item = _combo.FindItemByValue(values(0).ToString())
                                        If item IsNot Nothing Then
                                            item.Selected = True
                                        End If

                                    End Function

But the compiler complains that DataBound can not be called directly, but has to be called with RaiseEvents

Upvotes: 0

Views: 387

Answers (2)

Matthew Vines
Matthew Vines

Reputation: 27561

I think I got it figured out.

declare an event

Private Event OnCombo_DataBound(ByVal values As ArrayList)

Use RaiseEvent to fire it

RaiseEvent OnCombo_DataBound(values)

Create a handler for the event

 Private Sub Combo_DataBound(ByVal values As System.Collections.ArrayList) Handles OnCombo_DataBound
                Dim item = _combo.FindItemByValue(values(0).ToString())
                If item IsNot Nothing Then
                    item.Selected = True
                End If
            End Sub

Upvotes: 1

epitka
epitka

Reputation: 17627

AddHandler _combo.DataBound, Function(sender, args)... End Function is I think proper syntax.

Upvotes: 2

Related Questions