Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Help with case statement and constants

Author
Message
Death_fox
14
Years of Service
User Offline
Joined: 18th Apr 2012
Location:
Posted: 8th Nov 2012 21:06
yo, i been trying to use a variable for a case statement but didn't work, i did some reseach and found out that constants do work with case statments but when i try i just returned the "CASE needs a literal parameter" error

here's my code.



thinking about it, it might be the way i'm declearing the constants so heres there declaration as well.



the Create_block function returns a integer
The Weeping Corpse
14
Years of Service
User Offline
Joined: 19th Sep 2011
Location: United Kingdom
Posted: 8th Nov 2012 22:40 Edited at: 8th Nov 2012 23:12
You can't place executable code within a constant declaration

#constant mytest = 1 is fine

#constant mytest myfunction(....) is no no in darkbasic



WAIT, yes you can, I'm just very tired lol.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 9th Nov 2012 00:20
The parameter used in a case statement must be a literal - as the compiler is telling you.

If you use a constant as the parameter then that must be a literal too at compile time. Like this:



Remember that #constants are replaced by their defined code before the program is compiled for syntax checking etc. It is a simple text replacement as far as the compiler is concerned.

Hence my snippet looks like this when finally compiled:



#constants are purely a device to aid the programmer.
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 9th Nov 2012 00:21
#constant isn't evaluated as you might expect, it's really a pre-processor. So the text gets injected into source code in place of the constant name.


So this bit of mock up code...



become this, after the replacements have been made AIR and other constants.





Now even though the expression Create_block("Air",0,1,0,false might appear to be solvable at compile time, DBpro doesn't support this. So the compiler is seeing the Create_block function call returns a dynamic integer result, which is not compatible with the CASE statement, which only supports literal.

Death_fox
14
Years of Service
User Offline
Joined: 18th Apr 2012
Location:
Posted: 9th Nov 2012 01:19
thanks for the detail explanation kevin, i'm going to seperate the function from the constant but is there any why to bypass this out of curiosity

Login to post a reply

Server time is: 2026-07-09 01:50:37
Your offset time is: 2026-07-09 01:50:37