VB.Net Tutorial/Operator/Operator overload — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 16:40, 26 мая 2010

Operator overload

Public Structure Diamond
   Private weight As Single
   Private price As Decimal
   Private fWeight As Boolean
   Public Sub New(oz As Single, pr As Decimal)
      weight = oz
      price = pr
      fWeight = True
   End Sub
   Public Property ByWeight() As Boolean
      Get
         Return fWeight
      End Get
      Set
         fWeight = Value
      End Set
   End Property
   Public ReadOnly Property Size() As Single
      Get
         Return weight
      End Get
   End Property
   Public Shared Operator > (operand1 As Diamond, operand2 As Diamond) As Boolean
      If operand1.Price / operand1.Size > operand2.Price / operand2.Size Then
         Return True
      Else
         Return False
      End If
   End Operator
   Public Shared Operator < (operand1 As Diamond, operand2 As Diamond) As Boolean
      If operand1.Price / operand1.Size < operand2.Price / operand2.Size Then
         Return True
      Else
         Return False
      End If
   End Operator
   Public Shared Operator Or (op1 As Diamond, op2 As Diamond) As Diamond
     If op1.ByWeight And op2.ByWeight
        If op1 < op2 Then
           Return op1
        Else
           Return op2
        End If
     Else
        If op1.ByWeight Then
           Return op1
        ElseIf op2.ByWeight Then
           Return op2
        Else
           Return Nothing
        End If
     End If
   End Operator
   Public Shared Operator IsTrue(Byval op1 As Diamond) As Boolean
      If op1.ByWeight Then
         Return True
      Else
         Return False
      End If
   End Operator
   Public Shared Operator IsFalse(ByVal op1 As Diamond) As Boolean
      If op1.ByWeight Then
         Return False
      Else
         Return True
      End If
   End Operator
End Structure
Public Module modTest
   Public Sub Main()
      Dim a As Diamond = New Diamond(1, .3d)
      Dim b As Diamond = New Diamond(2, .5d)
      a.ByWeight = False
      b.ByWeight = True
      If a Or b Then Console.WRiteLine(a.Size)
      If a OrElse b Then Console.WriteLine(a.Size)
      Console.WriteLine(b < a)
   End Sub
End Module
1
1
True