VB -> C#: Problem with Operator and Nothing
See original GitHub issueHello, 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:
- Created 3 years ago
- Comments:7 (4 by maintainers)
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.
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
Won’t print anything
And
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