24
MELJUN CORTES MELJUN CORTES

MELJUN CORTES ARRAY

Embed Size (px)

DESCRIPTION

MELJUN CORTES ARRAY

Citation preview

Page 1: MELJUN CORTES ARRAY

MELJUN CORTESMELJUN CORTES

Page 2: MELJUN CORTES ARRAY

A simple or scalar variable is one that is unrelated to any other variable in memory

An array is a group of variables that have the same name and data type and are related in some way

The most commonly used arrays are one-dimensional and two-dimensional

Programmers use arrays to store related data in the internal memory of the computer

Page 3: MELJUN CORTES ARRAY

If you declare an array locally in a procedure, you can use it only in that procedure.

If you declare an array at the top of a form, you can use it throughout the form.

If you declare an array publicly in a module, you can use it anywhere in the project.

Page 4: MELJUN CORTES ARRAY

Syntax Elementsin Array Declaration

Description

Array name The name you’ll use to represent your array in the program. In general,array names follow the same rules as variable names.

Data type The type of data you’ll store in the array. In most cases, all thevariables in an array are the same type.

Number of dimensions

The number of dimensions that your array will contain. Most arrays areone-dimensional (a list of values) or two-dimensional (a table of values),but you can specify additional dimensions

Number of elements

The number of elements that your array will contain. The elements inyour array correspond directly to the array index. The first array indexis always 0 (zero).

Page 5: MELJUN CORTES ARRAY

For example, to declare a one-dimensional string array named Employees that has room for 10 employee names (numbered 0 through 9), you can type the following in an event procedure:

Dim Employees(9) As String

Public Employees(9) As String

Dim Employees(0 To 9) As String

Page 6: MELJUN CORTES ARRAY

To declare a public two-dimensional array named Scoreboard that has room for two rows and nine columns of Short integer data, you can type this statement in an event procedure or at the top of the form::

Dim Scoreboard(1, 8) As Short

Dim Scoreboard(0 To 1, 0 To 8) As Short

Page 7: MELJUN CORTES ARRAY

To refer to an element of an array, you use the array name and an array index enclosed in parentheses.

Employees(5) = "Leslie"

Page 8: MELJUN CORTES ARRAY

To refer to an element of an array, you use the array name and an array index enclosed in parentheses.

Scoreboard(0, 2) = 4

Page 9: MELJUN CORTES ARRAY

To create an array in this manner, you use what is called an array literal

An array literal consists of a list of comma-separated values that are enclosed in braces ({}).

Page 10: MELJUN CORTES ARRAY

A subscript must reference a valid element of the array. lf a list contains 10 names it wouldn't make sense to ask: What is the 15th name on the list? Or What is the 21/2th name on the list? Visual Basic rounds fractional subscripts and throws an exception for a subscript that is out of range.

Each individual variable is called an element of the array.

Note: Arrays are based on System.Array, which is a collection.

Page 11: MELJUN CORTES ARRAY
Page 12: MELJUN CORTES ARRAY
Page 13: MELJUN CORTES ARRAY
Page 14: MELJUN CORTES ARRAY
Page 15: MELJUN CORTES ARRAY
Page 16: MELJUN CORTES ARRAY
Page 17: MELJUN CORTES ARRAY
Page 18: MELJUN CORTES ARRAY

Fixed Array program uses the UBound function to check for the upper bound, or top index value, of the array.

LBound(ArrayName)UBound(ArrayName)

•LBound function, which confirmsthe lower index value, or lower bound, of

Page 19: MELJUN CORTES ARRAY

LBound function, which confirms the lower index value, or lower bound, of an array, is

Page 20: MELJUN CORTES ARRAY

Dynamic arrays are dimensioned at run time, either when the user specifies the size of the array or when logic you add to the program determines an array size based on specific

Page 21: MELJUN CORTES ARRAY

ReDim statement specifies the size of a dynamic array at run time

If you redimension an array that already has data in it, all the existing data is irretrievably lost

Dim Temperatures() As SingleDim Days As ShortDays = InputBox("How many days?", "Create Array")ReDim Temperatures(Days - 1)

Page 22: MELJUN CORTES ARRAY

Allows you to preserve the data in an array when you change its dimensions.

Dim Philosophers() As Stringyou can redimension the array and add data to it by using code similar to the following:ReDim Philosophers(200)Philosophers(200) = "David Probst"You can expand the size of the Philosophers array to 301 elements (0–300), and preserve the existing contents, by using the following syntax:ReDim Preserve Philosophers(300)

Page 23: MELJUN CORTES ARRAY

To use an array to store items that are related but have different data types.

One solution is to use two parallel one-dimensional arrays: a String array to store the IDs and an Integer array to store the salaries.

Parallel arrays are two or more arrays whose elements are related by their position in the arrays.

Page 24: MELJUN CORTES ARRAY