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#: Problem with Operator and Nothing

See original GitHub issue

Hello, there is a problem when using the operator and one of the class is null (if Test1 is nothing). In VB.NET the test returns true without entering into the operator code. In VB.NET a message appears (TEST) In C# there is a crash.

Input code

Public Class Form1
    Dim Test1 As CTst

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Test1 Is Nothing Then
            MessageBox.Show("Test ")
        End If

    End Sub


End Class

Friend Class CTst
    Public List As New List(Of Integer)

    Public Sub New()
    End Sub


    Public Shared Operator <>(ByVal aCls1 As CTst, ByVal aCls2 As CTst) As Boolean
        Return Not (aCls1 = aCls2)

    End Operator
    Public Shared Operator =(ByVal aCls1 As CTst, ByVal aCls2 As CTst) As Boolean
        If aCls1.List.Count <> aCls2.List.Count Then Return False
        Return True
    End Operator

End Class

Erroneous output

 public partial class Form1
    {
        public Form1()
        {
            InitializeComponent();
            _Button1.Name = "Button1";
        }

        private CTst Test1;

        private void Button1_Click(object sender, EventArgs e)
        {
            if (Test1 == null)
            {
                MessageBox.Show("Test ");
            }
        }
    }

    internal class CTst
    {
        public List<int> List = new List<int>();

        public CTst()
        {
        }

        public static bool operator !=(CTst aCls1, CTst aCls2)
        {
            return !(aCls1 == aCls2);
        }

        public static bool operator ==(CTst aCls1, CTst aCls2)
        {
            if (aCls1.List.Count != aCls2.List.Count)
                return false;
            return true;
        }
    }

Expected output

 public partial class Form1
    {
        public Form1()
        {
            InitializeComponent();
            _Button1.Name = "Button1";
        }

        private CTst Test1;

        private void Button1_Click(object sender, EventArgs e)
        {
            if (Test1 is null)
            {
                MessageBox.Show("Test ");
            }
        }
    }

    internal class CTst
    {
        public List<int> List = new List<int>();

        public CTst()
        {
        }

        public static bool operator !=(CTst aCls1, CTst aCls2)
        {
            return !(aCls1 == aCls2);
        }

        public static bool operator ==(CTst aCls1, CTst aCls2)
        {
            if (aCls1.List.Count != aCls2.List.Count)
                return false;
            return true;
        }
    }

Details

Version 8.1.5.0 Hello, When there is a test with is nothing it is converted to ==null that triggers the operator == (that crashes in the List.Count) while we could use is null to solve the problem.

EDIT If I compare Dim Test1 As New CTst Dim Test2 As CTst if Test1 is Test2 (VB.NET) (to see if I have the same reference (so object) there is a crash in C# if (Test1 == Test2) will crash if one of the class is null you must use if ( object.ReferenceEquals(Test1, Test2)) EDIT2 : in the reflector there is (object)Test1 == (object) Test2 For the first case (is null instead of ==) in the reflector it is still converted to == but there is no crash)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
marcalcommented, Jul 11, 2020

Hello, The problem with Is null is working. There is still a problem when you compare two classes and one class is null (

I will open another case.

0reactions
marcalcommented, Jul 11, 2020

Hello,

Yes. In VB.NET “is” will always compare references For example
If Test1 = Test2 Then
Will use the operator While If Test1 is Test2 Then will see if the items have the same reference

Test2 = New CTst
Test1 = New CTst
If Test1 Is Test2 Then
    MessageBox.Show("Test ")
End If

Won’t print anything

And

Test2 = New CTst
Test1 = New CTst
If Test1 = Test2 Then
    MessageBox.Show("Test ")
End If

Will print Test because the items have the same value (at least with an operator) Without an “=” operator, it is a syntax error to use = between objects

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ternary operator VB vs C#: why resolves Nothing to zero?
VB. Net's Nothing is actually a closer match for C#'s default(T) expression. The ternary operator can only return one type.
Read more >
Nothing keyword - Visual Basic
Therefore, "" = Nothing is true. The following example shows comparisons that use the Is and IsNot operators: VB Copy.
Read more >
VB.net null-conditional operator doesn't honor short circuit
Try my slightly changed solution, and you will see what I mean! Public Class Program Sub Main() Dim a As List(Of Integer) =...
Read more >
Visual Basic (VB.NET) – Full Course for Beginners - YouTube
Lean the fundamentals of programming with Visual Basic (sometimes ... his YouTube channel: https://www.youtube.com/ c /ComputerScienceLessons ...
Read more >
The 10 Most Common Mistakes in C# Programming
Don't fall into one of these C# programming mistakes that even savvy developers can find problematic. Read on to see all 10.
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