-1

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

Jon Q
  • 63
  • 1
  • 1
  • 7

1 Answers1

0

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
codechurn
  • 3,870
  • 4
  • 45
  • 65
  • It's not in a method it's in the class. – Jon Q Nov 14 '14 at 00:58
  • Thanks, that works. How come before I didn't have to use a Module, was it because it was in a loop triggered by an event? – Jon Q Nov 14 '14 at 01:17
  • @JonQ Just accept it as the answer and rep I shall receive :) – codechurn Nov 14 '14 at 01:18
  • @JonQ - To use a Structure (or anything) in a piece of code, that piece of code **must** have access to the **instance** of that structure, and must aswell access that structure (declaration) in order to know what's the type of the manipulated instance. Check if the structure were declared Private for example. Failing to grant access will force VS to try to _infer_ the type if `Option Infer` is `On` (by default ! - you save time doing so, but your code is more error prone) If Visual Studio is mistaken in the instance Type, you can get a compile error or worse, a runtime casting exception. – Karl Stephen Nov 14 '14 at 16:03