Thursday, July 12, 2012

Sample Program #1
















Let us create a program that will calculate the sum of two integers using the val(), str() and trim() function.


'LONG METHOD STYLE CODE
Private Sub cmdcal_Click()
  Dim a, b, c As Integer

  a = Val(txtn1.Text)
  b = Val(txtn2.Text)
  c = a + b
  txtsum.Text = Trim(Str(c))
End Sub


'SHORT METHOD STYLE CODE
Private Sub cmdcal_Click()
  txtsum.Text = Trim(Str(Val(txtn1.Text) + Val(txtn2.Text)))
End Sub

Discussion:

The val() function will convert any string type value to a numeric value while the str() on the other hand will convert the numeric value to string type. During the conversion of numeric data type to string, a space is added thus trim() function will remove the spaces