PDA

View Full Version : const or static?



largo_winch
22-08-2012, 18:06
general question: "const" and "static" commands are nearly equal? I've tried to convert a code example from freebasic. but the result for thinbasic is not correct I suppose. anyone has an idea where's my error?



' Empty GUI script created on 08-22-2012 15:35:52 by (thinAir)

Uses "console"

Static a As Long 'CONST
a = 1
Static b As Double
b = 2
Static c As Byte
c = 3

MsgBox 0, Str$(a) & Str$(Len(a)) ' 1 1
MsgBox 0, Str$(b) & Str$(Len(b)) ' 2 1
MsgBox 0, Str$(c) & Str$(Len(c)) ' 3 1

Print a + Len(a) ' 1 1
Print b + Len(b) ' 2 1
Print c + Len(c) ' 3 1

WaitKey

' -------------------
' result should be:
' 1 4
' 2 8
' 3 1
' -------------------

bye, largo

jack
23-08-2012, 00:51
in thinBasic len returns the length of a string, in your example use SizeOf instead

Petr Schreiber
23-08-2012, 09:00
Hi,

Jack is correct, you should use SizeOf instead for variable size in bytes.

The Static and Const is not the same!

Const
Declares variable, which can be assigned only once - a constant. The value is assigned during the declaration:


Const myConstant As Long = 5

myConstant = 5 ' -- This is tollerated, the assigned value is the same as the original one
myConstant = 6 ' -- This will cause run-time error - myConstant has been initialized to 5, you cannot change it later!


The Const variable is always visible in space it is declared in. If it is declared in global space (outside functions), it is global, but if it is declared in function, it is visible only in the function. This is the difference from the constants created using %, as is the only choice in some simpler BASICs.

Static
Static variables should be used in functions, in global space they make not much sense and Global variables would be better choice.
The variable declared as static has one special ability - it remembers its value after function call is finished. It can be used to neat tricks, but it can be also cause of confusion if used improperly. Comparison of Local and Static is in the code below.


Uses "console"

' -- Program start
Function TBMain()

Local i As Long

PrintL "3 consecutive calls to WithLocal"
For i = 1 To 3
PrintL "Returned " + WithLocal()
Next

PrintL

PrintL "3 consecutive calls to WithStatic"
For i = 1 To 3
PrintL "Returned " + WithStatic()
Next

PrintL

PrintL "Press any key to quit"
WaitKey

End Function

' -- Functions
Function WithLocal() As Long
Local a As Long
a += 1

Return a
End Function

Function WithStatic() As Long
Static a As Long
a += 1

Return a
End Function



Petr

largo_winch
23-08-2012, 17:15
thanks jack and petr for your example, sizeof is the right equivalent for my converting example.



' Empty GUI script created on 08-23-2012 14:41:16 by (thinAir)

Uses "console"

Function TBMain() As Long
Dim v As Long
For v = 1 To 5
myMessage()
my2Message()
Next v
End Function

Sub myMessage()
Static x As Long
'Dim Message(1 To 5) As Static Asciiz * 256
Static Message(5) As Asciiz * 256
Incr x
Message(x) = "Prometheus =" + Str$(x)
MsgBox 0, Message(x)
End Sub

Sub my2Message()
Local x As Long
Local Message(5) As Asciiz * 256
Incr x
Message(x) = "ODIN =" + Str$(x)
MsgBox 0, Message(x)
End Sub

here's a simple static array example. bye, largo

largo_winch
24-08-2012, 10:36
@petr: how I can convert your "CONST" example to powerbasic?


Const myConstant As Long = 5

myConstant = 5 ' -- This is tollerated, the assigned value is the same as the original one
'myConstant = 6 ' -- This will cause run-time error - myConstant has been initialized to 5, you cannot change it later!




a) with ENUM... END ENUM (I suggest this could be the right solution)

b) %equates = 1000

c) select case as static
...
end select

I've found not the right solution for it and asked where I have to use it working with powerbasic?

thank you for help. bye, largo

Petr Schreiber
24-08-2012, 15:01
Hi Largo,

PowerBASIC does not offer constants with local scope. For global scope constants you can use the:


%myConstant = 5


...or, as the PB help file suggests, create CONST macro:


MACRO CONST = MACRO

CONST a = 5&


This is the closest match, but it must be used outside functions...


Petr

largo_winch
27-08-2012, 11:34
thank you petr, I've understood this macro command. I will prefer "%myConstant = 5" for my converting example (from freebasic to thinbasic). bye, largo