1

I'm playing around with gambas.

This code gives me the error "unexpected dim in FMain.class:6"

Public Sub Form_Open()

  Print "this won't work"

  Dim nickname As String = "gambas"

  Print "Your new name is " & nickname

End

This code doesn't, and runs fine:

Public Sub Form_Open()

  Dim nickname As String = "gambas"

  Print "Your new name is " & nickname  

End

Does gambas have requirements where variables are declared like pascal? I can't find any mention of it in the documentation. Thanks.

Dai
  • 141,631
  • 28
  • 261
  • 374
CMR
  • 11
  • 1

2 Answers2

1

Gambas requires all DIM statements to be placed before any executable code inside a function or Subroutine (emphasis mine):

http://gambaswiki.org/wiki/lang/dim

All DIM declarations must be in the FUNCTION or SUB before the first executable command.

So change your code to this:

Public Sub Form_Open()

  Dim nickname As String = "gambas"

  Print "this will work"

  Print "Your new name is " & nickname

End

Gambas' requirement for forward declaration of all local variables is very old-school. Sometimes it does make it easier to make self-documenting code and it incentivizes making functions short, but if a function has many intermediate short-lived local variables that cannot be immediately initialized (e.g. inside nested loops inside a function) then it hinders readability. YMMV.

Dai
  • 141,631
  • 28
  • 261
  • 374
0

This is not required anymore since Gambas 3.12.

But I suggest to continue declaring variables at the top function. It makes the code far more readable two years later.

Benoît
  • 44
  • 1