I've turned Option Strict to On (Because I want to convert all this code from VB to C#) and there's this one situation I can't figure out. Here is a snippet of the code:

Private Sub MessageClicked(sender As Object, e As MouseButtonEventArgs)
    Dim obMenuItem = e.OriginalSource
    If obMenuItem.Tag IsNot Nothing Then

        Dim Message = MessageCollection.FirstOrDefault(Function(i) i.ID = obMenuItem.Tag.ToString)
        If Message.Outbound = False AndAlso Message.ReadBy = "" Then MarkAsRead(SelectedTractor, obMenuItem.Tag.ToString)
    End If
End Sub

The error arises any time time I try to use obMenuItem after it's been declared. I know I need to initialize it using the As phrase, but I don't know what I'm supposed to initialize it as.

I tried Object and MouseButtonEventArgs, but neither of those have the .tag I'm trying to access.

1 Answers

Clemens On Best Solutions

The Tag property is declared in the FrameworkElement class, so you need to perform the following type cast at least. Note that in case you need to access other propertyies of more derived classes, you must of course use a more derived target type for the cast:

Dim obMenuItem As FrameworkElement = CType(e.OriginalSource, FrameworkElement)

Instead of e.OriginalSource it seems also more appropriate to cast the sender object:

Dim obMenuItem As FrameworkElement = CType(sender, FrameworkElement)