Jumat, 25 Maret 2016

clas variable dan exceptionhandler : pertemuan 4

Share it Please
 1. Class Level Variable
Class level variable adalah variable yang dapat dibaca disemua Sub (function). Contoh nya dibawah ini, variable intValue dapat di-access dari sub btnSetValue_Click dan btnShowValueClick.



2. Execption Handler
1. Buat App seperti dibawah ini.
2. Double click button OK

source code nya 
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
        Dim decSalary As Decimal
        Try
            decSalary = CDec(txtsalary.Text)
            lblresult.Text = " Your salary " & decSalary.ToString("C")
        Catch
            lblresult.Text = "please try again"
        End Try
    End Sub
    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub
End Class

Cara menghandle error :
1. Gunakan Try-Catch 
logika nya seperti ini




tutorial :v
buat seperti gambar berikut ini pada vb kalian
 Klik 2 kali button Calculate, masukan code ini

Public Class Form2
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtannualsalary.TextChanged
    End Sub
    Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalculate.Click
        Dim decAnnualSary As Decimal 'Annual salary
        Dim intPayPeriods As Integer 'Number of pay periods
        Dim decSalary As Decimal     'SALARY PER PAY PERIOD
        'get the annual salary and number of pay periode.
        decAnnualSary = CDec(txtannualsalary.Text)
        intPayPeriods = CInt(txtpayperiods.Text)
        'Calculate the salary per pay period.
        decSalary = decAnnualSary / intPayPeriods
        'display the salary per pay period.
        lblsalary.Text = decSalary.ToString("c")
    End Sub
End Class

 Jika kita masukan angka pada textbox annualSalary dan payPeriod, maka aplikasi akan
berjalan dengan baik.
 Sekrang kita coba masukan String kosong “” pada payPeriode, maka akan muncul Error.




 Menghandle error nya dengan menggunakan Try-Catch

 source nya
Public Class Form2
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtannualsalary.TextChanged
    End Sub
    Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalculate.Click
        Dim decAnnualSary As Decimal 'Annual salary
        Dim intPayPeriods As Integer 'Number of pay periods
        Dim decSalary As Decimal     'SALARY PER PAY PERIOD
        lblstatus.Text = String.Empty
        Try
            'get the annual salary and number of pay periode.
            decAnnualSary = CDec(txtannualsalary.Text)
            intPayPeriods = CInt(txtpayperiods.Text)
            'Calculate the salary per pay period.
            decSalary = decAnnualSary / intPayPeriods
            'display the salary per pay period.
            lblsalary.Text = decSalary.ToString("c")
        Catch
            'Display an error message.
            lblstatus.Text = "Error: Be sure to enter nonzero " & "numeric values."
        End Try
    End Sub
End Class


 Menampilkan Spesific Error Message
1. Kita bisa menampilkan Error berdasarkan  error nya. Contohnya seperti ini
source code nya 
Public Class Form2
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtannualsalary.TextChanged
    End Sub
    Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalculate.Click
        Dim decAnnualSary As Decimal 'Annual salary
        Dim intPayPeriods As Integer 'Number of pay periods
        Dim decSalary As Decimal     'SALARY PER PAY PERIOD
        lblStatus.Text = String.Empty
        Try
            'get the annual salary and number of pay periode.
            decAnnualSary = CDec(txtAnnualSalary.Text)
            Try
                intPayPeriods = CInt(txtPayPeriods.Text)
                Try
                    'Calculate the salary per pay period.
                    decSalary = decAnnualSary / intPayPeriods
                    'display the salary per pay period.
                    lblSalary.Text = decSalary.ToString("c")
                Catch
                    'Display an error message.
                    lblStatus.Text = "Error: Be sure to enter nonzero " & "numeric values."
                End Try
            Catch
                'error message for invalid pay periods
                lblStatus.Text = "pay periods must be an integer."
            End Try
        Catch
            'error message for invalid salary.
            lblStatus.Text = "enter a numeric value for salary."
        End Try
    End Sub
End Class


Tidak ada komentar:

Posting Komentar

kalo bermanfaat tolong di share bro :D :D

klik blok

Blogroll

About