VB -> C#: WithEvents replacement causes NullReferenceException
See original GitHub issueVB.Net input code
Imports System
Public Module Program
Public Sub Main(args As String())
Dim c As New SomeClass(New SomeDependency())
Console.WriteLine("Done")
End Sub
End Module
Public Class SomeDependency
Public Event SomeEvent As EventHandler
End Class
Public Class SomeClass
Private WithEvents _dep As SomeDependency
Public Sub New(dep)
_dep = dep
End Sub
Private Sub _dep_SomeEvent(sender As Object, e As EventArgs) Handles _dep.SomeEvent
' Do Something
End Sub
End Class
Erroneous output
using System;
using System.Runtime.CompilerServices;
public static partial class Program
{
public static void Main(string[] args)
{
var c = new SomeClass(new SomeDependency());
Console.WriteLine("Done");
}
}
public partial class SomeDependency
{
public event EventHandler SomeEvent;
}
public partial class SomeClass
{
private SomeDependency __dep;
private SomeDependency _dep
{
[MethodImpl(MethodImplOptions.Synchronized)]
get
{
return __dep;
}
[MethodImpl(MethodImplOptions.Synchronized)]
set
{
if (__dep != null)
{
__dep.SomeEvent -= _dep_SomeEvent;
}
__dep = value;
if (__dep != null)
{
__dep.SomeEvent += _dep_SomeEvent;
}
}
}
public SomeClass(object dep)
{
_dep.SomeEvent += _dep_SomeEvent;
_dep = (SomeDependency)dep;
}
private void _dep_SomeEvent(object sender, EventArgs e)
{
// Do Something
}
}
The VB code prints “Done”, whereas the C# code throws a NullReferenceException in the first line of the constructor of SomeClass, since _dep
has not been initialized yet.
Expected output
The SomeClass constructor should not contain the line _dep.SomeEvent += _dep_SomeEvent;
. This line only makes sense if _dep
has an initializer.
Details
- Product in use: icsharpcode.github.io/CodeConverter
- Version in use: 9.1.3.0
Issue Analytics
- State:
- Created 6 months ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
c# - What is a NullReferenceException, and how do I fix it?
A NullReferenceException will be raised in the case of early-created controls with event handlers, etc., that fire during InitializeComponent ...
Read more >NullReferenceException Class (System)
To address this problem, instantiate the object so that its value is no longer null . The following example does this by calling...
Read more >How can I fix the error: System.NullReferenceException
When you have a statement like A.B.C = E.F; , and you receive an NullReferenceException 'Object reference not set to an instance of...
Read more >Object reference not set to an instance of an object.
The NullReference Exception for Visual Basic is no different from the one in C#. After all, they are both reporting the same exception...
Read more >[Solved] c# NullReferenceException declaration of integer
your KeyDown event handler is triggered when the Text property is still null (i.e. the event is registered before the Text property is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
FWIW I recently saw the same problem for an asp.net application.
Busy few months but finally got back to looking at this. I think I’ve moved it a step in the right direction in the version in master right now. I’ll wait a little before releasing in case you or anyone else spots an issue with it since there are quite a lot of situations to consider