Operator overload : Operator overload « Operator « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Operator » Operator overload 
3.5.1.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 DiamondAs 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 DiamondAs 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 DiamondAs 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 DiamondAs Boolean
      If op1.ByWeight Then
         Return True
      Else
         Return False
      End If
   End Operator

   Public Shared Operator IsFalse(ByVal op1 As DiamondAs Boolean
      If op1.ByWeight Then
         Return False
      Else
         Return True
      End If
   End Operator
End Structure

Public Module modTest
   Public Sub Main()
      Dim As Diamond = New Diamond(1.3d)
      Dim 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
3.5.Operator overload
3.5.1.Operator overload
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.