Checking for C # VB.NET object for null gives unexpected compilation error

Checking the C # VB.NET object for null gives an unexpected compilation error:

// Cannot compile: var basePackage = BasePackage.GetFromID(id); // GetFromID is from VB.NET if (basePackage != null) // Errormesage: "Cannot implicitly convert 'object' to 'bool' { } 

Recommended Resharper Solution:

 // Can compile if ((bool) (basePackage != null)) { linkedGroups = basePackage.GetLinkedGroups(); } 

I have a colleague who did this a year later without any problems. My colleague uses Visual Studio 2012, and I use Visual Studio 2013. Could it be some kind of settings?

Why basePackage != null a object ?

I know that VB.NET has Nothing , where C # has null .

UPDATE: BasePackage inherited this from another class: I don't know if this helps anyway.

 Public Shared Operator =([object] As CMSObject, type As System.Type) Return [object].GetType Is type End Operator Public Shared Operator <>([object] As CMSObject, type As System.Type) Return [object].GetType IsNot type End Operator Public Shared Operator =([object] As CMSObject, o As Object) Return [object].GetType Is o End Operator Public Shared Operator <>([object] As CMSObject, o As Object) Return [object].GetType IsNot o End Operator 

SOLUTION: When I exit these two statements, C # works fine again.

 Public Shared Operator =([object] As CMSObject, type As System.Type) Return [object].GetType Is type End Operator 'Public Shared Operator <>([object] As CMSObject, type As System.Type) ' Return [object].GetType IsNot type 'End Operator Public Shared Operator =([object] As CMSObject, o As Object) Return [object].GetType Is o End Operator 'Public Shared Operator <>([object] As CMSObject, o As Object) ' Return [object].GetType IsNot o 'End Operator 

Final Solution Added Type to VB.NET. Then there is no need for C #.

 Public Shared Operator =([object] As CMSObject, type As System.Type) **As Boolean** Return [object].GetType Is type End Operator Public Shared Operator <>([object] As CMSObject, type As System.Type) **As Boolean** Return [object].GetType IsNot type End Operator Public Shared Operator =([object] As CMSObject, o As Object) **As Boolean** Return [object].GetType Is o End Operator Public Shared Operator <>([object] As CMSObject, o As Object) **As Boolean** Return [object].GetType IsNot o End Operator 
+6
source share
1 answer

I took your vb sample, compiled it in dll and decompiled it in C #. The way you operators look

 public static object operator ==(Class1 @object, Type type) { return (object) (bool) (@object.GetType() == type ? 1 : 0); } public static object operator !=(Class1 @object, Type type) { return (object) (bool) (@object.GetType() != type ? 1 : 0); } public static object operator ==(Class1 @object, object o) { return (object) (bool) (@object.GetType() == o ? 1 : 0); } public static object operator !=(Class1 @object, object o) { return (object) (bool) (@object.GetType() != o ? 1 : 0); } 

So this is just because of the weird signature of the operator overload.

You commented on the Not Equal statements, now it works, but you get the same error when you write something like

 if ( (basePackage == null)) // etc. 

The solution would be, as suggested in the comments, to specify your operator overload signature as Boolean.

+2
source

Source: https://habr.com/ru/post/983212/


All Articles