Tampilkan postingan dengan label vb.net. Tampilkan semua postingan
Tampilkan postingan dengan label vb.net. Tampilkan semua postingan

Your Complex Number Class

Diposting oleh Source Code Program | 05.20 | | 0 komentar »

Your Complex Number Class
Imports System

Public Class MainClass

Shared Sub Main(ByVal args As String())
Dim X, Y As Complex
X = New Complex(1, 2)
Y = New Complex(3, 4)

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 Complex) As 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 Complex) As Complex
Return New Complex( _
c1.realPart + c2.realPart, _
c1.imaginaryPart + c2.imaginaryPart)
End Operator
Public Shared Operator -(ByVal c1 As Complex, ByVal c2 As Complex) As Complex
Return New Complex( _
c1.realPart - c2.realPart, _
c1.imaginaryPart - c2.imaginaryPart)
End Operator
Public Shared Operator =(ByVal c1 As Complex, ByVal c2 As Complex) As Boolean
Return (c1.realPart = c2.realPart) AndAlso (c1.imaginaryPart = c2.imaginaryPart)
End Operator
Public Shared Operator <>(ByVal c1 As Complex, ByVal c2 As Complex) As Boolean
Return (c1.realPart <> c2.realPart) OrElse (c1.imaginaryPart <> c2.imaginaryPart)
End Operator
Public Shared Operator -(ByVal c1 As Complex) As Complex
Return New Complex(c1.imaginaryPart, c1.realPart)
End Operator
Public Shared Narrowing Operator CType(ByVal c1 As Complex) As Double
Return System.Math.Sqrt(c1.realPart * c1.realPart + c1.imaginaryPart * c1.imaginaryPart)
End Operator
End Class

Nested Class Demo

Diposting oleh Source Code Program | 05.10 | | 0 komentar »

Nested Class Demo
Public Class Fraction
Private numerator As Integer
Private denominator As Integer

Public Sub New( _
ByVal numerator As Integer, ByVal denominator As Integer)
Me.numerator = numerator
Me.denominator = denominator
End Sub 'New

Public Overrides Function ToString( ) As String
Return [String].Format("{0}/{1}", numerator, denominator)
End Function 'ToString

' Nested Class
Class FractionArtist
Public Sub Draw(ByVal f As Fraction)
Console.WriteLine("Drawing the numerator: {0}", f.numerator)
Console.WriteLine( _
"Drawing the denominator: {0}", f.denominator)
End Sub 'Draw
End Class 'FractionArtist

End Class 'Fraction

Select Case Err Number

Diposting oleh Source Code Program | 16.25 | | 0 komentar »

Imports System.IO

public class Tester
public Shared Sub Main

On Error GoTo ErrorHandle
Exit Sub

ErrorHandle:
Select Case Err.Number
Case 52
Console.WriteLine("File Name Exception")
Case 53
Console.WriteLine("File cannot be found")
Case 54
Console.WriteLine("File Mode Exception")
Case 55
Console.WriteLine("File is aleady open")
Case 58
Console.WriteLine("File exists")
Case 68
Console.WriteLine("Device is not available")
Case 75
Console.WriteLine("Path Exception")
Case 76
Console.WriteLine("Path cannot be found")
Case Else
Console.WriteLine("Other")
End Select

End Sub

End class

On Error GoTo errorhandle

Diposting oleh Source Code Program | 16.19 | | 0 komentar »

Imports System.IO

public class Tester
public Shared Sub Main

Dim myfile As System.IO.File
Dim w As System.IO.StreamWriter
On Error GoTo errorhandle

w = myfile.AppendText("a:\temp.txt")
w.Write("a")
w.Close()
Exit Sub

errorhandle:
Console.WriteLine("Please Insert")
Exit Sub
Resume
End Sub

End class