Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking in C# a VB.NET object for null gives unexpected compile error

Checking in C# a VB.NET object for null gives unexpected compile error:

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

Resharper's suggested fix:

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

I have a colleague that have done this in a year without any problems. My colleague is using Visual Studio 2012 and I'm using Visual Studio 2013. Could it be some kind of a settings?

Why is basePackage != null an object?

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

UPDATE: BasePackage's inherited this from another class: I don't know if that's helps in any ways.

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 outcomment these two operators C# is working 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 in VB.NET. No need for C# cast then.

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
like image 340
radbyx Avatar asked Mar 02 '15 10:03

radbyx


1 Answers

I took your vb sample, combpiled it into a dll and decompiled it to c# That's how 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, it is just due to the strange operator overload signature.

You commented "Not Equal" operators, now it seems to work, but you will get the same error when you write something like

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

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

like image 52
Viktor Arsanov Avatar answered Oct 16 '22 19:10

Viktor Arsanov