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.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.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
w__ww_.java__2___s_._c__o___m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.