Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleMake a ComplexNumber class in VB .NET
DescriptionThis example shows how to make a ComplexNumber class in VB .NET. It uses private m_RealPart and m_ImaginaryPart variables to represent the number. It provides methods to add, subtract, negate, and multiply ComplexNumbers.
Keywordscomplex, complex number
CategoriesAlgorithms
 
The class uses the following code, most of which is straightforward.
 
Public Class ComplexNumber
    Private m_RealPart As Single
    Private m_ImaginaryPart As Single

    ' Property procedures.
    Public Property RealPart() As Single
        Get
            Return m_RealPart
        End Get
        Set(ByVal Value As Single)
            m_RealPart = Value
        End Set
    End Property

    Public Property ImaginaryPart() As Single
        Get
            Return m_ImaginaryPart
        End Get
        Set(ByVal Value As Single)
            m_ImaginaryPart = Value
        End Set
    End Property

    ' Constructor using real and imaginary parts.
    Public Sub New(Optional ByVal real_part As Single = _
        0.0, Optional ByVal imaginary_part As Single = 0.0)
        m_RealPart = real_part
        m_ImaginaryPart = imaginary_part
    End Sub

    ' Constructor using a String of the form R + Ii.
    Public Sub New(ByVal string_value As String)
        Dim pos As Integer

        ' Remove spaces and the "i".
        string_value = string_value.Replace(" "c, "")
        string_value = string_value.ToLower.Replace("i"c, _
            "")

        ' Find the + or - between the parts.
        Dim plus_or_minus() As Char = {"+"c, "-"c}
        pos = string_value.IndexOfAny(plus_or_minus)
        If pos = 0 Then
            ' Skip the leading +/-.
            pos = string_value.IndexOfAny(plus_or_minus, 1)
        End If

        ' Get the real and imaginary parts.
        m_RealPart = Single.Parse(string_value.Substring(0, _
            pos))
        m_ImaginaryPart = _
            Single.Parse(string_value.Substring(pos))
    End Sub

    ' Return -Me.
    Public Function Negate() As ComplexNumber
        Return New ComplexNumber( _
            -m_RealPart, _
            -m_ImaginaryPart)
    End Function

    ' Return Me + complex_number.
    Public Function Plus(ByVal complex_number As _
        ComplexNumber) As ComplexNumber
        Return New ComplexNumber( _
            m_RealPart + complex_number.m_RealPart, _
            m_ImaginaryPart + _
                complex_number.m_ImaginaryPart)
    End Function

    ' Return Me - complex_number.
    Public Function Minus(ByVal complex_number As _
        ComplexNumber) As ComplexNumber
        Return Me.Plus(complex_number.Negate())
    End Function

    ' Return Me * complex_number.
    Public Function Times(ByVal complex_number As _
        ComplexNumber) As ComplexNumber
        Dim re_part As Single = _
            m_RealPart * complex_number.RealPart - _
            m_ImaginaryPart * complex_number.ImaginaryPart
        Dim im_part As Single = _
            m_RealPart * complex_number.ImaginaryPart + _
            m_ImaginaryPart * complex_number.RealPart

        Return New ComplexNumber(re_part, im_part)
    End Function

    ' Return a string representation of this number.
    Public Overrides Function ToString() As String
        ' Start with the real part.
        Dim result As String = m_RealPart.ToString()

        ' Add the imaginary part.
        If m_ImaginaryPart >= 0 Then
            result &= " + " & _
                m_ImaginaryPart.ToString() & _
                "i"
        Else
            result &= " - " & _
                (-m_ImaginaryPart).ToString() & _
                "i"
        End If

        Return result
    End Function
End Class
 
For more information on algorithms in Visual Basic, see my book Ready-to-Run Visual Basic Algorithms.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated