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”