0
votes

Essentially what I am trying to do is write a program where you input the person's name and their date of birth, store them to an array (note sure if I have to do two separate arrays) and then by entering their name into another text box and clicking another button I can get the date of birth back.

I know I have to include an if-loop, I know how to declare an array.

I am guessing that I maybe need to use a select case.

Here is the code:

Public Class Form1
Dim Name(5) As String
Dim DOB(5) As String
Dim i As Integer
Private Sub btnNameEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNameEnter.Click

    Name(i) = txtInputName.Text
    DOB(i) = txtInputDOB.Text

    If i = 6 Then
        MsgBox("You cannot enter any more names.")
    End If

    For i = 1 To 5
        Name(i) = i
        txtInputName.Clear()
        txtInputDOB.Clear()
    Next i

End Sub

Private Sub btnFindDOB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindDOB.Click
    Select Case Name(i)
        Case Is Name(1)
            MsgBox("Date of birth: " & DOB(1))
        Case is Name(2)
            MsgBox("Date of birth: " & DOB(2))
        Case is Name(3)
            MsgBox("Date of birth: " & DOB(3))
        Case is Name(4)
            MsgBox("Date of birth: " & DOB(4))
        Case is Name(5)
            MsgBox("Date of birth: " & DOB(5))

    End Select
End Sub
End Class

I would show a picture of the program but visual basic keeps deleting it for some reason and doesn't let me re add the buttons.

Here is an image of the error list: http://gyazo.com/2489a307f4a8e2d9ce65aa2ad79b04f1 ScreenShot-error-list )


Public Class Form1 Dim DOB(5) As String Dim i As Integer Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click Dim Name(5) As String

    Dim Counter As Integer
    txtNameInp.Text = Name(i)
    txtDOBInput.Text = DOB(i)

    Counter = 0

    For i = 1 To 5
        If Counter = 6 Then
            MsgBox("You can only enter 5 names and DOBs")
        End If
        Name(i) = txtNameInp.Text
        DOB(i) = txtDOBInput.Text
        Counter = Counter + 1
    Next i
End Sub

Private Sub btnFindDOB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindDOB.Click
    Select Case Name(i)
        Case Name(1)
            MsgBox(DOB(1))
        Case Name(2)
            MsgBox(DOB(2))
        Case Name(3)
            MsgBox(DOB(3))
        Case Name(4)
            MsgBox(DOB(4))
        Case Name(5)
            MsgBox(DOB(5))
    End Select
End Sub

End Class

Here's an error: http://gyazo.com/487290c3e523003fe58f82a15fdf6faa - this one occurs when I try and enter the second number

2

2 Answers

2
votes

It is hard to be to precise using a picture of the errors instead of comments in the code indicating the location and text of the error, but from what I can see:

  • Forms already have a Name property and you are trying to redefine it via your array. use something like myNames instead.
  • You have a module level variable named i and a local one as well. Change the global one to something useful like curPosition. This will track with element you are currently storing to.
  • If you want to stop adding to the array at 6 you need to do so in code. Add Exit Sub after the MsgBox; you want to do this before you store anything to the arrays, not after. Maybe add a message saying - 'now I will tell you the DOB for a name`.
  • If you want to be clever (doubtful) have both a 'Guess Name' and Guess DOB button on the form and report the Name for a given DOB. Its basically the same code.
  • the button enter code will run every time they click so you do not need a loop. This is plain nonsense by the way: Name(i) = i you are overwriting everything with whatever i happens to be. Just add to myNames and myDOB using curPosition as the index and then increment it:

    myNames(curPosition) = txtInputName.Text

  • The relational error is from this: Case Is Name(1)

The error is telling you that it expects a relational operator, which Is is not (Is is used with Objects, which a string is not). The code is also a bit wonky here because you are not controlling the value if i. To compare txtInputName:

Select Case txtInputName.Text   
    Case = Name(1)       ' ie does txtInputName match the value in Name(1)?
      ... 

- Rather than a case statement, you could use a For Loop here for less code.


Lets learn about classes. Rather than arrays which will dissociate related pieces of info from each other a class will keep the data together.

Public Class Person

    Public property Name As String
    Public Property DOB As DateTime       ' I refuse to store a Date as string

    Public Sub New(sName as String, dt as DateTime)
       Name = sName
       DOB = dt
    End Sub
End Class

Next, a nifty list to hold some person objects. A list is like an array but smarter:

Private pList As New List(Of Person)

Add a person:

' ToDo: check that the date is valid using TryParse instead
' this exercise is left to the student
Dim p As New Person(txtInputName.Text, Datetime.Parse(txtInputDOB.Text))
plist.Add(p)

Find a person:

' case insensitive search
Dim search As String = txtNameToFind.Text.ToLowerInvariant

For Each p as Person In pList
    If P.Name.TolowerInvariant = search Then
        txtDOB.Text = p.DOB.ToString
        Exit For
    End If
Next 
0
votes

Try this:

Public Class Form1
    Dim Name, DOB As List(Of String) 'Lists are better in this situation.

    Private Sub btnNameEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNameEnter.Click
        'You don't need to specify the index in lists.
        Name.Add(txtInputName.Text)
        DOB.Add(txtInputDOB.Text)

        txtInputName.Clear()
        txtInputDOB.Clear()
    End Sub

    Private Sub btnFindDOB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindDOB.Click
        'You get the index of the name you are searching for and get DOB for this index.
        MsgBox("Date of birth: " & DOB.Item(Name.IndexOf(txtInputName.Text)))
    End Sub
End Class