Author |
Message |
Grogster
Admin Group
Joined: 31/12/2012 Location: New ZealandPosts: 9066 |
Posted: 02:57pm 24 Jul 2017 |
Copy link to clipboard |
Print this post |
|
Hi there.
I am trying to tidy up a program, and wanted to put all the CONST commands inside a sub, then call that, so you have three or four SUB's that are called at the top of the program, that setup all the system stuff.
Whenever I do this, it fails and none of the CONST take effect, so the system falls over. NONE of the CONST get declared, even though the SUB exits fine. All the Const inside the SUB.
If I revert back to the code where they are all declared at the top of the code(IE: not inside a sub), then everything works fine.
Can you not declare CONST inside a SUB? I thought you could do pretty much anything you wanted inside a SUB..... Smoke makes things work. When the smoke gets out, it stops! |
|
MicroBlocks
Guru
Joined: 12/05/2012 Location: ThailandPosts: 2209 |
Posted: 03:22pm 24 Jul 2017 |
Copy link to clipboard |
Print this post |
|
The whole point about a CONST is that you collect them at the top of the program so they are not scattered around and it is a lot more maintainable. If those values are only used in the subroutine itself then you have to consider using hardcoded values instead (if they never change (constant)) or use LOCAL variables at the top of the subroutine to hold those values.
Microblocks. Build with logic. |
|
robert.rozee Guru
Joined: 31/12/2012 Location: New ZealandPosts: 2290 |
Posted: 03:25pm 24 Jul 2017 |
Copy link to clipboard |
Print this post |
|
it does sound like CONST is treated as a local command. as a workaround, you could tuck away the constants at the end of your code with a RETURN and the end of them, and then call that block of code with GOSUB.
cheers, rob :-) |
|
Grogster
Admin Group
Joined: 31/12/2012 Location: New ZealandPosts: 9066 |
Posted: 03:42pm 24 Jul 2017 |
Copy link to clipboard |
Print this post |
|
Hello chaps.
I do not care, I just thought is was neater - to me.
Having said that, I am happy to leave them at the top of the code, I was just curious. Smoke makes things work. When the smoke gets out, it stops! |
|
VK2MCT Senior Member
Joined: 30/03/2012 Location: AustraliaPosts: 120 |
Posted: 03:48pm 24 Jul 2017 |
Copy link to clipboard |
Print this post |
|
Cludgy but works (Gosub would only be called once anyway),
const test1 = 1 goto listofconst:
resumemain: print test1 print test2 end
listofconst: const test2 = 2 goto resumemain:
John BEdited by VK2MCT 2017-07-26 |
|
flip Senior Member
Joined: 18/07/2016 Location: AustraliaPosts: 114 |
Posted: 05:10pm 24 Jul 2017 |
Copy link to clipboard |
Print this post |
|
Within a procedure, if you're happy to keep scope limited to within the subroutine, (and the space will be restored when exiting) use LOCAL AS STRING scName="Constant" , and make sure you don't set the value anywhere in your procedure...note scName...sc=String Constant, and Name is the meaningful name. (this is just one of thousands of methods...the other ideas are really good too)...just depends what your priorities are...
EDIT: sorry - re-read your question - I missed the point my point answers a slightly different question.. Regards, Phil
Edited by flip 2017-07-26 |
|