An Array of Values

download An Array of Values

of 32

Transcript of An Array of Values

  • 8/10/2019 An Array of Values

    1/32

    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 asone or more files. The values can then be accessed when needed. Probably the most fundamentaltype 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

    NationalBank12. In the Solution Explorer, right-click Form1.vb and click Rename

    3. Type Central.vband 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.LoadDim 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.LoadDim 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))

    NextEnd Sub

  • 8/10/2019 An Array of Values

    2/32

    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 youcan create your own class. Here is an example:

    Public Class PersonPublic PersonID As IntegerPublic FirstName As String

    Public LastName As StringPublic 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 = LNameGender = 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.LoadDim 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 7Dim lviPerson As ListViewItem = New ListViewItem(People(i).FirstName)

    lviPerson.SubItems.Add(People(i).LastName)lviPerson.SubItems.Add(People(i).Gender)

    lvwPeople.Items.Add(lviPerson)

  • 8/10/2019 An Array of Values

    3/32

    NextEnd Sub

    The Array Class

    To assist you with creating or managing arrays, the .NET Framework provides the Arrayclass. Everyarray 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 Arrayclass, 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 Lengthproperty 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 elementsthat 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.LoadDim People(4) As Person

  • 8/10/2019 An Array of Values

    4/32

    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. Thiswould produce an IndexOutOfRangeExcceptionexception:

    One of the most valuable features of the Arrayclass is that it allows an array to be "resizable". Thatis, 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 ofthe 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")

  • 8/10/2019 An Array of Values

    5/32

    Array.Resize(Of Person)(People, 8)

    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 People.Length - 1Dim lviPerson As ListViewItem = New ListViewItem(People(i).FirstName)

    lviPerson.SubItems.Add(People(i).LastName)

    lviPerson.SubItems.Add(People(i).Gender)

    lvwPeople.Items.Add(lviPerson)

    NextEnd Sub

    End Class

    The advantage of this approach is that you can access the array from any member of the same class oreven from another file of the same program.

    An Array as a Field

    As done previously, you can create an array in a method. A disadvantage of this approach is that the

    array can be accessed from only the method (or event) in which it is created. As an alternative, youcan declare an array as a member of a class. Here is an example:

    Public Class Exercise

    Dim People() As PersonEnd Class

    The advantage of this approach is that you can access the array from any member of the same class oreven from another file of the same program. After declaring the variable, you can initialize it. You can

    do this in a constructor or in an event that would be fired before any other event that would use the

    array. This type of initialization is usually done in a Loadevent of a form. After initializing the array,you can then access in another method or another event of the form. Here is an example:

    Public Class Exercise

    Dim People(7)As Person

    Private Sub Exercise_Load(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles MyBase.Load

    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", _

  • 8/10/2019 An Array of Values

    6/32

    "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 People.Length - 1Dim lviPerson As ListViewItem = New ListViewItem(People(i).FirstName)

    lviPerson.SubItems.Add(People(i).LastName)

    lviPerson.SubItems.Add(People(i).Gender)

    lvwPeople.Items.Add(lviPerson)

    NextEnd Sub

    End Class

    Arrays and Methods

    An array can be passed as argument to a method. Here is an example:

    Public Class Exercise

    Private Sub ArrayInitializer(ByVal People() As Person)

    End SubEnd Class

    In the method, you can use the array as you see fit. For example you can assign values to the elementsof the array. When calling a method that is passed an array, you can pass the argument by reference so

    it would come back with new values. Here are examples:

    Public Class Exercise

    Private Sub ArrayInitializer(ByVal People() 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")

    End Sub

  • 8/10/2019 An Array of Values

    7/32

    Private Sub ShowPeople(ByVal People() As Person)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

    Private Sub Exercise_Load(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles MyBase.LoadDim Persons(7) As Person

    ArrayInitializer(Persons)ShowPeople(Persons)

    End SubEnd Class

    A method can be created to return an array. When creating the method, on the right side of the name

    of the method, type the name of the type of value the method would return. In the method, create and

    initialize an array. Before exiting the method, you must return the array. Here is an example:

    Public Class ExercisePrivate FunctionArrayInitializer() As Person()

    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")

    Return PeopleEnd Function

    Private Sub ShowPeople(ByVal People() As Person)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)

  • 8/10/2019 An Array of Values

    8/32

    lvwPeople.Items.Add(lviPerson)

    Next

    End Sub

    Private Sub Exercise_Load(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles MyBase.LoadDim Persons(7) As Person

    Persons = ArrayInitializer()

    ShowPeople(Persons)End Sub

    End Class

    Practical Learning: Creating an Array

    1. To create a new class, on the main menu, click Project -> Add Class...2. Set the name to Employeeand press Enter

    3. Complete the file as follows:

    Public Class Employee

    Public EmployeeNumber As IntegerPublic FirstName As String

    Public LastName As StringPublic Title As StringPublic CanCreateNewAccount As Boolean

    Public HourlySalary As Double

    Public ReadOnly Property FullName() As StringGet

    Return LastName & ", " & FirstName

    End GetEnd Property

    End Class

    4.

    To create a form that will be used to displays the employees information, on the main menu,

    click Project -> Add Windows Form

    5. Set the name to Employeesand press Enter

    6. Design the form as follows:

  • 8/10/2019 An Array of Values

    9/32

    Control Text Name Other Properties

    ListView lvwEmployees

    FullRowSelect: True

    GridLines: True

    View: Details

    Columns

    (Name) Text TextAlignWidth

    colIndex # 20

    colEmployeeNumber Empl # 50

    colFirstName First Name 65

    colLastName Last Name 65

    colFullName Full Name 95

    colTitle Title 145

    colHourlySalary Salary Right 50

    Button Close btnClose

    7. To create a new class, on the main menu, click Project -> Add Class...

    8. Set the name to Customerand press Enter

    9. Complete the file as follows:

    Public Enum AccountStatus

    Active

    SuspendedClosed

    End Enum

    Public Class CustomerPublic CreatedBy As StringPublic DateCreated As DateTime

    Public CustomerName As String

    Public AccountNumber As String

    Public AccountType As String

  • 8/10/2019 An Array of Values

    10/32

    Public Status As AccountStatusEnd Class

    10.To create a form that will show the customers information, on the main menu, click Project ->Add Windows Form

    11.

    Set the name to Customersand press Enter12.Design the form as follows:

    Control Text Name Other Properties

    ListView lvwCustomers

    FullRowSelect: True

    GridLines: True

    View: Details

    Columns

    (Name) Text TextAlign Width

    colIndex # 20

    colCreatedBy Created By 100

    colDateCreated Date Created Center 75

    colCustomerName Customer Name 120

    colAccountNumber Account # Center 80

    colAccountType Account Type 80

    coloAccountStatus Status 50

    Button Close btnClose

    13.Double-click the Close button and implement its event as follows:

    Private Sub btnClose_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) HandlesbtnClose.Click

    Close()

    End Sub

  • 8/10/2019 An Array of Values

    11/32

    14.To create a new class, on the main menu, click Project -> Add Class...15.Set the name to AccountTransactionand press Enter

    16.Complete the file as follows:

    Public Enum TransactionTypesDeposit

    WithdrawalTransferMonthlyCharge

    MoneyOrderOverdraft

    TransactionFeeEnd Enum

    Public Class AccountTransactionPublic TransactionDate As DateTime

    Public ProcessedBy As IntegerPublic ProcessedFor As StringPublic TransactionType As TransactionTypes

    Public DepositAmount As Double

    Public WithdrawalAmount As Double

    Public ChargeAmount As DoubleEnd Class

    17.To create a new form that will show the transactions done on the customers accounts, on the

    main menu, click Project -> Add Windows Form18.Set the name to CustomersTransactionsand press Enter

    19.Design the form as follows:

  • 8/10/2019 An Array of Values

    12/32

    Control Text Name Other Properties

    ListView lvwTransactionsFullRowSelect: TrueGridLines: True

    View: Details

    Columns

    (Name) Text TextAlign Width

    colIndex # 25

    colTransactionDate Trans Date Center 80

    colProcessedBy Processed By Center 80

    colProcessedFor Processed For Center 90

    colTransactionType Trans Type 90

    colDepositAmount Deposit RightcolWithdrawalAmount Withdrawal Right 65

    colChargeAmount Charge Right 50

    Button Close btnClose

    20.Double-click the Close button and implement its event as follows:

  • 8/10/2019 An Array of Values

    13/32

    Private Sub btnClose_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles

    btnClose.ClickClose()

    End Sub

    21.To create a new class, on the main menu, click Project -> Add Class...

    22.Set the name to AccountsRecordsand press Enter23.Complete the file as follows:

    Public Class AccountsRecords

    Public Shared Function GetEmployees() As Employee()Dim StaffMembers(6) As Employee

    StaffMembers(0) = New EmployeeStaffMembers(0).EmployeeNumber = 74228

    StaffMembers(0).FirstName = "Chrissie"StaffMembers(0).LastName = "Nomads"

    StaffMembers(0).Title = "General Manager"StaffMembers(0).CanCreateNewAccount = TrueStaffMembers(0).HourlySalary = 40.25

    StaffMembers(1) = New Employee

    StaffMembers(1).EmployeeNumber = 27905StaffMembers(1).FirstName = "Calvin"StaffMembers(1).LastName = "Braxton"

    StaffMembers(1).Title = "Public Relations Manager"

    StaffMembers(1).CanCreateNewAccount = FalseStaffMembers(1).HourlySalary = 25.95

    StaffMembers(2) = New Employee()

    StaffMembers(2).EmployeeNumber = 94805StaffMembers(2).FirstName = "Emilie"

    StaffMembers(2).LastName = "Pastore"

    StaffMembers(2).Title = "Branch Manager"StaffMembers(2).CanCreateNewAccount = True

    StaffMembers(2).HourlySalary = 32.55

    StaffMembers(3) = New Employee()StaffMembers(3).EmployeeNumber = 39850StaffMembers(3).FirstName = "Walter"

    StaffMembers(3).LastName = "Lemme"StaffMembers(3).Title = "Accounts Manager"

    StaffMembers(3).CanCreateNewAccount = True

    StaffMembers(3).HourlySalary = 28.35

    StaffMembers(4) = New Employee()StaffMembers(4).EmployeeNumber = 70594

    StaffMembers(4).FirstName = "Cecile"StaffMembers(4).LastName = "Richards"

  • 8/10/2019 An Array of Values

    14/32

    StaffMembers(4).Title = "Accounts Representative"StaffMembers(4).CanCreateNewAccount = FalseStaffMembers(4).HourlySalary = 18.15

    StaffMembers(5) = New Employee()

    StaffMembers(5).EmployeeNumber = 85285

    StaffMembers(5).FirstName = "Joan"StaffMembers(5).LastName = "Verrion"

    StaffMembers(5).Title = "Accounts Representative"StaffMembers(5).CanCreateNewAccount = False

    StaffMembers(5).HourlySalary = 14.85

    StaffMembers(6) = New Employee()

    StaffMembers(6).EmployeeNumber = 94852StaffMembers(6).FirstName = "Donald"

    StaffMembers(6).LastName = "Waters"StaffMembers(6).Title = "Accounts Representative"StaffMembers(6).CanCreateNewAccount = False

    StaffMembers(6).HourlySalary = 16.45

    Return StaffMembersEnd Function

    Public Shared Function GetCustomers() As Customer()Dim Clients(4) As Customer

    Clients(0) = New Customer

    Clients(0).CreatedBy = "Lemme, Walter"

    Clients(0).DateCreated = New DateTime(2007, 1, 16)Clients(0).CustomerName = "Louis George Berholdt"

    Clients(0).AccountNumber = "95-722947-93"Clients(0).AccountType = "Checking"Clients(0).Status = AccountStatus.Active

    Clients(1) = New Customer()

    Clients(1).CreatedBy = "Pastore, Emilie"

    Clients(1).DateCreated = New DateTime(2007, 1, 18)

    Clients(1).CustomerName = "William Foster"Clients(1).AccountNumber = "62-384638-48"

    Clients(1).AccountType = " Checking"Clients(1).Status = AccountStatus.Active

    Clients(2) = New Customer()Clients(2).CreatedBy = "Lemme, Walter"

    Clients(2).DateCreated = New DateTime(2007, 1, 18)Clients(2).CustomerName = "Catherine Hoods"

    Clients(2).AccountNumber = "92-318284-75"

    Clients(2).AccountType = "Checking"Clients(2).Status = AccountStatus.Active

    Clients(3) = New Customer()Clients(3).CreatedBy = "Lemme, Walter"

    Clients(3).DateCreated = New DateTime(2007, 3, 26)Clients(3).CustomerName = "Harriett Cranston"

    Clients(3).AccountNumber = "17-490040-83"

  • 8/10/2019 An Array of Values

    15/32

    Clients(3).AccountType = "Saving"Clients(3).Status = AccountStatus.Active

    Clients(4) = New Customer()

    Clients(4).CreatedBy = "Nomads, Chrissie"

    Clients(4).DateCreated = New DateTime(2007, 4, 4)

    Clients(4).CustomerName = "Janice Bonnie Weiss"Clients(4).AccountNumber = "58-405048-15"

    Clients(4).AccountType = "Checking"Clients(4).Status = AccountStatus.Active

    Return ClientsEnd Function

    Public Shared Function GetTransactions() As AccountTransaction()

    Dim Transactions(28) As AccountTransaction

    Transactions(0) = New AccountTransaction

    Transactions(0).TransactionDate = New DateTime(2007, 1, 16)Transactions(0).ProcessedBy = 39850

    Transactions(0).ProcessedFor = "95-722947-93"Transactions(0).TransactionType = TransactionTypes.DepositTransactions(0).DepositAmount = 325.5

    Transactions(1) = New AccountTransaction

    Transactions(1).TransactionDate = New DateTime(2007, 1, 18)Transactions(1).ProcessedBy = 94805

    Transactions(1).ProcessedFor = "62-384638-48"

    Transactions(1).TransactionType = TransactionTypes.DepositTransactions(1).DepositAmount = 550.0

    Transactions(2) = New AccountTransactionTransactions(2).TransactionDate = New DateTime(2007, 1, 18)

    Transactions(2).ProcessedBy = 39850Transactions(2).ProcessedFor = "92-318284-75"

    Transactions(2).TransactionType = TransactionTypes.DepositTransactions(2).DepositAmount = 975.35

    Transactions(3) = New AccountTransactionTransactions(3).TransactionDate = New DateTime(2007, 1, 22)

    Transactions(3).ProcessedBy = 94852Transactions(3).ProcessedFor = "95-722947-93"Transactions(3).TransactionType = TransactionTypes.Withdrawal

    Transactions(3).WithdrawalAmount = 100.0

    Transactions(4) = New AccountTransactionTransactions(4).TransactionDate = New DateTime(2007, 1, 22)

    Transactions(4).ProcessedBy = 70594

    Transactions(4).ProcessedFor = "62-384638-48"Transactions(4).TransactionType = TransactionTypes.Withdrawal

    Transactions(4).WithdrawalAmount = 122.48

    Transactions(5) = New AccountTransaction

    Transactions(5).TransactionDate = New DateTime(2007, 1, 22)Transactions(5).ProcessedBy = 94852

    Transactions(5).ProcessedFor = "92-318284-75"

  • 8/10/2019 An Array of Values

    16/32

    Transactions(5).TransactionType = TransactionTypes.WithdrawalTransactions(5).WithdrawalAmount = 214.86

    Transactions(6) = New AccountTransaction

    Transactions(6).TransactionDate = New DateTime(2007, 1, 22)

    Transactions(6).ProcessedBy = 85285

    Transactions(6).ProcessedFor = "62-384638-48"Transactions(6).TransactionType = TransactionTypes.Withdrawal

    Transactions(6).WithdrawalAmount = 140.0

    Transactions(7) = New AccountTransactionTransactions(7).TransactionDate = New DateTime(2007, 1, 24)Transactions(7).ProcessedBy = 85285

    Transactions(7).ProcessedFor = "95-722947-93"Transactions(7).TransactionType = TransactionTypes.MoneyOrder

    Transactions(7).WithdrawalAmount = 116.24

    Transactions(8) = New AccountTransaction

    Transactions(8).TransactionDate = New DateTime(2007, 1, 24)Transactions(8).ProcessedFor = "95-722947-93"

    Transactions(8).TransactionType =TransactionTypes.TransactionFee

    Transactions(8).ChargeAmount = 1.45

    Transactions(9) = New AccountTransaction

    Transactions(9).TransactionDate = New DateTime(2007, 1, 30)Transactions(9).ProcessedBy = 70594

    Transactions(9).ProcessedFor = "62-384638-48"

    Transactions(9).TransactionType = TransactionTypes.WithdrawalTransactions(9).WithdrawalAmount = 40.0

    Transactions(10) = New AccountTransactionTransactions(10).TransactionDate = New DateTime(2007, 1, 30)

    Transactions(10).ProcessedFor = "95-722947-93"Transactions(10).TransactionType =

    TransactionTypes.MonthlyChargeTransactions(10).ChargeAmount = 6.0

    Transactions(11) = New AccountTransactionTransactions(11).TransactionDate = New DateTime(2007, 1, 30)

    Transactions(11).ProcessedFor = "92-318284-75"Transactions(11).TransactionType =

    TransactionTypes.MonthlyCharge

    Transactions(11).ChargeAmount = 6.0

    Transactions(12) = New AccountTransactionTransactions(12).TransactionDate = New DateTime(2007, 1, 30)

    Transactions(12).ProcessedFor = "62-384638-48"

    Transactions(12).TransactionType =TransactionTypes.MonthlyCharge

    Transactions(12).ChargeAmount = 6.0

    Transactions(13) = New AccountTransaction

    Transactions(13).TransactionDate = New DateTime(2007, 2, 6)Transactions(13).ProcessedBy = 85285

    Transactions(13).ProcessedFor = "92-318284-75"

  • 8/10/2019 An Array of Values

    17/32

    Transactions(13).TransactionType =TransactionTypes.Withdrawal

    Transactions(13).WithdrawalAmount = 42.35

    Transactions(14) = New AccountTransaction

    Transactions(14).TransactionDate = New DateTime(2007, 2, 6)

    Transactions(14).ProcessedBy = 70594Transactions(14).ProcessedFor = "95-722947-93"

    Transactions(14).TransactionType =TransactionTypes.Withdrawal

    Transactions(14).WithdrawalAmount = 115.0

    Transactions(15) = New AccountTransaction

    Transactions(15).TransactionDate = New DateTime(2007, 2, 6)Transactions(15).ProcessedBy = 94852

    Transactions(15).ProcessedFor = "62-384638-48"Transactions(15).TransactionType =

    TransactionTypes.Withdrawal

    Transactions(15).WithdrawalAmount = 64.14

    Transactions(16) = New AccountTransactionTransactions(16).TransactionDate = New DateTime(2007, 2, 28)Transactions(16).ProcessedFor = "95-722947-93"

    Transactions(16).TransactionType =TransactionTypes.MonthlyCharge

    Transactions(16).ChargeAmount = 6.0

    Transactions(17) = New AccountTransaction

    Transactions(17).TransactionDate = New DateTime(2007, 2, 28)Transactions(17).ProcessedFor = "95-722947-93"

    Transactions(17).TransactionType = TransactionTypes.OverdraftTransactions(17).WithdrawalAmount = 35.0

    Transactions(18) = New AccountTransactionTransactions(18).TransactionDate = New DateTime(2007, 2, 28)

    Transactions(18).ProcessedFor = "62-384638-48"Transactions(18).TransactionType =

    TransactionTypes.MonthlyCharge

    Transactions(18).ChargeAmount = 6.0

    Transactions(19) = New AccountTransactionTransactions(19).TransactionDate = New DateTime(2007, 2, 28)Transactions(19).ProcessedFor = "92-318284-75"

    Transactions(19).TransactionType =TransactionTypes.MonthlyCharge

    Transactions(19).ChargeAmount = 6.0

    Transactions(20) = New AccountTransaction

    Transactions(20).TransactionDate = New DateTime(2007, 3, 15)Transactions(20).ProcessedBy = 70594

    Transactions(20).ProcessedFor = "95-722947-93"Transactions(20).TransactionType =

    TransactionTypes.Withdrawal

    Transactions(20).WithdrawalAmount = 200.0

    Transactions(21) = New AccountTransaction

  • 8/10/2019 An Array of Values

    18/32

    Transactions(21).TransactionDate = New DateTime(2007, 3, 26)Transactions(21).ProcessedBy = 39850Transactions(21).ProcessedFor = "17-490040-83"

    Transactions(21).TransactionType = TransactionTypes.Deposit

    Transactions(21).DepositAmount = 1000.0

    Transactions(22) = New AccountTransactionTransactions(22).TransactionDate = New DateTime(2007, 4, 4)

    Transactions(22).ProcessedBy = 74228Transactions(22).ProcessedFor = "58-405048-15"

    Transactions(22).TransactionType = TransactionTypes.DepositTransactions(22).DepositAmount = 126.85

    Transactions(23) = New AccountTransactionTransactions(23).TransactionDate = New DateTime(2007, 4, 6)

    Transactions(23).ProcessedBy = 85285Transactions(23).ProcessedFor = "92-318284-75"Transactions(23).TransactionType = TransactionTypes.Deposit

    Transactions(23).DepositAmount = 250.0

    Transactions(24) = New AccountTransactionTransactions(24).TransactionDate = New DateTime(2007, 4, 6)Transactions(24).ProcessedBy = 70594

    Transactions(24).ProcessedFor = "62-384638-48"Transactions(24).TransactionType = TransactionTypes.Deposit

    Transactions(24).DepositAmount = 400.0

    Transactions(25) = New AccountTransaction

    Transactions(25).TransactionDate = New DateTime(2007, 4, 12)Transactions(25).ProcessedBy = 94852

    Transactions(25).ProcessedFor = "95-722947-93"Transactions(25).TransactionType = TransactionTypes.DepositTransactions(25).DepositAmount = 100.0

    Transactions(25) = New AccountTransaction()

    Transactions(25).TransactionDate = New DateTime(2007, 4, 30)Transactions(25).ProcessedFor = "58-405048-15"

    Transactions(25).TransactionType =

    TransactionTypes.MonthlyChargeTransactions(25).ChargeAmount = 6.0

    Transactions(26) = New AccountTransactionTransactions(26).TransactionDate = New DateTime(2007, 4, 30)

    Transactions(26).ProcessedFor = "62-384638-48"Transactions(26).TransactionType =

    TransactionTypes.MonthlyChargeTransactions(26).ChargeAmount = 6.0

    Transactions(27) = New AccountTransaction()Transactions(27).TransactionDate = New DateTime(2007, 4, 30)

    Transactions(27).ProcessedFor = "92-318284-75"Transactions(27).TransactionType =

    TransactionTypes.MonthlyCharge

    Transactions(27).ChargeAmount = 6.0

    Transactions(28) = New AccountTransaction()

  • 8/10/2019 An Array of Values

    19/32

    Transactions(28).TransactionDate = New DateTime(2007, 4, 30)Transactions(28).ProcessedFor = "17-490040-83"Transactions(28).TransactionType =

    TransactionTypes.MonthlyCharge

    Transactions(28).ChargeAmount = 6.0

    Return TransactionsEnd Function

    End Class

    24.Access the Central form and a button to it

    25.Change the properties of the button as follows:

    (Name): btnEmployeesText: Employees

    26.Double-click the Employees button and implement its event as follows:

    Private Sub btnEmployees_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) HandlesbtnEmployees.Click

    Dim i As Integer

    Dim frmEmployees As Employees = New EmployeesDim StaffMembers() As Employee = AccountsRecords.GetEmployees()

    frmEmployees.lvwEmployees.Items.Clear()

    i = 1

    For Each empl As Employee In StaffMembersDim lviEmployee As ListViewItem = New ListViewItem(i)

    lviEmployee.SubItems.Add(CStr(empl.EmployeeNumber))lviEmployee.SubItems.Add(empl.FirstName)

    lviEmployee.SubItems.Add(empl.LastName)

    lviEmployee.SubItems.Add(empl.FullName)

    lviEmployee.SubItems.Add(empl.Title)lviEmployee.SubItems.Add(empl.HourlySalary.ToString("F"))

    frmEmployees.lvwEmployees.Items.Add(lviEmployee)i = i + 1

    Next

    frmEmployees.ShowDialog()

    End Sub

    27.Return to the Central form and a button to it28.Change the properties of the button as follows:

    (Name): btnCustomers

    Text: Customers29.Double-click the Employees button and implement its event as follows:

  • 8/10/2019 An Array of Values

    20/32

    Private Sub btnCustomers_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles

    btnCustomers.Click

    Dim i As Integer

    Dim frmCustomers As Customers = New Customers

    Dim Clients() As Customer = AccountsRecords.GetCustomers()

    frmCustomers.lvwCustomers.Items.Clear()

    i = 1

    For Each client As Customer In ClientsDim lviCustomer As ListViewItem = New ListViewItem(i)

    lviCustomer.SubItems.Add(client.CreatedBy)

    lviCustomer.SubItems.Add(client.DateCreated.ToShortDateString())lviCustomer.SubItems.Add(client.CustomerName)

    lviCustomer.SubItems.Add(client.AccountNumber)lviCustomer.SubItems.Add(client.AccountType)

    lviCustomer.SubItems.Add(client.Status.ToString())

    frmCustomers.lvwCustomers.Items.Add(lviCustomer)

    i = i + 1Next

    frmCustomers.ShowDialog()

    End Sub

    30.Return to the Central form and a button to it

    31.Change the properties of the button as follows:(Name): btnTransactions

    Text: Transactions32.Double-click the Employees button and implement its event as follows:

    Private Sub btnTransactions_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) _

    Handles btnTransactions.Click

    Dim i As IntegerDim frmTransactions As CustomersTransactions = _

    New CustomersTransactionsDim Transactions() As AccountTransaction = _

    AccountsRecords.GetTransactions()

    frmTransactions.lvwTransactions.Items.Clear()

    i = 1

    For Each ActTrans As AccountTransaction In TransactionsDim lviTransactions As ListViewItem = New ListViewItem(i)

    lviTransactions.SubItems.Add( _ActTrans.TransactionDate.ToString("dd-MMM-yyyy"))

  • 8/10/2019 An Array of Values

    21/32

    If ActTrans.ProcessedBy = 0 Then

    lviTransactions.SubItems.Add("")

    Else

    lviTransactions.SubItems.Add(ActTrans.ProcessedBy)

    End If

    lviTransactions.SubItems.Add(ActTrans.ProcessedFor)

    lviTransactions.SubItems.Add(ActTrans.TransactionType.ToString())

    If ActTrans.DepositAmount = 0 ThenlviTransactions.SubItems.Add("")

    ElselviTransactions.SubItems.Add( _

    ActTrans.DepositAmount.ToString("F"))End If

    If ActTrans.WithdrawalAmount = 0 ThenlviTransactions.SubItems.Add("")

    ElselviTransactions.SubItems.Add( _

    ActTrans.WithdrawalAmount.ToString("F"))

    End If

    If ActTrans.ChargeAmount = 0 ThenlviTransactions.SubItems.Add("")

    Else

    lviTransactions.SubItems.Add( _ActTrans.ChargeAmount.ToString("F"))

    End If

    frmTransactions.lvwTransactions.Items.Add(lviTransactions)i = i + 1

    Next

    frmTransactions.ShowDialog()

    End Sub

    33.Execute the application to see the results34.Close the forms and return to your programming environment

    Operations on an Array

    Introduction

    Because an array is primarily a series of objects or values, there are various pieces of information youwould get interested to get from it. Typical operations include:

    Adding elements to the array

    Re-arranging the list or the order of elements in the array.

    Finding out whether the array contains a certain element

  • 8/10/2019 An Array of Values

    22/32

    If the array contains a certain element, what index that element has

    Although you can write your own routines to perform these operations, the Array class provides most

    of the methods you would need to get the necessary information.

    Practical Learning: Introducing Operations on Arrays

    1. To create a new form, on the main menu, click Project -> Add Windows Form2. Set the name to CustomerTransactionsand press Enter

    3. Design the form as follows:

    Control Text Name Other Properties

    Label View Transactions For:

    MaskedTextBox txtAccountNumber Mask: 00-000000-00

    Button Find btnFind

    ListView lvwCustomersFullRowSelect: TrueGridLines: True

    View: Details

    Columns

    (Name) Text TextAlignWidth

    colIndex # 25

    colTransactionDate Trans Date Center 80

  • 8/10/2019 An Array of Values

    23/32

    colProcessedBy Processed By Center 80

    colTransactionType Trans Type 90

    colDeposit Deposit Right

    colWithdrawal Withdrawal Right 65

    colChargeAmount Charge Right 50

    Button Close btnClose

    4. Save all

    Adding an Element to an Array

    We have seen that, using the () operator, you can add a new element to an array. Still, to support thisoperation, the Array class is equipped with the SetValue()method that is overloaded with various

    versions. Here is an example that adds an element to the third position of the array:

    Public Class Exercise

    Dim People(7) As Person

    Private Sub ArrayInitializer()

    For i As Integer = 0 To 7

    People(i) = New PersonPeople(i).PersonID = 0

    People(i).FirstName = ""People(i).LastName = ""People(i).Gender = "Unknown"

    Next

  • 8/10/2019 An Array of Values

    24/32

    End Sub

    Private Sub ShowPeople()lvwPeople.Items.Clear()

    For Each Pers As Person In PeopleDim lviPerson As ListViewItem = New ListViewItem(Pers.FirstName)

    lviPerson.SubItems.Add(Pers.LastName)

    lviPerson.SubItems.Add(Pers.Gender)

    lvwPeople.Items.Add(lviPerson)Next

    End Sub

    Private Sub Exercise_Load(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles MyBase.LoadArrayInitializer()ShowPeople()

    End Sub

    Private Sub btnAdd_Click(ByVal sender As Object, _ByVal e As System.EventArgs) Handles btnAdd.Click

    Dim pers As Person = New Person

    pers.PersonID = CInt(txtPersonID.Text)

    pers.FirstName = txtFirstName.Textpers.LastName = txtLastName.Textpers.Gender = cbxGenders.Text

    People.SetValue(pers, 2)

    ShowPeople()

    End SubEnd Class

    When the Array.SetValue()method is called, it replaces the element at the indicated position.

    Getting an Element From an Array

    The reverse of adding an item to an array is to retrieve one. To support this operation, the Arrayclassis equipped with the GetValue()method that comes in various versions. Here is an example of calling

    it:

    Public Class Exercise

    Dim People(7) As Person

    Private Sub Exercise_Load(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles MyBase.Load

    People(0) = New Person(72947, "Paulette", _"Cranston", "Female")

    People(1) = New Person(70854, "Harry", _

    "Kumar", "Male")People(2) = New Person(27947, "Jules", _

  • 8/10/2019 An Array of Values

    25/32

    "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 People.Length - 1Dim 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

    Private Sub lvwPeople_ItemSelectionChanged(ByVal sender As Object, _ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) _

    Handles

    lvwPeople.ItemSelectionChangedDim pers As Person = CType(People.GetValue(e.ItemIndex), Person)

    txtPersonID.Text = pers.PersonIDtxtFirstName.Text = pers.FirstName

    txtLastName.Text = pers.LastNamecbxGenders.Text = pers.Gender

    End Sub

    End Class

    When calling this method, make sure you provide a valid index, if you do not, you would get anIndexOutOfRangeExceptionexception.

    Checking the Existence of an Element

    Once some elements have been added to an array, you can try to locate one. One of the most

    fundamental means of looking for an item consists of finding out whether a certain element exists in

    the array. To support this operation, the Arrayclass is equipped with the Exists()method whose

    syntax is:

    Public Shared Function Exists(Of T) ( _array As T(), _match As Predicate(Of T) _

    ) As Boolean

    This is a generic method that takes two arguments. The first is the array on which you will look forthe item. The second argument is a delegate that specifies the criterion to apply. Here is an example:

  • 8/10/2019 An Array of Values

    26/32

    Public Class ExerciseDim People(7) As Person

    Shared IDToFind As Integer

    Private Sub Exercise_Load(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles MyBase.Load. . . No Change

    End Sub

    Private Shared Function IDExists(ByVal p As Person) As Boolean

    If p.PersonID = IDToFind ThenReturn True

    ElseReturn False

    End If

    End Function

    Private Sub btnExists_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles

    btnExists.Click

    IDToFind = CInt(txtPersonID.Text)

    If Array.Exists(People, AddressOf IDExists)ThenMessageBox.Show("The person was found")

    Else

    MessageBox.Show("The person was not found anywhere")End If

    End SubEnd Class

  • 8/10/2019 An Array of Values

    27/32

    Practical Learning: Checking the Existence of an Element

    1. On the Account Transactions form, double-click the Find button and change the file asfollows:

    Public Class CustomerTransactions

    Shared AccountNumber As String

    Private Shared Function AccountNumberExists(ByVal Client As

    Customer) _As Boolean

    If Client.AccountNumber = AccountNumber ThenReturn True

    Else

    Return FalseEnd If

    End Function

    Private Sub btnFind_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) _Handles btnFind.Click

    AccountNumber = txtAccountNumber.Text

    Dim Accounts() As Customer = AccountsRecords.GetCustomers()

    If Array.Exists(Accounts, AddressOf AccountNumberExists) Then

    MsgBox("Good")Else

    MsgBox("There is no customer with that account number")

    End If

  • 8/10/2019 An Array of Values

    28/32

    End SubEnd Class

    2. Display the Central form and complete its design as follows:

    ButtonText Name

    Employees btnEmployees

    Customers btnCustomers

    Transactions btnTransactions

    Customer Records btnCustomerRecords

    Close btnClose

    3. Double-click the Customer Records button and implement its event as follows:

    Private Sub btnCustomerRecords_Click(ByVal sender As Object, _

    ByVal e As System.EventArgs)_

    HandlesbtnCustomerRecords.ClickDim CustRecords As CustomerTransactions = New

    CustomerTransactionsCustRecords.ShowDialog()

    End Sub

    4. Return to the Central form and double-click the Close button5. Implement its event as follows:

  • 8/10/2019 An Array of Values

    29/32

    Private Sub btnClose_Click(ByVal sender As Object, _

    ByVal e As System.EventArgs) Handles

    btnClose.ClickEnd

    End Sub

    6. Execute the application to test it

    7. Close the forms and return to your programming environment

    Finding an Element

    One of the most valuable operations you can perform on an array consists of looking for a particularelement. To assist you with this, the Array class is equipped with the Find() method. Its syntax is:

    Public Shared Function Find(Of T) ( _

    array As T(), _match As Predicate(Of T) _

    ) As T

    This generic method takes two arguments. The first argument is the name of the array that holds the

    elements. The second argument is a delegate that has the criterion to be used to find the element. Here

    is an example:

    Private Sub btnFind_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnFind.Click

    IDToFind = CInt(txtPersonID.Text)

    Dim pers As Person = Array.Find(People, AddressOf IDExists)

    If Not pers Is Nothing ThentxtFirstName.Text = pers.FirstName

    txtLastName.Text = pers.LastNamecbxGenders.Text = pers.Gender

    End IfEnd Sub

    Practical Learning: Finding an Element

    1. Access the CustomerTransactions.cs source code and change the Click event of the Findbutton as follows:

    Private Sub btnFind_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) HandlesbtnFind.Click

    ' AccountNumber = txtAccountNumber.Text

    ' Dim Accounts() As Customer =

  • 8/10/2019 An Array of Values

    30/32

    AccountsRecords.GetCustomers()

    ' If Array.Exists(Accounts, AddressOf AccountNumberExists)

    Then

    ' MsgBox("Good")

    ' Else

    ' MsgBox("There is no customer with that account number")' End If

    Dim TotalDeposits As Double = 0

    Dim TotalWithdrawals As Double = 0Dim TotalCharges As Double = 0Dim Balance As Double

    AccountNumber = txtAccountNumber.Text

    Dim Accounts() As Customer = AccountsRecords.GetCustomers()Dim Trans() As AccountTransaction = _

    AccountsRecords.GetTransactions()

    Dim AccountFound As Boolean = _Array.Exists(Accounts, AddressOf AccountNumberExists)

    If AccountFound = True ThenDim i As Integer = 1

    lvwTransactions.Items.Clear()

    For Each ActTrans As AccountTransaction In TransIf ActTrans.ProcessedFor = AccountNumber Then

    Dim lviTransactions As ListViewItem = New

    ListViewItem(i)

    lviTransactions.SubItems.Add( _ActTrans.TransactionDate.ToString("dd-MMM-yyyy"))

    If ActTrans.ProcessedBy = 0 ThenlviTransactions.SubItems.Add("")

    Else

    lviTransactions.SubItems.Add(ActTrans.ProcessedBy)

    End If

    lviTransactions.SubItems.Add( _ActTrans.TransactionType.ToString())

    If ActTrans.DepositAmount = 0 ThenlviTransactions.SubItems.Add("")

    ElselviTransactions.SubItems.Add( _

    ActTrans.DepositAmount.ToString("F"))

    End If

    If ActTrans.WithdrawalAmount = 0 ThenlviTransactions.SubItems.Add("")

    Else

    lviTransactions.SubItems.Add( _ActTrans.WithdrawalAmount.ToString("F"))

    End If

  • 8/10/2019 An Array of Values

    31/32

    If ActTrans.ChargeAmount = 0 Then

    lviTransactions.SubItems.Add("")

    Else

    lviTransactions.SubItems.Add( _

    ActTrans.ChargeAmount.ToString("F"))

    End If

    lvwTransactions.Items.Add(lviTransactions)

    i = i + 1End If

    Next

    For Each lviTransaction As ListViewItem In

    lvwTransactions.ItemsIf lviTransaction.SubItems(4).Text "" Then

    TotalDeposits = TotalDeposits + _

    CDbl(lviTransaction.SubItems(4).Text)End If

    If lviTransaction.SubItems(5).Text "" ThenTotalWithdrawals = TotalWithdrawals + _

    CDbl(lviTransaction.SubItems(5).Text)End If

    If lviTransaction.SubItems(6).Text "" Then

    TotalCharges = TotalCharges + _

    CDbl(lviTransaction.SubItems(6).Text)End If

    Next

    Balance = TotalDeposits - (TotalWithdrawals + TotalCharges)

    txtTotalDeposits.Text = FormatNumber(TotalDeposits)txtTotalWithdrawals.Text = FormatNumber(TotalWithdrawals)

    txtTotalCharges.Text = FormatNumber(TotalCharges)txtBalance.Text = FormatNumber(Balance)

    Else

    MessageBox.Show("There is no customer with that accountnumber")

    End IfEnd Sub

    2. In the Class Name combo box, select btnClose

    3. In the Method Name combo box, select Click and implement the event as follows:

    Private Sub btnClose_Click(ByVal sender As Object, _

    ByVal e As System.EventArgs) _Handles btnClose.Click

    Close()

    End Sub

    4. Execute the application to test it

  • 8/10/2019 An Array of Values

    32/32