Are common operator errors resolved in .NET 4?

I assume no, but I can’t find strong evidence on Google to support this assumption. Using the keywords "vb.net", the general operator overload "gives exactly 1 result, and removing the" overload "gives more, but does not have a direct statement on the problem.

My thinking is given an abstract class, it would be great to be able to implement a common operator overload, which a derived class can use in this case, when the operator overload mentioned should return a new copy of the derived class, but the code for each overload is the same. If that makes sense.

This applies to my previous questions about my custom class Enum and overloaded Bitwise Operators ( And, Or, Notand Xor), but this particular thought was caused by mere curiosity, "Can it be done?".

Here is which of my user enumerations basically looks:
Parent, EBasenothing special, just hosting shared Nameand Valueproperties, plus two shared operators, op_Equalityand op_Inequality.

Friend NotInheritable Class EExample
    Inherits EBase

    Private Sub New()
    End Sub

    Friend Shared Function GetValue(ByVal Name As String) As Enums
        Dim tmpOffset As Int32 = Array.IndexOf(_Names, Name)
        Return If(HasContent(Name), If(tmpOffset <> -1, Values(tmpOffset), Nothing), Nothing)
    End Function


    ' Num of Enums defined.
    Friend Shared ReadOnly MaxEnums As Int32 = 5

    ' String literals.
    Private Shared ReadOnly _Names As String() = New String() _
        {"one_adam", "two_boy", "three_charles", "four_david", "five_edward"}

    ' Enums.
    Friend Shared ReadOnly OneA As New Enums(_Names(0), 1)
    Friend Shared ReadOnly TwoB As New Enums(_Names(1), 2)
    Friend Shared ReadOnly ThreeC As New Enums(_Names(2), 4)
    Friend Shared ReadOnly FourD As New Enums(_Names(3), 8)
    Friend Shared ReadOnly FiveE As New Enums(_Names(4), 16)


    ' Enum Values Array.
    Friend Shared ReadOnly Values As Enums() = New Enums() _
        {OneA, TwoB, ThreeC, FourD, FiveE}


    Friend NotInheritable Class Enums
        Inherits EBase

        Private Sub New()
        End Sub

        Friend Sub New(ByVal Name As String, ByVal Value As Int32)
            MyBase.Name = Name
            MyBase.Value = Value
        End Sub
    End Class
End Class

Here's how things are used:

Dim Foo As EExample.Enums
Foo = EExample.TwoB
Debug.Print(Foo.Name)

will print two_boy

Now, considering this, if I want to do the following:

Dim Foo as EExample.Enums
Foo = EExample.OneA Or EExample.FiveE

I need to define operator overload for Or inside definition EExample.Enums. How will this statement reload?

Public Shared Operator Or(ByVal lhOp As Enums, ByVal rhOp As Enums) As Enums
    Return New Enums(String.Concat(lhOp.Name, "|"c, rhOp.Name),
                     lhOp.Value Or rhOp.Value, True)
End Operator

EExample.Enums, Bitwise-Or'ed Value EExample . Name , - .

, 20 enum, EExample. , IDE . IL :

.method public specialname static class MyAssembly.EExample/Enums 
        op_BitwiseOr(class MyAssembly.EExample/Enums lhOp,
                     class MyAssembly.EExample/Enums rhOp) cil managed
{ ... }

! , EBase!

Friend Interface IEnums
    Property Name As String
    Property Value As Int32
End Interface

Public Shared Operator Or(Of T As IEnums)(ByVal lhOp As T, ByVal rhOp As T) As T
    Return New T(String.Concat(lhOp.Name, "|"c, rhOp.Name),
                 lhOp.Value Or rhOp.Value, True)
End Operator

( ) EExample.OneA Or EExample.FiveE , , EBase, , EExample.Enums IEnums T.

. , ? StackOverflow? Spice?

PS: , Return New T( ... ) , , .

+3
2

, , . 9.8 :

, .

, , , 9.2.1.

+4

"" .

EBase (IEnumBase) Friend, EBase :

Protected Shared Function _
op_BitwiseOr(Of T As {IEnumBase, Class})(ByVal lhOp As T, ByVal rhOp As T, ByVal RetEnum As T) As T
    RetEnum.Name = String.Concat(lhOp.Name, "|"c, rhOp.Name)
    RetEnum.Value = (lhOp.Value Or rhOp.Value)

    Return RetEnum
End Function

, RetEnum . Enums (.. EExample) :

Public Shared Shadows Operator Or(ByVal lhOp As Enums, ByVal rhOp As Enums) As Enums
    Return EBase.op_BitwiseOr(lhOp, rhOp, New Enums)
End Operator

, EBase, . generics Enums!

, . , . , .

, . , , MSDN, , .
+2

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


All Articles