Skip to main content

Introduction to Array

Array
A standard structure for storing data in any programming language is the array. Whereas individual variables can hold single entities, such as one number, one date, or one string, arrayscan hold sets of data of the same type (a set of numbers, a series of dates, and so on). An array has a name, as does a variable, and the values stored in it can be accessed by an index.
For example, you could use the variable Salary to store a person’s salary:
Salary = 23000
But what if you wanted to store the salaries of 16 employees? You should either declare 16 variables – salary1, salary2, up to salary16 – or you could declare an array with 16 elements. An array is similar to a variable: it has a name and multiple values. Each value is identified by an index (an integer value) that follows the array’s name in parentheses. Each different value is an element of the array. If the array salaries holds the salaries of 16 employees, the element salaries(0)holds the salary of the first employee, the element salaries(1) holds the salary of the second employee, and so on up the element salaries(15).
Unlike simple variables, arrays must be declared with the Dim (or Public, or Private) statement followed by the name of the array and the index of the last element in the array in parentheses – for example,
            Dim salaries(15) as Integer
As I said before, salariesis the name of an array that holds the 16 values (the salaries of the 16 employees), with indices ranging from 0 to 15. Salaries(0)is the first person’s salary, Salaries(1) the second person’s salary, and so on. All you have to do is remember who corresponds to each salary, but even this data can be handled by another array. To do this, you’d declare another array of 16 elements as follows:
            Dim Names(15) As String
and then assign values to the elements of both arrays
Names(0) = “Amit”
Salaries(0) = 28000
Names(1) = “Sonal”
Salaries(1) = 34000
…..
Names(15) = “Abhay”
Salaries(15) = 23000
This structure is more compact and more convenient than having to hard-code the names of employees and their salaries in variables.
All elements in an array have the same data type. Of course, when the data type is Object, the individual elements can contain different kinds of data (objects, strings, numbers, and so on).
Arrays, like variables, are not limited to the basic data types. You can declare arrays that hold any type of data, including objects. The following array holds colors, which can be used later in the code as arguments to the various functions that draw sphere.
Dim colors(2) as Color
Colors(0) = Color.Orange
Colors(1) = Color.Pink
Colors(2) = Color.Green

Initializing Arrays

Just as you can initialize variables in the same line where you declare them, you can initialize arrays, too, with the following constructor:

Dim arrayname() as type = {entry0, entry1, . . . entryN}

Here is an example that initializes an array of strings:

Dim names() as String = {“Amit”, “Sonal”}

This statement is equivalent to the following statements, which declare an array with two elements and then set their values:

Dim names(1) As String
names(0) = “Amit”
names(1) = “Sonal”

The number of elements in the curly brackets following the array’s declaration determines the dimensions of the array, and you can’t add new elements to the array without resizing it. However, you can change the value of the existing elements at will, as you would with any other array. The following declaration initializes an array of Color objects in a single statement:
Dim Colors() As Color = {Color.Pink, Color.Blue, Color.Orange, Color.White)

Array Limits
The first element of an array has index 0. The number that appears in the Dim statement is one less than the array’s total capacity and is the array’s upper limit (or upper bound). The index of the last element of an array (its upper bound) is given by the function UBound(), which accepts as argument the array’s name. For the array
            Dim myArray(19) As Integer
its upper bound is 19, and its capacity is 20 elements. The function UBound() is also exposed as a method of the Array object, and it’s the GetUpperBound method. It returns the same value as the UBound() function. The GetLowerBound method. It returns the same value as the UBound() function. The GetLowerBound method returns the index of the array’s first element, which is always zero anyway.
Let’s say you need an array to store 20 names. Declare it with the following statement:
Dim names(19) As String
The first element is names(0), and the last is names(19). If you execute the following arguments the values in bold will appear in the Output window:
Console.WriteLine(names.GetLowerBound(0))
0
Console.WriteLine(names.GetUpperBound(0))
19
To assign a value to the first and last element of the names array, use the following statements:
Names(0) = “First”
Names(19) = “Last”

Note:
This is just an introduction about an array. Still lots of topics are left to discuss like Multi-dimension array, properties of an array and a lot. So I will try my level best to garner all the material and post it on this blog as soon as possible.
Resource Used:
1. DEITEL Text Book
2. Mastering Visual Basic.NET
Compiled By: Chaudhary Amit V.

Comments

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Email/Password

In our  previous  article we saw how to create authentication module using Google identity provider. Now we will see how to implement Email and Password authentication using Firebase. We will use Bootstrap form to create intuitive user interface for sign-up and login using Email and Password. Step 1: Create signUp component. ng generate component signUp Step 2: Create custom form in sign-up.component.html file. The result of above code: Step 3: Add two functions for creating the user and login using Email and Password in src/app/providers/AFAuth.ts file. Step 4: Call createUserWithEmailAndPassword function in service from sign-up.component.ts. Step 5: Update routing configuration in app.module.ts  to include signUp component. Step 6: Update Login form to have Bootstrap form. The result of the above code. Step 7: Enable Email/Password component in Firebas...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...