|
-
Nov 26th, 2003, 12:53 PM
#1
Thread Starter
New Member
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.
-
Nov 26th, 2003, 01:41 PM
#2
Frenzied Member
is MintNewContacts always a bigger number than the upper bound of your array?
try:
VB Code:
lblOutput.Text = mstrAddressArray(mintNewContacts-1)
Personally, I would Redim like this:
VB Code:
Redim Preserve mstrAddressArray(ubound(mstrAddressArray)+1))
Then store it in:
VB Code:
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
-
Nov 26th, 2003, 01:42 PM
#3
Frenzied Member
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.)
-
Nov 26th, 2003, 03:11 PM
#4
Thread Starter
New Member
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:
Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpen.Click
Dim objStreamReader As System.IO.StreamReader
Dim strLineofData As String
Dim strFields(1) As String
Dim intCtr As Integer
Dim chrDelimiter() As Char = {System.Convert.ToChar(",")}
With dlgOpenFileDialog
.Filter = "text(*.txt) |*.txt"
.FileName = "Sales.txt"
.InitialDirectory = Application.StartupPath & ("\..\")
.ShowDialog()
End With
objStreamReader = System.IO.File.OpenText(dlgOpenFileDialog.FileName)
Do While objStreamReader.Peek <> -1
strLineofData = objStreamReader.ReadLine
strFields = strLineofData.Split(chrDelimiter)
lstAddress.Items.Add(strFields(0))
mstrAddressArray(intCtr) = strFields(1)
mstrCityArray(intCtr) = strFields(2)
mstrPhoneArray(intCtr) = strFields(3)
intCtr += 1
Loop
objStreamReader.Close()
mnuShow.Enabled = True
mnuModify.Enabled = True
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:
Private Sub mnuShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuShow.Click
Dim objDetailsForm As New frmDetails
Dim strAddress, strCity, strPhone As String
If lstAddress.SelectedIndex = -1 Then
MessageBox.Show("No entry was selected to view", "View Error", MessageBoxButtons.OK)
End If
'Exit Sub
strAddress = mstrAddressArray(lstAddress.SelectedIndex)
strCity = mstrCityArray(lstAddress.SelectedIndex)
strPhone = mstrPhoneArray(lstAddress.SelectedIndex)
objDetailsForm.lblDetails.Text &= strAddress & ControlChars.NewLine
objDetailsForm.lblDetails.Text &= strCity & ControlChars.NewLine
objDetailsForm.lblDetails.Text &= strPhone & ControlChars.NewLine
objDetailsForm.Show()
End Sub
Now when I want to add a New Contact to the list.
VB Code:
Private Sub mnuAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAdd.Click
Dim objNewContactForm As New frmNewContact
mstrNewContact = InputBox("Enter the name of the new contact", "Add Contact")
lstAddress.Items.Add(mstrNewContact)
objNewContactForm.lblContact.Text = "Contact information for " & mstrNewContact
objNewContactForm.Show()
End Sub
Which open an inputbox and then a third form to add the New Contact details.
VB Code:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
ReDim Preserve mstrAddressArray(UBound(mstrAddressArray) + 1)
mstrAddressArray(UBound(mstrAddressArray) - 1) = txtAddress.Text
ReDim Preserve mstrCityArray(UBound(mstrCityArray) + 1)
mstrCityArray(UBound(mstrCityArray) - 1) = txtCityStateZip.Text
ReDim Preserve mstrPhoneArray(UBound(mstrPhoneArray) + 1)
mstrPhoneArray(UBound(mstrPhoneArray) - 1) = txtPhoneNumber.Text
'Me.Close()
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)
-
Nov 26th, 2003, 03:15 PM
#5
Please post .Net questions in the VB.Net forum. I don't know if ober5861's code applies to .Net.
-
Nov 26th, 2003, 03:16 PM
#6
Frenzied Member
ah ha! Forgot to tell me it was .NET! Sorry... don't know that just yet
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|