Results 1 to 6 of 6

Thread: Appending to an array

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Posts
    13

    Appending to an array

    I have an address book that has three arrays declared in a code module:

    Code:
    Module ArrayModule
        Public mstrAddressArray(9), mstrCityArray(9), mstrPhoneArray(9) As String
    End Module
    I need to update/append these arrays when I add a new contact.
    Code:
    mstrNewContact = InputBox("Enter the name of the new contact", "Add Contact")
    I call another form to input the new contact information. I use text boxes to add info. Here is where my problem is. I need to append to the existing array but am unsure how to do that.

    This code
    Code:
    ReDim Preserve mstrAddressArray(mintNewContacts)
            mstrAddressArray(mintNewContacts) = txtAddress.Text
    places whatever I type into the txtAddress.text

    The code I test to find out the value of mstAddressArray works and displays whatever I typed into txtAddress.text.
    Code:
    lblOutput.Text = mstrAddressArray(mintNewContacts)
    This tells me it is being written to the array, however when I click on a name in my list box in my Address Form after I have added a new contact, and click on show details button I get the error "index was outside the bounds of the arrray."

    Any help is greatly appreciated.

    Thanks in advance.

  2. #2
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    is MintNewContacts always a bigger number than the upper bound of your array?

    try:
    VB Code:
    1. lblOutput.Text = mstrAddressArray(mintNewContacts-1)

    Personally, I would Redim like this:

    VB Code:
    1. Redim Preserve mstrAddressArray(ubound(mstrAddressArray)+1))

    Then store it in:

    VB Code:
    1. mstrAddressArray(ubound(mstrAddressArray)-1)) = txtAddress.Text

    I don't know if that's what you need or not... your question was a little fuzzy... or maybe it's a long afternoon
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  3. #3
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Also, and this is just personal preference, I would declare a UDT instead of a module array... (although I have to say I don't know much about module arrays as you have defined it.)
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Posts
    13
    Thanks for the quick responce

    I'll try to clarify with code.

    I open the file and read in a txt file with the following comma delimited data:

    Joe Green,1045 Green Street,Las Vegas Nevada 87465,485-876-3344
    Diana Estep,5968 Howard Drive,Houston Texas 23498,298-989-2354
    Roberta Rosenberger,876 West Jefferson,Maderia Beach Florida 48765,576-349-8738
    Randy Raney,198 Jackson Drive,St Louis Missouri 34875,314-564-9923


    VB Code:
    1. Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpen.Click
    2.         Dim objStreamReader As System.IO.StreamReader
    3.         Dim strLineofData As String
    4.         Dim strFields(1) As String
    5.         Dim intCtr As Integer
    6.         Dim chrDelimiter() As Char = {System.Convert.ToChar(",")}
    7.  
    8.         With dlgOpenFileDialog
    9.             .Filter = "text(*.txt) |*.txt"
    10.             .FileName = "Sales.txt"
    11.             .InitialDirectory = Application.StartupPath & ("\..\")
    12.             .ShowDialog()
    13.  
    14.         End With
    15.  
    16.         objStreamReader = System.IO.File.OpenText(dlgOpenFileDialog.FileName)
    17.         Do While objStreamReader.Peek <> -1
    18.             strLineofData = objStreamReader.ReadLine
    19.             strFields = strLineofData.Split(chrDelimiter)
    20.             lstAddress.Items.Add(strFields(0))
    21.             mstrAddressArray(intCtr) = strFields(1)
    22.             mstrCityArray(intCtr) = strFields(2)
    23.             mstrPhoneArray(intCtr) = strFields(3)
    24.             intCtr += 1
    25.         Loop
    26.  
    27.         objStreamReader.Close()
    28.         mnuShow.Enabled = True
    29.         mnuModify.Enabled = True
    30.     End Sub


    A list box is loaded with name of the contacts. When I highlite a name and click Show it displays the Contact Details on another form.

    VB Code:
    1. Private Sub mnuShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuShow.Click
    2.         Dim objDetailsForm As New frmDetails
    3.         Dim strAddress, strCity, strPhone As String
    4.  
    5.         If lstAddress.SelectedIndex = -1 Then
    6.             MessageBox.Show("No entry was selected to view", "View Error", MessageBoxButtons.OK)
    7.         End If
    8.         'Exit Sub
    9.  
    10.         strAddress = mstrAddressArray(lstAddress.SelectedIndex)
    11.         strCity = mstrCityArray(lstAddress.SelectedIndex)
    12.         strPhone = mstrPhoneArray(lstAddress.SelectedIndex)
    13.  
    14.         objDetailsForm.lblDetails.Text &= strAddress & ControlChars.NewLine
    15.         objDetailsForm.lblDetails.Text &= strCity & ControlChars.NewLine
    16.         objDetailsForm.lblDetails.Text &= strPhone & ControlChars.NewLine
    17.  
    18.  
    19.         objDetailsForm.Show()
    20.     End Sub

    Now when I want to add a New Contact to the list.

    VB Code:
    1. Private Sub mnuAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAdd.Click
    2.         Dim objNewContactForm As New frmNewContact
    3.         mstrNewContact = InputBox("Enter the name of the new contact", "Add Contact")
    4.         lstAddress.Items.Add(mstrNewContact)
    5.         objNewContactForm.lblContact.Text = "Contact information for " & mstrNewContact
    6.         objNewContactForm.Show()
    7.     End Sub

    Which open an inputbox and then a third form to add the New Contact details.

    VB Code:
    1. Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    2.  
    3.         ReDim Preserve mstrAddressArray(UBound(mstrAddressArray) + 1)
    4.         mstrAddressArray(UBound(mstrAddressArray) - 1) = txtAddress.Text
    5.  
    6.         ReDim Preserve mstrCityArray(UBound(mstrCityArray) + 1)
    7.         mstrCityArray(UBound(mstrCityArray) - 1) = txtCityStateZip.Text
    8.  
    9.         ReDim Preserve mstrPhoneArray(UBound(mstrPhoneArray) + 1)
    10.         mstrPhoneArray(UBound(mstrPhoneArray) - 1) = txtPhoneNumber.Text
    11.  
    12.  
    13.         'Me.Close()
    14.     End Sub

    I need to add new names and details for the contact to the arrays, and also be able to delete names from the listbox and array and then sort the index (which I havn't started yet, just trying to get the names and details to add to the array first)


    My first 8 project for this .NET class went by with no problems. It is these damn arrays where I am getting stuck.

    This is project 9 of 10 (almost finished)

  5. #5

  6. #6
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    ah ha! Forgot to tell me it was .NET! Sorry... don't know that just yet
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width