1 Replies - 389 Views - Last Post: 05 December 2012 - 07:03 PM Rate Topic: -----

#1 Elsy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-December 12

Comparing a string entry to an array with no duplicates

Posted 05 December 2012 - 05:38 PM

Hello all,
First time poster, please forgive any faux pas.

"...code for a program that asks the user for ten words. The program alerts the user if they enter a word that they have entered before and does not count it against the ten."

This is what I have so far. I can accept and display the numbers, but I cannot figure out how to make the comparison work with an array. The subject matter is 'arrays,' so one is necessary.

Public Class Form1
    Dim WORDS(0 To 9) As String
    Dim holding As String
    Dim Comp As Short

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

    End Sub

    Private Sub Input_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Input.Click

        Dim i As Short
        Dim Output As String

        For i = 0 To UBound(WORDS)
            holding = InputBox("Words, please.")
            Comp = (StrComp(holding, WORDS(i)))
            If Comp = 0 Then
                MsgBox("Try another word")
            Else : WORDS(i) = holding
            End If
        Next

        Output = Join(WORDS, ", ")

        MsgBox(Output)

    End Sub
    Private Sub Check()
        ' For i = 0 To UBound(WORDS)
        ' If holding = WORDS(i) Then
        'InputBox("try a different word")
        ' Else
        ' WORDS(i) = holding
        ' End If
        'Next

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Close()
    End Sub

End Class





There are two different attempts here- the one to use 'StrComp' and the 'Check' Sub. Any advice would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Comparing a string entry to an array with no duplicates

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Comparing a string entry to an array with no duplicates

Posted 05 December 2012 - 07:03 PM

View PostElsy, on 05 December 2012 - 06:38 PM, said:

Hello all,
First time poster, please forgive any faux pas.

"...code for a program that asks the user for ten words. The program alerts the user if they enter a word that they have entered before and does not count it against the ten."

This is what I have so far. I can accept and display the numbers, but I cannot figure out how to make the comparison work with an array. The subject matter is 'arrays,' so one is necessary.

There are two different attempts here- the one to use 'StrComp' and the 'Check' Sub. Any advice would be appreciated.

I'd go with the code in the Sub, though I'd either put it in-line right after the InputBox, or call it as a Function right after that. StrComp is Visual Basic, not VB.Net.

The reason it isn't working is that you need to compare all the existing words in the array with the one you just got from the InputBox.

Think "A loop within a loop".

This post has been edited by lar3ry: 05 December 2012 - 08:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1