Using the overloaded VB.NET Not statement from C #

I have a VB class that overloads the Not operator; this is not like using C # applications.

 Public Shared Operator Not(item As MyClass) As Boolean Return False End Operator 

I can use this in VB.NET:

 If Not MyClassInstance Then ' Do something End If 

I am trying to use this in a C # application, but it will not be created.

 if (!MyClassInstance) { // do something } 

I get an error

The operator '!' cannot be applied to an operand of type MyClass

Can someone tell me what I am missing?

+7
c # operator-overloading
source share
1 answer

The Not operator in VB.NET is a bitwise operator; it creates one addition to its operand. It does not have the equivalent of a C # operator ! , logical operator. You must use the equivalent bitwise operator in C # to use the overload of the VB.NET operator:

 if(~MyClassInstance) { // do something } 

You can write a function in VB.NET that will map to the C # logical operator. It should look like this:

 <System.Runtime.CompilerServices.SpecialName> _ Public Shared Function op_LogicalNot(item As MyClass) As Boolean Return False End Function 
+15
source share

All Articles