Monday, April 18, 2011

Chapter 8 Summary

8.1 Arrays
-Arrays are "like groups of variables with one name". They are stored within an array and are called by using a subscript (known as an index).

The basic format for an array is: Dim ArrayName (UpperSubscript) As Datatype
-As you can see, it's very similar to the declaration of your simple variable, but notice the parantheses for the upper subscript. The subscript is used to declare how many values are stored in the array. The subscript starts at an index of 0, just like a counter in a loop, so keep that in mind.

Example: Dim IntegerArray (10) As Integer will declare an array that can hold 11 values (0-10).

-Arrays work well within loops to store a large number of values into an array quickly and easily without having to write out individual statements.

Example:
Dim IntegerArray (10) As Integer
Dim intCount as Integer

For intCount=0 to IntegerArray
      IntegerArray(intCount)=100
Next

This will put the value of 100 into every value position index in the array. You can also use input boxes to store values in an array.

-Arrays can throw exceptions when an array is called using an index that is "out of range". Make sure to double check code during design time.

-To get the length of an array, make sure to remember that the "length" function is set at a range starting at 1, not 0 like an array. Make sure to use arrayName.length-1 to set the correct range.

-You can use array values just like variables, just make sure you're calling the right value you're looking for.
Example: IntegerArray(2) *50. <---This take the 3rd index value in the array and multiplies it by 50.

For Each Loop
-Special loop to simplify array processing when you need to retrieve the value of each element. This loop cannot modify array values, just read them.
For Each var As type In array
   statements
Next

var-name of variable that will be created for use with the loop.
type-data type of the array
array-name of the array.

-These loops will iterate once for every value in the array. Each loop it'll store a copy of the array elements into the variable you just created.

Book Example: Dim intArray() As Integer={10,20,30,40,50,60}
For Each intVal As Integer In intArray
    lstShow.Items.Add(intVal)
Next

What this does is declare the "intVal" variable that will store the copies of the elements in the array intArray. The first time this loop executes, it'll copy what is in the 0 position of intArray into the 0 position of intVal. It'll do this for all positions in the index, and it'll add them to their respective positions in the list box (the 0 position copied in intVal, in this case 10, will be put in the 0 position in the list box and so forth).

8.2 More About Array Processing
-You can total the values in a numeric array using a For Next loop.
For intcount=0 to intUnits.length-1
   intTotal+=intUnits(intCount)
Next

-You can also find the average by adding up all the values like in the last example and then dividing that number by the length of the array.

-You can search through an array using the sequential search method.

-To sort an array, use the Array.Sort method. Array.Sort(ArrayName)

Example: Dim intNumbers() As Integer={7,12,1,6,3}
Array.Sort(intNumbers) will sort the array into 1,3,6,7,12

-To change the amount of elements in an array at runtime, use the ReDim statement
ReDim [Preserve] ArrayName (UpperSubscript)
Preserve-optional keyword, if used any existing values in the array are preserved. If it's not used, existing values in the array are destroyed.
ArrayName-name of the array being resided.
UpperSubscript-new index of the array

8.3 Procedures/Functions and Arrays
-You can use arrays in procedures/functions.

8.4 Multidimensional Arrays
-Like an array of arrays. It can be used to hold multiple sets of values.

A square 3x4 has 12 squares total. A Multi-dimensional array with this square would be declared as
Dim dblScores (2,3) As Double.
(2,3)-the subscript for the array. Since arrays start at a value of 0, the rows are now 2 (0-2) and the columns 3 (0-3).

8.5 GUI Design
Enabled property: When set to false, whatever control is disabled and can't receive the focus/respond to events. Controls may appear "dimmed". Default, it is set to true.

Timer control: Allows an application to automatically execute code at regular time intervals. This is based in milliseconds.

Timer Events: You can create event handlers that respond to timer ticks.

Control Properties: When enabled is set to true, the timer control will respond to tick events. The interval property is the number of milliseconds that elapse between timer events. An interval property of 1,000 means that every second will result in a tick event as 1000 ms=1 second.

8.6 GUI Design
Anchor Property: When a control is anchored to a form's edge, it'll resize if the application is resized in runtime.

Dock Property: When a control is docked, it is positioned directly against a form edge. The length/width of the control is changed to match the length/width of the form edge.

No comments:

Post a Comment