본문 바로가기

VB.net & WPF & C#

Structure Classes Example

' Structure to hold and manage a complex number
Public Structure Complex
   Public Real As Double
   Public Imag As Double   ' "Imaginary" part (coefficient of "i")
   ' Add another complex number to this one:
   Public Function Plus(ByVal Operand As Complex) As Complex
      Plus.Real = Operand.Real + Real
      Plus.Imag = Operand.Imag + Imag
   End Function
   ' Multiply this complex number by another one:
   Public Function Times(ByVal Operand As Complex) As Complex
      Times.Real = (Operand.Real * Real) - (Operand.Imag * Imag)
      Times.Imag = (Operand.Imag * Real) + (Operand.Real * Imag)
   End Function
   ' Invert this complex number:
   Public Function Reciprocal() As Complex
      Dim Denominator As Double = (Real * Real) + (Imag * Imag)
      If Denominator = 0 Then Throw New System.DivideByZeroException()
      Reciprocal.Real = Real / Denominator
      Reciprocal.Imag = -Imag / Denominator
   End Function
End Structure   ' Complex


' Classes to hold data for various types of employees
Public Class Employee
   Public GivenName As String
   Public FamilyName As String
   Public Supervisor As Manager
   Public PhoneNumber As String   ' Allows "-", ".", "x" in number
End Class   ' Employee
' Manager of one or more other employees:
Public Class Manager
   Inherits Employee
   Public DirectReports() As Employee
End Class   ' Manager
' Temporary employee engaged for a limited time period:
Public Class Temporary
   Inherits Employee
   Public LastWorkDate As Date
End Class   ' Temporary
' Temporary employee contracted from an employment agency:
Public Class Contractor
   Inherits Temporary
   Public AgencyName As String
   Public AgencyPhoneNumber As String
End Class   ' Contractor