Documents

An Array of Values

Description
array of values
Categories
Published
of 32
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Transcript
  An Array of Values Introduction A database is a list of values. The values can be organized to make it easy to retrieve and optionally manipulate them. A computer database is a list of values that are stored in the computer, usually as one or more files. The values can then be accessed when needed. Probably the most fundamental type of list of values can be created, and managed, as an array. Practical Learning: Introducing Databases 1.   Start Microsoft Visual Basic and create a Windows Forms Application named NationalBank1  2.   In the Solution Explorer, right-click Form1.vb and click Rename 3.   Type Central.vb  and press Enter Creating an Array Before creating an array, you must decide what type of values each element of the array would be. A simple array can be made of primitive types of values. Here is an example: Private Sub Exercise_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim Numbers() As Double = { 12.44, 525.38, 6.28, 2448.32, 632.04 } End Sub Once the array has been created, you can access each one of its elements using the () operator. Here is an example: Private Sub Exercise_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim Numbers() As Double = { 12.44, 525.38, 6.28, 2448.32, 632.04 } For i As Integer = 0 To 4 lbxNumbers.Items.Add(Numbers(i)) Next End Sub  An array can also be made of elements that are each a composite type. That is, each element can be of a class type. Of course, you must have a class first. You can use one of the many built-in classes of the .NET Framework or you can create your own class. Here is an example: Public Class Person Public PersonID As Integer Public FirstName As String Public LastName As String Public Gender As String Public Sub New() End Sub Public Sub New(ByVal ID As Integer, ByVal FName As String, _ ByVal LName As String, ByVal Gdr As String) PersonID = ID FirstName = FName LastName = LName Gender = Gdr End Sub End Class After creating the class, you can then use it as the type of the array. Here is an example: Private Sub Exercise_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim People(7) As Person People(0) = New Person(72947, Paulette , _ Cranston , Female ) People(1) = New Person(70854, Harry , _ Kumar , Male ) People(2) = New Person(27947, Jules , _ Davidson , Male ) People(3) = New Person(62835, Leslie , _ Harrington , Unknown ) People(4) = New Person(92958, Ernest , _ Colson , Male ) People(5) = New Person(91749, Patricia , _ Katts , Female ) People(6) = New Person(29749, Patrice , _ Abanda , Unknown ) People(7) = New Person(24739, Frank , _ Thomasson , Male ) For i As Integer = 0 To 7 Dim lviPerson As ListViewItem = New ListViewItem(People(i).FirstName) lviPerson.SubItems.Add(People(i).LastName) lviPerson.SubItems.Add(People(i).Gender) lvwPeople.Items.Add(lviPerson)   Next End Sub The Array Class To assist you with creating or managing arrays, the .NET Framework provides the Array  class. Every array you create is derived from this class. As a result, all arrays of your program share many characteristics and they get their foundation from the Array  class, which include its properties and methods. Once you declare an array variable, it automatically has access to the members of the Array class. For example, instead of counting the number of elements in an array, you can access the Length   property of the array variable. Here is an example: Private Sub Exercise_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim Numbers() As Integer = {20, 5, 83405, 734, 5} For i As Integer = 0 To Numbers.Length - 1 lbxNumbers.Items.Add(Numbers(i)) Next End Sub In traditional languages, when you declare an array variable, you must specify the number of elements that the array will contain. You must do this especially if you declare the variable without initializing the array. Here is an example: Private Sub Exercise_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load dim People(4) as Person End Sub After declaring the variable, before using the array, you must initialize it. Otherwise you would receive an error. When initializing the array, you can only initialize it with the number of elements you had specified. To illustrate this, consider the following program: Private Sub Exercise_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim People(4) As Person   People(0) = New Person(72947, Paulette , _ Cranston , Female ) People(1) = New Person(70854, Harry , _ Kumar , Male ) People(2) = New Person(27947, Jules , _ Davidson , Male ) People(3) = New Person(62835, Leslie , _ Harrington , Unknown ) People(4) = New Person(92958, Ernest , _ Colson , Male ) For i As Integer = 0 To People.Length - 1 Dim lviPerson As ListViewItem = New ListViewItem(People(i).FirstName) lviPerson.SubItems.Add(People(i).LastName) lviPerson.SubItems.Add(People(i).Gender) lvwPeople.Items.Add(lviPerson) Next End Sub  Notice that the variable is declared to hold only 4 elements but the user tries to access a 5th one. This would produce an IndexOutOfRangeExcception  exception: One of the most valuable features of the Array  class is that it allows an array to be resizable . That is, if you find out that an array has a size smaller than the number of elements you want to add to it, you can increase its capacity. To support this, the Array class is equipped with the static Resize()  method. Its syntax is: Public Shared Sub Resize(Of T) (ByRef array As T(), newSize As Integer) As you can see, this is a generic method that takes two arguments. The first argument is the name of the array variable that you want to resize. It must be passed by reference. The second argument is the new size you want the array to have. Here is an example of calling this method: Public Class Exercise Private Sub Exercise_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim People(4) As Person People(0) = New Person(72947, Paulette , _ Cranston , Female ) People(1) = New Person(70854, Harry , _ Kumar , Male ) People(2) = New Person(27947, Jules , _ Davidson , Male ) People(3) = New Person(62835, Leslie , _ Harrington , Unknown ) People(4) = New Person(92958, Ernest , _ Colson , Male )
Search
Tags
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks