08 January 2015


Penggabungan 2 buah nilai string atau lebih dalam VB.NET bisa menggunakan karakter "&" atau "+". Tetapi karakter "&" lebih sering dan lebih aman digunakan untuk mencegah error saat kita ingin menggabungkan karakter string dengan angka. Untuk lebih jelas nya kita lihat beberapa contoh berikut:

Contoh Penggunaan:
1. Penggabungan nilai "Rani " dan "Irsan"
    Menggunakan "+"
  Private Sub Button1_Click(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles Button1.Click
      Try
          MsgBox("Rani " + "Irsan")
      Catch ex As Exception
          MsgBox("Error:" & ex.Message)
      End Try
  End Sub


    Menggunakan "&"
  Private Sub Button1_Click(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles Button1.Click
      Try
          MsgBox("Rani " + "Irsan")
      Catch ex As Exception
          MsgBox("Error:" & ex.Message)
      End Try
  End Sub

 

2. Menggabungkan Karakter String "Rani " dengan angka 8
    Menggunakan "+"
  Private Sub Button1_Click(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles Button1.Click
      Try
          MsgBox("Rani " + 8)
      Catch ex As Exception
          MsgBox("Error:" & ex.Message)
      End Try
  End Sub

    Menggunakan "&"
  Private Sub Button1_Click(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles Button1.Click
      Try
          MsgBox("Rani " & 8)
      Catch ex As Exception
          MsgBox("Error:" & ex.Message)
      End Try
  End Sub

3. Menggabungkan Angka 2 dan 8
    Menggunakan "+"
  Private Sub Button1_Click(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles Button1.Click
      Try
          MsgBox(2 + 8)
      Catch ex As Exception
          MsgBox("Error:" & ex.Message)
      End Try
  End Sub


    Menggunakan "&"
  Private Sub Button1_Click(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles Button1.Click
      Try
          MsgBox(
2 & 8)
      Catch ex As Exception
          MsgBox("Error:" & ex.Message)
      End Try
  End Sub



Click here if you like this article.


0 comments: