Is Define$ legit vs Define.s ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
chikega
User
User
Posts: 34
Joined: Fri Dec 04, 2020 3:19 am

Is Define$ legit vs Define.s ?

Post by chikega »

I was playing with PB and explicitly declaring variables of String type with 'Define.s' :

Code: Select all

Define.s user_input, q = "q"
Then by chance I changed Define.s to Define$, and it works,

Code: Select all

Define$ user_input, q = "q"
but I couldn't find Define$ in the help files and was wondering whether Define$ is a legitimate keyword. Thanks!

My complete code sample:

Code: Select all

If OpenConsole()
EnableExplicit

Define$ user_input, q = "q"

Repeat 
  Print("> ");
  user_input = Input();
  PrintN("You entered '" + user_input + "'")
Until user_input = "q"

EndIf
Gary E Chike DMD MS
'Experience is what you get when you don't get what you want' Image
Allen
User
User
Posts: 92
Joined: Wed Nov 10, 2021 2:05 am

Re: Is Define$ legit vs Define.s ?

Post by Allen »

Hi,

I found this information in the help file under 'Varible' that may be helpful .

Notation of string variables: it is possible to use the '$' as last char of a variable name to mark it as string. This way you can use 'a$' and 'a.s' as different string variables. Please note, that the '$' belongs to the variable name and must be always attached, unlike the '.s' which is only needed when the string variable is declared the first time.

a.s = "One string"
a$ = "Another string"
Debug a ; will give "One string"
Debug a$ ; will give "Another string"
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Is Define$ legit vs Define.s ?

Post by BarryG »

chikega wrote: Tue Jan 25, 2022 1:20 amI couldn't find Define$ in the help files and was wondering whether Define$ is a legitimate keyword
A quick test seems to confirm it's supported and works:

Code: Select all

Define$ test
test="does it work?"
Debug test
User avatar
chikega
User
User
Posts: 34
Joined: Fri Dec 04, 2020 3:19 am

Re: Is Define$ legit vs Define.s ?

Post by chikega »

Thanks Allen, that's how I understand it as well concerning string variables. But, I was zeroing in on the 'Define' keyword vs variables. Affixing an '$' suffix to a keyword like 'Define' (vs variable) is not in any of the documentation. Very interesting I thought. :D

Thanks BarryG for verifying! PureBasic is full of surprises .. in a good way :)
Gary E Chike DMD MS
'Experience is what you get when you don't get what you want' Image
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Is Define$ legit vs Define.s ?

Post by skywalk »

For human and automation ease of reading your code, it is better to stick with Define/Protected.x instead of Define/Protected$.
And, I recommend string variables keep the trailing $.

Ex. Define.s myString$
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
chikega
User
User
Posts: 34
Joined: Fri Dec 04, 2020 3:19 am

Re: Is Define$ legit vs Define.s ?

Post by chikega »

Totally agree skywalk! But what are your thoughts on:

Code: Select all

Define$ myString$
vs

Code: Select all

Define.s myString$
We are perplexed that the keyword Define is accepting the '$' suffix. '$' is normally tagged onto variables.
Gary E Chike DMD MS
'Experience is what you get when you don't get what you want' Image
Post Reply