question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

VB -> C#: WithEvents replacement causes NullReferenceException

See original GitHub issue

VB.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

Issue Analytics

  • State:closed
  • Created 6 months ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jrmoreno1commented, Apr 22, 2023

FWIW I recently saw the same problem for an asp.net application.

0reactions
GrahamTheCodercommented, Jun 17, 2023

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found