-1

I found some code online that was C#. I used telerik code converter to convert to VB.net. I am getting an error for the below code

Closed is already declared as 'public Even Closed(Sender as object, e as System.EventArgs)' in this class.

Same error happens for Shown as well. Anyone have some ideas to fix?

 #Region "Events"
    Public Event Closed As EventHandler
    Public Event Shown As EventHandler

    Protected Overridable Sub closed(e As EventArgs)
        Dim handler As EventHandler = Closed

        RaiseEvent handler(Me, e)
    End Sub

    Protected Overridable Sub shown(e As EventArgs)
        Dim handler As EventHandler = Shown

        RaiseEvent handler(Me, e)
    End Sub
#End Region

Here is C# code i converted to VB.

#region Events
    public event EventHandler Closed;
    public event EventHandler Shown;

    protected virtual void closed(EventArgs e)
    {
        EventHandler handler = Closed;

        if (handler != null) handler(this, e);
    }

    protected virtual void shown(EventArgs e)
    {
        EventHandler handler = Shown;

        if (handler != null) handler(this, e);
    }
    #endregion
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Steve
  • 55
  • 1
  • 10

2 Answers2

2

It looks to me this is a consequence of the fact that VB.NET is case insensitive meaning that an item shown is equivalent to an item Shown.

You can resolve the problem by renaming (better using some refactoring tool) one of the events, such that the name clash does not appear. You better do this at the C# project level, and then run your transcompiler tool again to produce the equivalent VB.NET code.

Community
  • 1
  • 1
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
1

There are 2 problems here.

First, if you are using WinForms, System.Windows.Forms already has Closed event which conflicts with the event you are creating. So if shadowing is intended you must shadow the event by declaring Shadows. But if you are declaring this in a normal class (which doesn't derive from Form) you need not worry about it.

Public Shadows Event Closed As EventHandler
Public Shadows Event Shown As EventHandler

Second, the property name and the event name cannot be same. Vb.Net is case-insensitive in this matter. So you should refactor the names as follows.

Public Event Closed As EventHandler
Public Event Shown As EventHandler

Protected Overridable Sub whenClosed(ByVal e As EventArgs)
    RaiseEvent Closed(Me, e)
End Sub

Protected Overridable Sub whenShown(ByVal e As EventArgs)
    RaiseEvent Shown(Me, e)
End Sub
Marshal
  • 6,551
  • 13
  • 55
  • 91
  • No, in that case C#/VB.NET will ask to create a `new Event ...`, you can (I know this is a terrible design decision) have multiple items in the inheritance path sharing the same name. – Willem Van Onsem Jan 05 '16 at 21:15
  • 1
    Yes but if the shadowing is intented, it should be shadowed. [Here is what MSDN says](https://msdn.microsoft.com/en-us/library/ya2h8das.aspx). And if shadowing is not intended he should change the event names like `WhenClosed` or `WhenShown` – Marshal Jan 05 '16 at 21:18