0

Hi I have been trying to convert a sample project written in C# to VB.NET, and I have successfully corrected most errors, bar the following two. Any tips on what the conversion should be would be appreciated:

The original C# code is as follows:

public event EventHandler<ExplorerErrorEventArgs> ExplorerError;

private void InvokeExplorerError(ExplorerErrorEventArgs e)
{
  EventHandler<ExplorerErrorEventArgs> handler = ExplorerError;
  if (handler != null) handler(this, e);
}

public ExplorerTreeView()
{
  Loaded += (s, e) => InitExplorer();

  SelectedItemChanged += OnSelectedItemChanged;
  AddHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(OnItemExpanded));
  AddHandler(TreeViewItem.CollapsedEvent, new RoutedEventHandler(OnItemCollapsed));
}

And the converted VB.NET code is here:

Public Event ExplorerError As EventHandler(Of ExplorerErrorEventArgs)

Private Sub InvokeExplorerError(e As ExplorerErrorEventArgs)
    Dim handler As EventHandler(Of ExplorerErrorEventArgs) = ExplorerError
    RaiseEvent handler(Me, e)
End Sub

Public Sub New()
    Loaded += Function(s, e) InitExplorer()

    SelectedItemChanged += OnSelectedItemChanged

    [AddHandler](TreeViewItem.ExpandedEvent, New RoutedEventHandler(OnItemExpanded))
    [AddHandler](TreeViewItem.CollapsedEvent, New RoutedEventHandler(OnItemCollapsed))
End Sub

The problem areas are:

 Dim handler As EventHandler(Of ExplorerErrorEventArgs) = ExplorerError

which underlines ExplorerError with the error:

Public Event ExplorerError(sender As Object, e As ExplorerErrorEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

Secondly:

Loaded += Function(s, e) InitExplorer()

produces the error:

Public Event Loaded(sender As Object, e As System.Windows.RoutedEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

There are also error squiggles under s,e, and InitExplorer but I suspect there is a syntax problem with this whole line. InitExplorer() is a Sub with no parameters.

I have read a bunch of articles to try and get ideas but I have not come up with anything. Any tips would be greatly appreciated! Thanks in advance.

3 Answers 3

3

In VB.NET, the only thing you can do with events is to raise them, or add/remove handlers. They cannot be referenced in a delegate object:

Dim handler As EventHandler(Of ExplorerErrorEventArgs) = ExplorerError

You probably want

Dim args = New ExplorerErrorEventArgs()
'fill args with any extra data
RaiseEvent ExplorerError(args)

C# allows you to reference the event directly, as a shortcut for raising the event:

ExplorerError(new ExplorerErrorEventArgs());

Again, this C# shortcut for adding handlers:

Loaded += (s,e) InitExplorer();

is not supported in VB.NET. You must use this for your second issue:

AddHandler Loaded, Sub(s,e) InitExplorer()

Additional reference: AddHandler and RemoveHandler

Sign up to request clarification or add additional context in comments.

3 Comments

VB events can be referenced in a delegate object - you just need to refer to the hidden VB 'Event' field: Dim handler As EventHandler(Of EventArgs) = ExplorerErrorEvent
@DaveDoknjas Is the event field subject to the same restrictions as events in C# - not raisable or settable (new MyClass().MyEvent = null) outside the class?
@ZevSpitz: I actually do not recall, but you could construct a simple test to find out - let me know what you find. I think it must have the same restrictions because the hidden VB field is private.
3

The event part can be re-written simply as this:

RaiseEvent ExplorerError(Me, e)

And the second one, with the handler for Loaded can be written simply as:

AddHandler Loaded, 
           Sub(s as Object, e as EventArgs) 
                 InitExplorer() 
           EndSub

1 Comment

@TripleAntigen: No problem. He was first by a few seconds anyway.
2

The first issue can be handled easily in VB via use of the hidden 'Event' field in VB (which exists for exactly these kind of cases). The following compiles fine in VB:

Public Event ExplorerError As EventHandler(Of ExplorerErrorEventArgs)

Private Sub InvokeExplorerError(ByVal e As ExplorerErrorEventArgs)
    'note the "Event" added to the end of "ExplorerError":
    Dim handler As EventHandler(Of ExplorerErrorEventArgs) = ExplorerErrorEvent
    If handler IsNot Nothing Then
        handler(Me, e)
    End If
End Sub

VB events are really a wrapper around 'raw' events like we have in C# - the 'Event' field is just the raw event that the VB event wraps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.