Operator Overload Demo : Operator Overload « Class « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Development
8.Event
9.File Directory
10.Generics
11.GUI
12.Language Basics
13.LINQ
14.Network Remote
15.Security
16.Thread
17.Windows Presentation Foundation
18.Windows System
19.XML
20.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » Class » Operator OverloadScreenshots 
Operator Overload Demo
Operator Overload Demo

Imports System

Public Class MainClass

    Shared Sub Main(ByVal args As String())
        Dim X, Y As Complex
        X = New Complex(12)
        Y = New Complex(34)

        Console.WriteLine( (X + Y).ToString )
        Console.WriteLine( (X - Y).ToString )
        Console.WriteLine( (X * Y).ToString )
        Console.WriteLine( (X = Y).ToString )
        Console.WriteLine( (X <> Y).ToString )
        Console.WriteLine( (-X).ToString )
        Dim abs_x As Double = CDbl(X)
        Console.WriteLine(  abs_x.ToString )
    End Sub

End Class

Public Class Complex
    Public realPart As Double
    Public imaginaryPart As Double

    ' Constructors.
    Public Sub New()
    End Sub
    Public Sub New(ByVal real_part As Double, ByVal imaginary_part As Double)
        realPart = real_part
        imaginaryPart = imaginary_part
    End Sub

    ' ToString.
    Public Overrides Function ToString() As String
        Return realPart.ToString & " + " & imaginaryPart.ToString & "i"
    End Function

    ' Operators.
    Public Shared Operator *(ByVal c1 As Complex, ByVal c2 As ComplexAs Complex
        Return New Complex_
            c1.realPart * c2.realPart - c1.imaginaryPart * c2.imaginaryPart, _
            c1.realPart * c2.imaginaryPart + c1.imaginaryPart * c2.realPart)
    End Operator
    Public Shared Operator +(ByVal c1 As Complex, ByVal c2 As ComplexAs Complex
        Return New Complex_
            c1.realPart + c2.realPart, _
            c1.imaginaryPart + c2.imaginaryPart)
    End Operator
    Public Shared Operator -(ByVal c1 As Complex, ByVal c2 As ComplexAs Complex
        Return New Complex_
            c1.realPart - c2.realPart, _
            c1.imaginaryPart - c2.imaginaryPart)
    End Operator
    Public Shared Operator =(ByVal c1 As Complex, ByVal c2 As ComplexAs Boolean
        Return (c1.realPart = c2.realPartAndAlso (c1.imaginaryPart = c2.imaginaryPart)
    End Operator
    Public Shared Operator <>(ByVal c1 As Complex, ByVal c2 As ComplexAs Boolean
        Return (c1.realPart <> c2.realPartOrElse (c1.imaginaryPart <> c2.imaginaryPart)
    End Operator
    Public Shared Operator -(ByVal c1 As ComplexAs Complex
        Return New Complex(c1.imaginaryPart, c1.realPart)
    End Operator
    Public Shared Narrowing Operator CType(ByVal c1 As ComplexAs Double
        Return System.Math.Sqrt(c1.realPart * c1.realPart + c1.imaginaryPart * c1.imaginaryPart)
    End Operator
End Class

           
       
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.