Evan
Evan

Reputation: 1713

For Each loop element not declared

I need to use VB within the framework of SSRS. I am trying to beautify some XPaths in order to make them more presentable to a human user of the report. I haven't gone very far and I hit the first problem.

Module VBModule
    Public Function BeautifyXpath(ByVal s As String) As String
        Dim strBuilder As New System.Text.StringBuilder(s)
        If s.Contains("[") Then
            strBuilder.Replace("[%s]", "")
            strBuilder.Replace("@", "")
            Dim tokens = strBuilder.ToString().Split("/")
            For Each token In tokens
                Console.WriteLine(token)
            Next
            Return strBuilder.ToString()
        Else : Return s
        End If
    End Function

    Sub Main()
        Console.WriteLine(BeautifyXpath("/priorities/priority[%s]/@patternName"))
    End Sub
End Module

I get the following error from the compiler:

(8,24) : error VBNC30451: 'token' is not declared. It may be inaccessible due to its protection level.

Upvotes: 0

Views: 2044

Answers (1)

Mymozaaa
Mymozaaa

Reputation: 474

You must declare token in your For Each statement.

For Each token As String In tokens
    Console.WriteLine(token)
Next

Or

Dim token As String()
For Each token In tokens
    Console.WriteLine(token)
Next

See MSDN for more information.

Upvotes: 2

Related Questions