Monday, August 16, 2010

Monday, August 16th 2010

Looking at Anne Boehms tutorials, we're going to take a look back at sub procedures. Sub procedures have four parts, the access modifier, the sub keyword, the name of the procedure, and the parameter list.

The access modifier states whether the procedure can be called from other classes. You can set the access modifier to be either public or private.


Private Function FutureValue(ByVal monthlyInvestment As Decimal, _
ByVal monthlyInterestRate As Decimal, ByVal months As Integer) _
As Decimal
For i As Integer = 1 To months
FutureValue = (FutureValue + monthlyInvestment) _
* (1 + monthlyInterestRate)
Next
Return FutureValue
End Function


Ms. Boehm reminds us that every function procedure should return a value.

The next tutorial is about data validation, meaning making sure that the end user is entering the correct type of data. One way to do this is by using the IsNumeric function. The example Ms. Boehm uses is



If not IsNumeric(txtSubtotal.text) Then
MessageBox.Show( _
"Please enter a valid number for the Subtotal field.", _
"Entry Error")
Exit Sub
End If


What sub procedure would we be exiting in this case? It doesn't seem quite clear to me.

Here is an example for a dialog box in response to a program error:



MessageBox.Show( _
"An exception has occurred. " & ControlChars.NewLine _
& "The program will be canceled." _
"Program Error")


Since checking text boxes for valid data is a regular occurrence, it makes sense to create sub procedures and functions to handle them so that you don't have to keep reinventing the wheel.

For instance:



Private Function IsPresent(ByVal textBox as TextBox, ByVal name as String) _
As Boolean
If textBox.Text = "" Then
MessageBox.Show(Name & " Is a required field.", "Entry Error")
textBox.Select()
Return False
Else
Return True
End If
End Function


This function returns a Boolean value that indicates whether the data was valid or invalid via Return true and Return false

No comments: