in thinBasic len returns the length of a string, in your example use SizeOf instead
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?
bye, largo' 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 ' -------------------
in thinBasic len returns the length of a string, in your example use SizeOf instead
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:
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.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!
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
Learn 3D graphics with ThinBASIC, learn TBGL!
Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB
thanks jack and petr for your example, sizeof is the right equivalent for my converting example.
here's a simple static array example. bye, largo' 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
@petr: how I can convert your "CONST" example to powerbasic?
a) with ENUM... END ENUM (I suggest this could be the right solution)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!
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
Hi Largo,
PowerBASIC does not offer constants with local scope. For global scope constants you can use the:
...or, as the PB help file suggests, create CONST macro:%myConstant = 5
This is the closest match, but it must be used outside functions...MACRO CONST = MACRO CONST a = 5&
Petr
Learn 3D graphics with ThinBASIC, learn TBGL!
Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB
thank you petr, I've understood this macro command. I will prefer "%myConstant = 5" for my converting example (from freebasic to thinbasic). bye, largo
Bookmarks