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

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. Stream File
13. GUI
14. GUI Applications
15. 2D Graphics
16. I18N Internationlization
17. Reflection
18. Regular Expressions
19. Security
20. Socket Network
21. Thread
22. Windows
23. XML
24. Database ADO.net
25. Design Patterns
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
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
w___w___w__.j_a__va__2___s.c_o__m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.