I have an assignment that asks me to create an application that lets the user enter a word or phrase, then displays the number of vowels found in that word or phrase.

Here's what I have so far:

Dim mystring As String = Me.txtenter.Text
        Dim chars As Char() = mystring.ToCharArray()

        Dim newString As System.Text.StringBuilder = New System.Text.StringBuilder()

        For Each c As Char In chars
            Dim strpattern As String
            strpattern = "[U,u,E,e,O,o,A,a,I,i]"
            Dim strstore As String
            strstore = c Like strpattern
            If strstore = True Then
                Dim intnumber As Integer = 0
                intnumber = intnumber + 1
            End If
        Next c

        newString.Append("There are " & intnumber & " vowels")

        Me.lblanswer.Text = newString.ToString()

I try to run it, but there happens to be a "blue wavy line" under the word "intnumber" in the line

newString.Append("There are " & [U]intnumber[/U] & " vowels")

It says "name intnumber is not declared".

May you show me a way to fix this? and by the way, is the code above correct, because i'm afraid I might mistake somewhere in the Loop.

Thanks for your time!

Recommended Answers

All 3 Replies

You are declaring intnumber inside the body of the loop. This is a matter of scope. Take a look at this

Public Class Car

    Dim mass As Integer

    Public Sub Drive()

        Dim speed As Integer

        For velocity As Integer = 1 To speed

            Dim force As Integer
            force = mass * velocity

        Next

    End Sub

End Class

In this class, the variable mass is declared outside of any method or property. It is said to be an instance member or field, but the basic point is that it is available (or in scope) within the entire body of the class.

Inside the Drive() method is the variable speed . It cannot be referenced by any other method or property, it is local to the Drive() method. If you had another method, maybe Brake() and tried to use speed, you would get a compilation error. Nothing would prevent you from declaring another variable named speed in the Brake() method, however.

In the loop, you actually have 2 new variables introduced. velocity , which is the loop variable in this case, and force . The scope of those variables are limited to the body of the loop. They have no meaning outside of the loop, and (as with speed outside of Drive()) if you try to use them outside of the loop, you get a compilation error.

Back to your code, the variable intnumber is not defined anywhere except for inside the loop. Trying to reference it once the loop is finished is not going to work. Further, it is being declared each time you are inside the loop, which means it goes out of scope when one iteration is finished (and you lose its value) and is being declared once again on the next iteration with a brand new initial value.

So what should you do?

Edit: Curse me for not paying attention! intnumber is actually declared inside the body of an If statement inside the loop. So its scope is not limited to the loop, but rather to the body if the If statement! It's of even narrower scope than mentioned.

Dim mystring As String = Me.txtenter.Text
        Dim chars As Char() = mystring.ToCharArray()
        Dim intnumber As Integer = 0
        Dim strnew As String
        Dim strstore2 As String

        For Each c As Char In chars
            Dim strpattern As String
            strpattern = "[U,u,E,e,O,o,A,a,I,i]"
            Dim strstore As String
            strstore = c Like strpattern
            If strstore = True Then
                intnumber = intnumber + 1
                strnew = "There are " & intnumber & " vowels."
                strstore2 = strnew
                Me.lblanswer.Text = strstore2.ToString()
            End If
        Next c

I just composed the above code. It actually works. May you revise it a little bit and let me know if my code is good!
Actually it takes me a long time to decide which line goes outside of the Loop, and which goes into the Loop. And even now, when the code does work, I still don't know why I have "this" outside of Loop, "that" inside the Loop.

Let's sort of mentally go through your code, starting with the beginning of the loop. Before we get there, let's assume my input is "orange banana" in your textbox.

So, we're at the loop. For each character in the string (there are 13), I'm going to do this code. So for the first character (o):

1. Declare strpattern, set it to "[U,u,E,e,O,o,A,a,I,i]"
2. Declare strstore as string
3. Set strstore equal to the boolean result of c Like strpattern 3a. Self: Why am I storing a boolean in a string variable?
4. Check to see if strstore is true
4a. (assumes true) Increment intnumber value
4b. (assumes true) Concatenate intnumber within a string and store it in strnew
4c. (assumes true) Store strnew in strstore2
4c-mynumberingsystemsucks. What's the point of strnew, then?
4d. (assumes true) Have label show the value of strstore2
5. Go the the next character.
6. Declare strpattern, set it to "[U,u,E,e,O,o,A,a,I,i]"
6a. Why am I declaring this variable with the same constant value all over again?
7. Declare strstore as string. Again.
8. Store a boolean result in a string variable. Again.
9. Test
9a. Incremenet...
9b. Store...
9c. Store again...
9d. Display again...
10. Back to top

...and so it continues.

Just thinking of how the loop is executing, how would you think to improve it? Some declarations and initializations should probably move to before the loop, right? Maybe change that pesky string variable to a boolean instead? And, of course, the display of the result should probably wait until the loop is finished.

Oh, for those keeping score at home, there are 6 vowels in "orange banana."

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.