Const error (Expected End-of-Line)

General FreeBASIC programming questions.
Post Reply
VANYA
Posts: 1839
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Const error (Expected End-of-Line)

Post by VANYA »

Hi all!

I'm getting a compilation error in this code:

Code: Select all

const s = !"\9\32"
? s[0]
error 3: Expected End-of-Line, found '[' in '? s[0]'
fxm
Moderator
Posts: 12181
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Const error (Expected End-of-Line)

Post by fxm »

The string index operator ('[]') applies only to references.
Otherwise in your case, instead of:
s[n]
you can use:
asc(s, n+1)

See the Operator [] (String index) documentation page.
VANYA
Posts: 1839
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Const error (Expected End-of-Line)

Post by VANYA »

clear , fxm thanks!
fxm
Moderator
Posts: 12181
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Const error (Expected End-of-Line)

Post by fxm »

Operator [] (String index)
.....
Usage:
result = lhs [ rhs ]
.....
Parameters:
lhs : The string (a string reference, not a string returned as local copy).
rhs : A zero-based offset from the first character.
.....

Example:

Code: Select all

Function returnByval(Byref s As String) As String
    Return s
End Function

Function returnByref(Byref s As String) Byref As String
    Return s
End Function

Dim As String s = " "

'Print returnByval(s)[0]  '' error 17: Syntax error
Print Asc(returnByval(s), 1)
Print
Print returnByref(s)[0]
Print Asc(returnByref(s), 1)

Sleep
VANYA
Posts: 1839
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Const error (Expected End-of-Line)

Post by VANYA »

a string reference, not a string returned as local copy
The explanation in the help for this case is not very clear. But after you explained, it is now clear.
fxm
Moderator
Posts: 12181
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Const error (Expected End-of-Line)

Post by fxm »

VANYA wrote: Apr 19, 2024 11:39
a string reference, not a string returned as local copy
The explanation in the help for this case is not very clear. But after you explained, it is now clear.

Small update:
Operator [] (String index)
.....
Usage:
result = lhs [ rhs ]
.....
Parameters:
lhs : The string (a string reference, not a string returned as local copy)
lhs : The string variable or a string reference (not for example a string returned as a local copy).
rhs : A zero-based offset from the first character.
.....
VANYA
Posts: 1839
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Const error (Expected End-of-Line)

Post by VANYA »

lhs : The string variable or a string reference (not for example a string returned as a local copy).
Maybe you need to write that constants are not supported for the indexing operation?
Because I personally find this technical definition difficult to understand. I'm afraid to imagine what new users will understand.
fxm
Moderator
Posts: 12181
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Const error (Expected End-of-Line)

Post by fxm »

New update:
Operator [] (String index)
.....
Usage:
result = lhs [ rhs ]
.....
Parameters:
lhs : The string (a string reference, not a string returned as local copy)
lhs : The string variable or a string reference (not for example a constant or a string returned as a local copy).
rhs : A zero-based offset from the first character.
.....
VANYA
Posts: 1839
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Const error (Expected End-of-Line)

Post by VANYA »

New update:
Ok! Thanks!
Post Reply