Results 1 to 7 of 7

Thread: const or static?

  1. #1

    const or static?

    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

  2. #2
    in thinBasic len returns the length of a string, in your example use SizeOf instead

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    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
    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

  4. #4
    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

  5. #5
    @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

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    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
    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

  7. #7
    thank you petr, I've understood this macro command. I will prefer "%myConstant = 5" for my converting example (from freebasic to thinbasic). bye, largo

Similar Threads

  1. static universe ?
    By danbaron in forum Science
    Replies: 7
    Last Post: 08-01-2012, 21:34
  2. STATIC elements in UDT
    By ErosOlmi in forum thinBasic vaporware
    Replies: 11
    Last Post: 12-05-2009, 07:29

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •