[Gambas-user] Correct strategy for VB static var conversion to Gambas?

Benoît Minisini g4mba5 at gmail.com
Sat May 29 22:09:13 CEST 2021


Le 29/05/2021 à 21:25, John Anderson a écrit :
> On converting larger codebase from VB6 to Gambas,  I wanted to double 
> check my method for converting "static" type variable module declares 
> into the Gambas way.  This might be obvious to everyone, but I just 
> wanted to be sure.  Or am I smoking the wrong stuff again?
> [...]

I didn't allow to declare static class variable inside functions, like 
you can do in C (and other languages).

I think it's mainly for the same reason I prevented declaring local 
variables anywhere in the code.

After some pressure from people I won't give the names, I accepted to 
allow local variable declaration anywhere.

Now I regret. It makes the code so ugly for me! :-)

So maybe I could allow declaring "static variables" (actually private 
global class variables) inside functions in the future.

In the meantime, you have to use the wokraround you described. Something 
like that:

Public Sub MyFunction()

   Private (or 'Static' or something else?) somevar As String = "first"

   ...

End

becomes:

Static Private MyFunction_somevar As String = "first"

Public Sub MyFunction()

   ...

End

One question remains: does the "static" 'somevar' be actually static or 
not? I.e. is there only one 'somevar' variable for all the process, or 
one 'somevar' variable for each object of the class. How to make the 
difference?

> 
> 'Gambas module file
> 
> 'Static-like vars declared here
> Private myLVar As Long = 0
> Private myArray[20, 500] As Integer

One remark about that last line: don't do that.

Don't use the syntax myArray[20, 500], because it creates an embedded 
array which is a special beast that is mainly useful for interfacing 
with C code.

Use a "normal" array instead (which is a standard Gambas object):

Private myArray As New Integer[20, 500]

Regards,

-- 
Benoît Minisini


More information about the User mailing list