Your Complex Number Class : Class Define « 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 » Class DefineScreenshots 
Your Complex Number Class
Your Complex Number Class

There is a minor bug though. The unary minus operator is incorrect.

    Public Shared Operator -(ByVal c1 As ComplexAs Complex
        Return New Complex(c1.imaginaryPart, c1.realPart)
    End Operator

This should be replaced with
    Public Shared Operator -(ByVal c1 As ComplexAs Complex
        Return New Complex(-c1.realPart, -c1.imaginaryPart)
    End Operator

and perhaps an additional function like
    Public function Conjungate() As Complex
        Return New Complex(realPart, -imaginaryPart)
    End Function

And you might consider adding a .Length function equal to the current narrowing.
(And mathematically preferable replacing it, since converting complex->real isn't well-defined)

Best regards
Eske Rahn
rahn at sol.dk       
       
       
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
1.Nested Class DemoNested Class Demo
2.Class CompositionClass Composition
3.Define and use your own Time ClassDefine and use your own Time Class
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.