View previous topic :: View next topic |
Author |
Message |
njepsen
Joined: 13 Aug 2007 Posts: 469
|
Posted: Mon May 16, 2022 6:49 am Post subject: Constants and #if |
|
|
I'm trying to compare a constant with a variable and having trouble.
The help for const says " CONST symbol = expression " and gives a numeric example that creates a const with const x = (b*3)+2 , but I have not been able to assign a string constant that way. if any part is a var. Perhaps its not possible?
for example :
const fred = "fre" + "d" 'this compiles
but
myvariable = "d"
const fred = "fre" + myvariable 'this does not compile, I assume because my variable can be changed after compilation ?
This code doesnt work.
Code: |
$regfile = "m1284pdef.dat"
$crystal = 9830400
$framesize =1500
$hwstack = 400 '
$swstack = 400
const site_ph_number = "+64 xx yyy zzzz"
const echo_site = "+64aabbccddd"
dim tempstr as string * 20
'i want to compare these two phone numbers.
'I want to use spaces in the 1st ph number because without spaces, its real easy
'to make a typo and enter a mistake. Spaces make the number more readable.
tempstr = site_ph_number
delchars tempstr,32
#if echo_site = tempstr
'do stuff
#endif
|
(BASCOM-AVR version : 2.0.8.5 ) _________________ Neil |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Mon May 16, 2022 8:12 am Post subject: Re: Constants and #if |
|
|
njepsen wrote: |
Code: |
#if echo_site = tempstr
'do stuff
#endif
|
|
Ask yourself about the sense of conditional compilation and why you would use it in a place, where regular use of an IF/THEN command is due.
Conditional compilation means, that every code between the '#' tags is either existent in flash or completely missing, depending the logical result of '#if' at compile-time.
Why anyone in the world with the intention to compare more than one string would want such?
Or do you not understand what conditional compilation is used for? |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Mon May 16, 2022 9:38 am Post subject: |
|
|
Hi MWS
Njepsen is not a beginner, if he pauses the question it is because he needs this feature.
Indeed I asked myself the question : Why this step?
So Niepsen would you explain the needed ?
JP
_________________ pleasure to learn, to teach, to create |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Mon May 16, 2022 12:20 pm Post subject: |
|
|
Duval JP wrote: | Njepsen is not a beginner, | It is a beginner-like question.
Quote: | if he pauses (poses) the question it is because he needs this feature. | If he understands how conditional compilation works, then he can't want this feature, as it's an impossible one.
njepsen wrote: | I assume because my variable can be changed after compilation | He assumes right.
It's an hen/egg problem.
How would the compiler know whether to include code at compile-time, if the conditions for the inclusion are only set later in run-time?
This rule applies to every creation of a constant, Njepsen implied it would work with numeric values, which is not true.
It only looks alike because his quote of the help-example is wrong or incomplete, from the help:
Code: | Const B1 = &B1001
Const X =(b1 * 3) + 2 | To resolve a constant, anything included into an equation needs to be a constant itself. |
|
Back to top |
|
|
njepsen
Joined: 13 Aug 2007 Posts: 469
|
Posted: Tue May 17, 2022 6:27 am Post subject: |
|
|
Hello MWS and thank you for your reply. As usual, your reply is very helpful and on the money, but wrapped in an abrasive wrapper.
I DO understand how conditional compilation works, but had missed the point that Quote: |
To resolve a constant, anything included into an equation needs to be a constant itself. |
and had overlooked the fact that a var in the equation is of course not evaluated until run time, not compile time.
Why do i use constants and conditional compilation you ask? I have 100 or so units in the field all of which use IDENTICAL code, but some do slightly different things and some for example have an on board raspberry Pi and others don't. Some units have cameras, some have different instrumentation so CC allows them to function as required, all with the same main code body, and a different setups in a list of const in the code header.
Code: |
code
const has_pi = 1
const has_camera = 1
const has weather=0
const site_phone = "+64 1234567"
'etc
'main body
#if has_pi = 1
' do lots of things
' not compiling this code save me about 20% of code space, and makes a BIG difference to compile time
#endif
|
MWS you need to be careful that members are afraid to ask a question that you might consider a "beginners question" and get harpooned by your caustic response. I am not a beginner, but I am allowed to ask a beginners question from time to time, and am not afraid to do so.
As a parting shot - I do value your expertise. _________________ Neil |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Tue May 17, 2022 9:02 am Post subject: |
|
|
njepsen wrote: | but wrapped in an abrasive wrapper. |
As usual. Either stand it or ignore my message.
Quote: | I DO understand how conditional compilation works, but had missed the point that |
I was rather stunned that you did not recognize the logical quandary that the compiler needs information at compile-time, which he only can get later at run-time.
Quote: | Why do i use constants and conditional compilation you ask? |
JP asked that and added the 'beginner'
Of course I asked myself too, but rather why your post did not first explain the exact purpose, instead of presenting an unusable solution.
Quote: | Code: | const has_pi = 1
const has_camera = 1
const has weather=0
const site_phone = "+64 1234567" |
| This is well understood, but still does not explain your initial question, which was something about working with strings.
I will give some random hints:
Code: | const fred = "fre" + myvariable | does not work.
Code: | dim fStr As String * 30
const site_phone_cc = "+64"
const site_phone_phn = "1234567"
const site_phone = site_phone_cc + " " + site_phone_phn
const fred = "fre" + site_phone_phn
fStr = site_phone
#IF fred = "fre1234567"
' inlcude code
#ENDIF | Instead of applying vars to constants, take the other way around and assign a var's initial value by a combination of constants.
Also notice that the command VAREXIST() - despite its name - works also for constants, this allows for checking if a constant or variable was declared at all.
Commenting out 'site_phone' may exclude then the phone code from being compiled in.
Quote: | MWS you need to be careful that members are afraid to ask a question that you might consider a "beginners question" and get harpooned by your caustic response. | I grew too old to care if my answer is seen as caustic or not.
Also I tend to serve as it comes in, means adapt my response on how good the question is done.
For your case: you're in the forum since 15 years, not only you did ask a beginner's question, but you have asked beginner-like.
Beginners often describe not what they exactly want, instead present an unsuitable solution, while the reader's first riddle to solve is: what does the TO want at all?
Had you described in your top post exactly what you want, we'd be already further.
Also it seems you took offense from the description 'beginner', which - as well you acted alike - you are not. Your self esteem should be raised enough and you should be mentally sufficiently hardened to simply ignore that.
In contrary, what about if I would tell a beginner he asks beginner questions?
Well, as this would be only obvious, would I do that? Likely no, as his level would show.
Quote: | and am not afraid to do so. | Good, that's the proper attitude to withstand my answers
Quote: | I do value your expertise. | Thanks. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|