-2

This is a homework assignment for Visual Basic (using Visual Studio 2013). This is my first question on this site (or any site).

I am trying to make a phone directory that utilizes lists. The directory should be able to search, add and delete contacts.

On my form, I have a list box that has names already entered through the collection property. I have a label that outputs the corresponding phone number. I have added 10 names and 10 phone numbers. (phone numbers were added with a list - at design time)

At run time, the user should be able to click the "add contact" button to add another contact to the list. I am using two input boxes; first prompts for the name to add and the second prompts for the phone number.

First contact that I try to add, seems to be working out just fine. My issue is when I try to add a second contact to the list, the phone number from the first add changes to reflect the same number that is being added for the second add. (looks like second contact phone number is overriding what I put for the first add) Just to be clear, when the program runs, I start out with 10 names in a list box and 10 numbers that can only be viewed if you click on the name in the list box. When I added the first one, it made the count 11 and then adding the second one, the count went to 12. The phone numbers are being added to a list (not list box).

I do not receive any error messages, just the information is not getting put into the list correctly (the list that is similar to an array).

Here is the code I have:

Let me know if I need to be more clear on what the issue is.

FYI: I just tested it again before submitting my question. I am just adding a name and a one digit number just to test it. When I enter first added contact, I used the number 5. Added second contact, I used the number 6. When I check at that point, the first contact showed a 6 and the second contact showed a 6. However, I tested doing 3 added contacts. Same number for the first add (5), same number for the second add (6) and used 7 for the third added contact. When I checked the results, first added contact changed to 7, second contact stayed at 6 and third contact shows 7. I have no idea what is going on??

Option Explicit On
Option Strict On

'=========== Class mainForm =================
Public Class mainForm

    Const MAX_SUBSCRIPT_Integer As Integer = 9

    Dim inputNameString As String                'user contact name input
    Dim inputPhoneString As String               'user phone number input

    Private phoneList As New List(Of String) 

    '============== mainForm_Load ====================

    Private Sub mainForm_Load(ByVal sender As Object, _
         ByVal e As System.EventArgs) Handles Me.Load
        splashForm.ShowDialog()

        phoneList.Add("555 - 266 - 9563")
        phoneList.Add("555 - 266 - 5461")
        phoneList.Add("555 - 266 - 7412")
        phoneList.Add("555 - 266 - 5642")
        phoneList.Add("555 - 266 - 6721")
        phoneList.Add("555 - 266 - 1465")
        phoneList.Add("555 - 266 - 3541")
        phoneList.Add("555 - 266 - 2874")
        phoneList.Add("555 - 266 - 9114")
        phoneList.Add("555 - 266 - 2245")

    End Sub

    '======= namesListBox_SelectedIndexChanged() =====================
    Private Sub namesListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles namesListBox.SelectedIndexChanged

        If namesListBox.SelectedIndex >= 0 Then
            phoneNumberLabel.Text = (phoneList(CInt(namesListBox.SelectedIndex.ToString())))
            Dim count As Integer
            contactPictureBox.Image = contactImageList.Images(namesListBox.SelectedIndex)
            count += 1

        End If

    End Sub

    '============== addButton_Click =====================

    Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click

        inputNameString = (InputBox("Enter name of Contact. Ex. Doctor: Dr. Sigmund Freud ", "Add Contact"))
        inputPhoneString = (InputBox("Enter contact phone number. Ex. 555-555-1212", "Add Contact Phone Number"))
        namesListBox.Items.Add(inputNameString.ToString())
        phoneList.Add(inputPhoneString.ToString())
        AddElementToPhoneString(inputPhoneString)

    End Sub

    '============== AddElementToPhoneString() ========================

    Public Sub AddElementToPhoneString(ByVal stringToAdd As String)

        phoneList(MAX_SUBSCRIPT_Integer + 1) = stringToAdd
        Dim countInteger As Integer
        countInteger = phoneList.Count()
        countLabel.Text = CStr(CInt(phoneList.Count))

    End Sub

End Class
David Wilson
  • 4,369
  • 3
  • 18
  • 31
Dawn Marie
  • 13
  • 4
  • 1
    Rather than storing the name in a List and the number in a UI control, your list could hold both pieces of info. Related data should be kept together. See [this answer](http://stackoverflow.com/a/34164458) for an example – Ňɏssa Pøngjǣrdenlarp May 09 '16 at 01:02
  • @Plutonix I had an idea of how I wanted my program and kind of ran with it. If you seen my GUI you might understand. I will check out the link you gave me. This is my first semester of programming so I am still a newbie. Thank you – Dawn Marie May 09 '16 at 02:03
  • @Plutonix do you know if I need the constant MAX_SUBSCRIPT_Integer since the list resize themselves? – Dawn Marie May 09 '16 at 02:07
  • Look this might be your first question but its just not good enough. You have a massive block of text. Don't. Just stick to the actual problem at hand. You have a massive block of code. Don't. Just include the failing code. Which leads to the next problem. You don't even know what part of your code is the problem. Don't. Take time to figure out where the problem lies before you ask. And to top it all off, you don't have an actual question, just an implied one. `I have no idea what is going on??` is NOT a question. – JK. May 09 '16 at 02:17
  • Thank god! A new programmer that uses Option Strict On and Option Explicit On! You'll go far with that excellent attitude! @Plutonix - Surely that alone deserves an upvote? ;) – David Wilson May 09 '16 at 10:36
  • @DavidWilson Smiles to you! By the way... how do I upvote? First time on here as a participant :) – Dawn Marie May 09 '16 at 16:22
  • Hehe. I was suggesting to plutonix that he up votes your question. One of his(and my and any decent programmer's) pet hates is people not setting those two to On. So many new programmers take the easy route and have them off.. It allows them to develop very bad habits. You can up vote an answer or a question by clicking on the icon above the number to the left of the item .. Though I think you might need a minimum reputation to do this - not sure tho. – David Wilson May 09 '16 at 16:30
  • I see the arrow next to PhillipXT below but not next to Plutonix. Anyways, have an awesome day @David :) – Dawn Marie May 09 '16 at 16:35
  • @DavidWilson Thanks for appreciating that by the way. My teacher had us doing that from day one (Option Strict On and Option Explicit On) I can see where people could get into trouble if they are not using it. – Dawn Marie May 09 '16 at 16:45
  • You can tell him/her from me. Thanks! – David Wilson May 09 '16 at 16:50
  • @DavidWilson I sure will – Dawn Marie May 09 '16 at 16:55

2 Answers2

1

Looks like there might be a few issues, but here's the one that's tripping you up right now:

phoneList(MAX_SUBSCRIPT_Integer + 1) = stringToAdd

You have MAX_SUBSCRIPT_Integer set as a constant, so you're always adding item 10 to the list, no matter how many items you've added. You should be doing something like:

phoneList.add(stringToAdd)

That will ensure you're always adding a new item to the list, instead of overwriting the last item.

  • Thank you for your comments. However, I did figure out how to fix it. I will upload the edited version. I guess I need to set up an email for getting answers because I kept checking my email instead of here. I am still working thru this program and right now I am working on the delete function, which is also not working the way it is coded. I got it to quit crashing but now the code doesn't seem to do anything as far as removing a contact from the list box. Do I need to re-post as another question? – Dawn Marie May 09 '16 at 01:55
  • It would be better to post a new question for a different problem, yes. –  May 09 '16 at 01:58
  • I fixed the problem... I was using Remove and changed it to RemoveAt and now it is working :) Now off to work on the search contact textbox... – Dawn Marie May 09 '16 at 02:10
0

This is the fix to adding a contact issue: I removed AddElementToPhoneString completely. I thought it was doing something productive but it was actually causing the problem.

Option Explicit On Option Strict On

'=========== Class mainForm ================= Public Class mainForm

Const MAX_SUBSCRIPT_Integer As Integer = 9

Dim inputNameString As String                'user contact name input
Dim inputPhoneString As String

Private phoneList As New List(Of String) 'user phone number input

'============== mainForm_Load ====================

Private Sub mainForm_Load(ByVal sender As Object, _
     ByVal e As System.EventArgs) Handles Me.Load
    splashForm.ShowDialog()

    phoneList.Add("555 - 266 - 9563")
    phoneList.Add("555 - 266 - 5461")
    phoneList.Add("555 - 266 - 7412")
    phoneList.Add("555 - 266 - 5642")
    phoneList.Add("555 - 266 - 6721")
    phoneList.Add("555 - 266 - 1465")
    phoneList.Add("555 - 266 - 3541")
    phoneList.Add("555 - 266 - 2874")
    phoneList.Add("555 - 266 - 9114")
    phoneList.Add("555 - 266 - 2245")

End Sub

'======= namesListBox_SelectedIndexChanged() ====================
Private Sub namesListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles namesListBox.SelectedIndexChanged

    If namesListBox.SelectedIndex >= 0 Then
        phoneNumberLabel.Text = (phoneList(CInt(namesListBox.SelectedIndex.ToString())))
        Dim count As Integer
        contactPictureBox.Image = contactImageList.Images(namesListBox.SelectedIndex)
        count += 1

    End If

End Sub

'============== addButton_Click =====================

Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click

    inputNameString = (InputBox("Enter name of Contact. Ex. Doctor: Dr. Sigmund Freud ", "Add Contact"))
    inputPhoneString = (InputBox("Enter contact phone number. Ex. 555-555-1212", "Add Contact Phone Number"))
    namesListBox.Items.Add(inputNameString.ToString())
    phoneList.Add(inputPhoneString.ToString())

End Sub

End Class

Dawn Marie
  • 13
  • 4