Home > Mobile >  Variable shown as not declared in VB.NET
Variable shown as not declared in VB.NET

Time:11-28

I'm a beginner trying to declare some variables in a module to use them across multiple sub-procedures.

It gives me the error for frontPointer: Error BC30188 Declaration expected.

I've tried adding it as Public

Module Module1
    Public queue(10) As Integer
    Public frontPointer As Integer
    Public endPointer As Integer
    Public full As Integer
    Public length As Integer

    frontPointer = 1

    Sub Main()

    End Sub

End Module

And I've tried declaring it normally too

Module Module1
    Dim queue(10) As Integer
    Dim frontPointer As Integer
    Dim endPointer As Integer
    Dim full As Integer
    Dim length As Integer

    frontPointer = 1

    Sub Main()

    End Sub

End Module

Picture of error

CodePudding user response:

It's not telling you that the variable is not declared. It's telling you that, where you have that code, only declarations are allowed. Every code block at the type (class, module, structure) level has to be a declaration. This:

frontPointer = 1

is not a declaration. You need to either combine the assignment and the declaration of the variable you're assigning to:

Public frontPointer As Integer = 1

or else perform the assignment inside a method:

Sub Main
    frontPointer = 1

    '...
End Sub
  • Related