Sunday, October 11, 2009

Textbox validation for numeric values


A variable that is declared as numeric ( integer, decimal, ….etc.) can only contain numbers 0 through 9. For example look at the following code:
Dim intTest1 As Decimal

Move Form objects into variables
intTest1 = txtTestScore.Text

Program Crash
If the user were to enter any characters other than 0 through 9 in the txtTestScore text box or if left empty (in which case the text box contains blanks), the assignment statement would fail as the program is trying to push invalid characters into the decimal variable.

Validation
Two type of validation for textboxes where the user would enter numeric values.
1. Check if the entered value is numeric
2. Sometimes, you would want to perform a range checkor check for positive numbers. For example, a test score should be between 0 and 100.

In the following code snippet, note where the contents of the textbox is being moved into the variable – after validating and making sure the contents of the textbox is numeric.

If NOT ISNUMERIC( txtTestScore.text) then
Msgbox(“Enter a valid numeric value”, , “Input error”)

Else

‘Move Form objects into variables
intTest1 = txtTestScore.Text

If intTest1 is less than (<) 0 AND intTest1 gtreater than (>) 0 Then

Msgbox(“Enter a number between 0 and 100”, , “input error’)

Endif
Endif

How will you validate a positive number?

Thursday, October 8, 2009

Validation


Need for validation: Simple, Garbage In- Garbage Out.

Where will you validate? Typically, the calculate button click event should have the following 3 steps.
1. Move form objects into variables
2. Processing or calculation
3. Move contents of output variable into form objects


Knowing that the computer executes code sequentially, where would you want to place the code that validates what the user keys in.
1.After you display the output to the user?
2.After Processing but before display the output to the user?
3.Before you start processing?

Thoughts? Comments?

Code Structure - Must Read


The following is a code snippet from the click event of the calculate button (Assignment 5-4). Please make sure your code has the following 3 steps. Note, The computer executes code sequentially, so these steps must be in the order specified.


1. Move form objects into variables ( otherwise your variables will contain nothing)

2. Processing (All of the calculations)

3. Move the output variable into form object. ( otherwise the labels will display nothing)


Const cIntSingle As Integer = 38I
Const cIntFamily As Integer = 58I
Const cintAncient As Integer = 27I
Dim intMonths As Integer
Dim decTotal As Decimal

'Move inputs from form into variables
intMonths = txtMonths.Text


'Processing

If Me.radSingle.Checked Then
decTotal = intSingle * intMonths
ElseIf Me.radFamily.Checked Then
decTotal = intFamily * intMonths
ElseIf Me.radAncient.Checked Then
decTotal = intAncient * intMonths
End If


'Move output to form objects
lblTotal.Text = decTotal.ToString("C")

Wednesday, September 30, 2009

Variables - M&M Perspective


There is a big bowl of M&M candies. There are red, yellow, green and blue M&Ms all mixed in the big bowl. If your job is to sort and separate and group the colored candies, how will you go about solving the problem?



Here is how most programmers would attempt to solve the problem :)

Get 4 containers and label them yellow, red, blue and green.
Pick each M&M from the big bowl

If Blue, put it in the “Blue” container
If Yellow, put it in the “yellow” container
If Red, put it in the “Red” container
If Green, put it in the “Green” container

Repeat from step 2 until big bowl is empty

Ignore the decision structures and the iterative process in this example, but instead focus on the 4 containers and their labels. The containers were brought in to hold M&Ms and also each container should contain only a specific colored M&M based on the label.

Similarly,

Variables help hold intermediate results, especially in a multi-step complex calculation.

The content of the variable depends on its data type. ( String, Numeric, Boolean, Date, etc)

The data type of a variable determines what type of calculations may be performed, arithmetic or string, etc.


Using Form Objects - for Input and Output

  1. Typically VB objects are placed on a form and are used for communication between the program and the user. Text Boxes, Radio Buttons, Labels, Buttons are some of the commonly used objects.

  2. The objects allow the user to enter the “input” and the objects also facilitate “output” to be displayed to the user.

  3. In a program designed to calculate the average of 4 test scores, the input – which are the 4 test scores are entered via 4 text boxes, the output – the average test score is displayed back to the user via a label.

  4. Textboxes and Labels are of string data types. Any information entered into these objects are treated as strings.

Event -Driven Programming





  1. Visual Basic is an event driven programming language. An event has to be triggered for the code to be executed. The most common type of an event is clicking on a button. (Click Button Event)


  2. Most of you should be familiar with the Submit, Calculate, Cancel and the Exit buttons. These buttons are so common on the web that you hardly notice them anymore.


  3. As a programmer, you will be embedding code under these button’s events. For example in a form designed to calculate the average of 4 test scores, the logic written to calculate the average test score will be embedded under the “click” event subroutine of the “submit” button”