How Do I fill in this Structure String Array in vbNET?

333 views Asked by At

Visual studio tells me the variable must be declared even though it already is.

I filled in a structured array in a similar way using a loop though the type was an Int. I do not want to use a loop this time just hard code it.

Structure Sentence
    Dim strWord As String
End Structure

Dim strArticles(1) As Sentence

strArticles(0).strWord = "The"

Thanks

1

There are 1 answers

4
codechurn On BEST ANSWER

Are you defining the Structure in your method body? It must be defined outside of a method, either in a module ore a class. See this example.

This works just fine:

Module Module1

    Sub Main()
        Dim s = New Sample()
        s.DoIt()
    End Sub

End Module
Class Sample
    Structure Sentence
        Dim strWord As String
    End Structure

    Public Sub DoIt()
        Dim strArticles(1) As Sentence
        strArticles(0).strWord = "The"
        Console.WriteLine(strArticles(0).strWord)
    End Sub
End Class