Can FB create arrays of constant values? [YES]

New to FreeBASIC? Post your questions here.
elsairon
Posts: 207
Joined: Jul 02, 2005 14:51

Can FB create arrays of constant values? [YES]

Postby elsairon » May 06, 2011 0:08

Quick test using a constant array yields error;

'Invalid assignment/conversion'.

I'm guessing FB does not support constant arrays?

Or am I using const keyword incorrectly?

The commented out lines produce errors.

Code: Select all

' test flags

' arrays can't be constant?

const F01 = 1, F02 = 2, F03 = 4, F04 = 8
const MIN_FLAG = 1, MAX_FLAG = 4
dim as integer FLAG_ARRAY ( MIN_FLAG to MAX_FLAG ) = { F01, F02, F03, F04 }
'const FLAG_ARRAY ( MIN_FLAG to MAX_FLAG ) = { F01, F02, F03, F04 }
'dim as const integer FLAG_ARRAY ( MIN_FLAG to MAX_FLAG ) = { F01, F02, F03, F04 }

sub set_flag _
    ( _
        byref flag as integer, _
        byref target as integer _
    )
   
    target = target or flag

end sub

function get_flag _
    ( _
        byref flag as integer, _
        byref target as integer _
    ) as integer
   
    if ( target and flag ) then return -1
    return 0
   
end function

dim as integer bit_field

set_flag( F02, bit_field )

dim as integer test_bit
for test_bit = MIN_FLAG to MAX_FLAG
    if get_flag( FLAG_ARRAY( test_bit ), bit_field ) then
        ? "Flag " & test_bit & " set."
    else
        ? "Flag " & test_bit & " not set."
    end if
next

sleep
Last edited by elsairon on May 06, 2011 3:44, edited 1 time in total.
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » May 06, 2011 0:36

elsairon, the commented code is fine. The problem is FreeBASIC has two meanings of "constant": one is the enum-like compile-time constant declared using CONST, and the other refers to a variable -- or in your case, an array -- where the datatype itself is said to be constant.

Your commented code declares an array of const integers, but you try to pass one to get_flag, which takes a non-const integer by reference. You can't convert from const to non-const in this way (or else the variable could be modified, and that would crash the whole const party), and hence the error message. Either change your flag parameter to be passed by value (BYVAL) or that it takes a reference to a const integer, like the following:

Code: Select all

function get_flag _
    ( _
        byref flag as const integer, _
        byref target as const integer _
    ) as integer
elsairon
Posts: 207
Joined: Jul 02, 2005 14:51

Postby elsairon » May 06, 2011 2:47

stylin wrote:Your commented code declares an array of const integers, but you try to pass one to get_flag, which takes a non-const integer by reference.


This makes it clear. I didn't think of checking how the parameters were passed, which is silly because as I wrote the example I thought, 'why are these both byref?' and then I dismissed the thought.

Thanks again.
Provoni
Posts: 393
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Can FB create arrays of constant values? [YES]

Postby Provoni » Jun 11, 2020 20:55

I'm trying this simple example but it doesn't work. What am I doing wrong?

Thanks

Code: Select all

screenres 640,480,32

const test(3)={1,2,3,4}

print test(2)

sleep
badidea
Posts: 2184
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Can FB create arrays of constant values? [YES]

Postby badidea » Jun 11, 2020 22:03

I don't think freebasic allows that syntax, was is allowed is:

Code: Select all

dim as const integer test(0 to 3) = {11, 22, 33, 44}
dim as const string label(0 to 3) = {"aa", "bb", "cc", "dd"}

print test(2)
'test(2) = 55 '<-- test this
print label(3)
'label(3) = "ff"

getkey

Not sure if there is a difference between two 2 lines below:

Code: Select all

const as integer x = 10
dim as const integer x = 10

The second form can be used for procedure variables as well, see https://freebasic.net/wiki/wikka.php?wa ... tQualifier.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Can FB create arrays of constant values? [YES]

Postby Tourist Trap » Jun 12, 2020 2:37

I think we can also have a static array of const:

Code: Select all

static as integer test(0 to 3) = {11, 22, 33, 44}



About CONST, it can guess the type so it's more like that I think:

Code: Select all

const A = 99         'I think means: var as const A = 99
fxm
Posts: 10058
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Can FB create arrays of constant values? [YES]

Postby fxm » Jun 12, 2020 5:11

Do not confuse:
- Const (the keyword) when it is used to define a constant data which remains at the compiler level only (replaced by its value during the compilation phase),
- and Const (the qualifier) when used to qualify the declaration (with Dim) of a real variable (for the execution phase) but with a constant value (read only).

FreeBASIC does not support arrays of compilation constants.
Provoni
Posts: 393
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Can FB create arrays of constant values? [YES]

Postby Provoni » Jun 12, 2020 5:34

fxm wrote:Do not confuse:
- Const (the keyword) when it is used to define a constant data which remains at the compiler level only (replaced by its value during the compilation phase),
- and Const (the qualifier) when used to qualify the declaration (with Dim) of a real variable (for the execution phase) but with a constant value (read only).

FreeBASIC does not support arrays of compilation constants.

I get it now.

Thanks, so another way to read the topic would be "Can FB create arrays of read-only values? [YES]".

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 9 guests