kali ini saya akan menjelaskan tentang “Menghitung Bilangan memakai Function dengan menggunakan Microsoft Visual Basic 2008” dengan desain seperti dibawah ini:
Dengan koding seperti dibawah ini:
Public Class Bilangan
Dim awal, akhir, hasil As String
Private Sub btnLihatBil_Click(ByVal sender As System.Object, ByVale As System.EventArgs) Handles btnLihatBil.Click
Dim strGanjil, strGenap, strPrima, strfibonaci As Integer
prosesbilangan(strGanjil, strGenap, strPrima, strfibonaci)
showSum(txtbil1.Text, txtbil2.Text)
End Sub
Sub showSum(ByVal intNum1 As Integer, ByVal intNum2 As Integer)
Dim intSum As Integer
intSum = intNum1 + intNum2
txthasil.Text = intSum
End Sub
Sub prosesbilangan(ByVal strGanjil As String, ByVal strGenap AsString, ByVal strPrima As String, ByVal strFibonaci As String)
If cboBilangan.SelectedItem = "Ganjil" Then
lstBilangan.Items.Clear()
Ganjil()
ElseIf cboBilangan.SelectedItem = "Genap" Then
lstBilangan.Items.Clear()
Genap()
ElseIf cboBilangan.SelectedItem = "Prima" Then
lstBilangan.Items.Clear()
Prima()
ElseIf cboBilangan.SelectedItem = "Fibonaci" Then
lstBilangan.Items.Clear()
Fibonacci()
End If
End Sub
Sub Ganjil()
Dim i As Integer
awal = Val(InputBox("masukkan batasan awal", "Batasan"))
akhir = Val(InputBox("masukkan batasan akhir", "Batasan"))
For i = awal To akhir
i = i + 0
If (i Mod 2 = 1) Then
lstBilangan.Items.Add(i)
End If
Next
End Sub
Sub Genap()
Dim i As Integer
awal = Val(InputBox("masukkan batasan awal", "Batasan"))
akhir = Val(InputBox("masukkan batasan akhir", "Batasan"))
For i = awal To akhir
i = i + 0
If (i Mod 2 = 0) Then
lstBilangan.Items.Add(i)
End If
Next
End Sub
Sub Prima()
Dim x, y, z As Integer
awal = Val(InputBox("masukkan batasan awal", "Batasan"))
akhir = Val(InputBox("masukkan batasan akhir", "Batasan"))
If awal >= 2 Then
For x = awal To akhir
z = 0
For y = 2 To x
hasil = x Mod y
If hasil = 0 Then
z = z + 1
End If
Next
If z <= 1 Then
lstBilangan.Items.Add(x)
End If
Next
Else
MsgBox("Bilangan Prima dimulai dari 2")
End If
End Sub
Sub Fibonacci()
Dim i, x, y, z As Integer
akhir = Val(InputBox("masukkan batasan akhir", "Batasan"))
y = 0
z = 1
lstBilangan.Items.Add(y)
lstBilangan.Items.Add(z)
For i = 0 To akhir
x = y + z
y = z
z = x
If x > akhir Then Exit For
lstBilangan.Items.Add(x)
Next
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles txtbil1.TextChanged
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button2.Click
Dim sngTotal As Single
sngTotal = sum(txtbil1.Text, txtbil2.Text)
End Sub
Function sum(ByVal sngNum1 As Single, ByVal sngNum2 As Single) AsSingle
Dim sngHasil
sngHasil = sngNum1 + sngNum2
txthasil.Text = sngHasil
Return sngHasil
End Function
End Class
Dan outputnya seperti dibawah ini: