CustomProperties some clarity please
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
CustomProperties some clarity please
So, Custom Properties :
Am I right/wrong in believing that :
-1- We can use CustomProperties (CPs) in lieu of Global Variables and, in fact, it is BETTER to use CPs than Globals (in which case WHY please ?)
-2- Whilst CPs may be changed at run time their 'state' when closing a MOBILE app will NOT be saved (just like a Global) because of write access rules to the application folder prohibiting data writes ?
-3- CP's WILL be saved on closing DESKTOP apps (Mac, Windows, Linux) ?
-4- If CPs are to be used for application preference / persistent data (language choice, date formats etc) then these CPs will need to be 'written out' to a preferences file (or somesuch) before app. close/exit and 'read in' on app. startup ? (If so, is this just a simple file create/write/read operation ?)
A little basic I'm sure but...
Thanks
Am I right/wrong in believing that :
-1- We can use CustomProperties (CPs) in lieu of Global Variables and, in fact, it is BETTER to use CPs than Globals (in which case WHY please ?)
-2- Whilst CPs may be changed at run time their 'state' when closing a MOBILE app will NOT be saved (just like a Global) because of write access rules to the application folder prohibiting data writes ?
-3- CP's WILL be saved on closing DESKTOP apps (Mac, Windows, Linux) ?
-4- If CPs are to be used for application preference / persistent data (language choice, date formats etc) then these CPs will need to be 'written out' to a preferences file (or somesuch) before app. close/exit and 'read in' on app. startup ? (If so, is this just a simple file create/write/read operation ?)
A little basic I'm sure but...
Thanks
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1
Re: CustomProperties some clarity please
The main reason for not using globals is because it is easy to accidentally reuse the the same variable name later and that can be very difficult to diagnose. It can also make reading the code harder if you don't know the code in it's entirety. I actually break the rule, and make one global array for larger apps named tMainArray. I then can save local variables to elements of the global array when I need a global variable. The only drawback to this is that I need to make a "cheat sheet" so I can remember which element is which variable. The benefit of keeping them in one array for me outweighs that drawback. I can then simply write the array to a text file and I have effectively saved the stack. Then on openstack if their is a text file I have it read the file and write the elements back to the appropriate variables and fields. Of course I'm not much more than a newbie myself, so if some of the more experienced users make a better suggestion, listen to them. Using custom properties instead of globals prevents the accidentally duplicated name, but to me is just as hard to follow when you come back a year later so I still need the "cheat sheet". They are great for things like variables that determine the state of a button that toggles, because it can be a custom property of that same button.
Also, custom properties are not saved in desktop apps either (in the standalone version). The only way to do that is to have a standalone launch a separate un-compiled .rev file.
Also, custom properties are not saved in desktop apps either (in the standalone version). The only way to do that is to have a standalone launch a separate un-compiled .rev file.
Last edited by magice on Wed Mar 06, 2013 5:22 pm, edited 1 time in total.
Re: CustomProperties some clarity please
Hi Trax,
1. there is no BETTER in this case, although many developers like me prefer CPs over GLOBALs.
Maybe because they:
a. do not need to be declared and
b. they will (CAN) be saved with the stack
2. and 3. NO standalone can save itself, NEVER!
4. Yes, just a simple file operation! Example:
Best
Klaus
1. there is no BETTER in this case, although many developers like me prefer CPs over GLOBALs.
Maybe because they:
a. do not need to be declared and
b. they will (CAN) be saved with the stack
2. and 3. NO standalone can save itself, NEVER!
4. Yes, just a simple file operation! Example:
Code: Select all
on closestack
put specialfolderpath("documents") & "/MyAppFolder/the_prefs.txt" into tPrefsFile
put the cWhatEverCustomProp of this stack into url("file:" & tPrefsFile)
...
end closestack
on preopenstack
put specialfolderpath("documents") & "/MyAppFolder/the_prefs.txt" into tPrefsFile
## App has been opened before:
if there is a file tPrefsFile then
set the cWhatEverCustomProp of this stack to url("file:" & tPrefsFile)
end if
...
end preopenstack
Klaus
Re: CustomProperties some clarity please
Magice & Klaus
Thanks for clearing this up for me... I'm definitely a newbie to LC but beginning to REALLY like it ! Hardest thing for me is remembering NOT to apply C, VB, .NET principles and syntax. I have yet to deploy a LC app to anything BUT I'm getting there. Another couple of weeks and I'm hopeful to try and deploy across Mac, Windows, Android and iOS... then the fun will start !
Again, thanks both. Appreciated.
Regards.
Thanks for clearing this up for me... I'm definitely a newbie to LC but beginning to REALLY like it ! Hardest thing for me is remembering NOT to apply C, VB, .NET principles and syntax. I have yet to deploy a LC app to anything BUT I'm getting there. Another couple of weeks and I'm hopeful to try and deploy across Mac, Windows, Android and iOS... then the fun will start !
Again, thanks both. Appreciated.
Regards.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1
-
- VIP Livecode Opensource Backer
- Posts: 10052
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: CustomProperties some clarity please
There are many opinions about global variables, but most programming languages include them for a reason. There's a useful role for both custom properties and global variables:Traxgeek wrote:-1- We can use CustomProperties (CPs) in lieu of Global Variables and, in fact, it is BETTER to use CPs than Globals (in which case WHY please ?)
Custom properties can be useful when you need persistence, or when the data is related to the object their bound to.
Global variables can be a good choice for data you don't want to survive beyond the session, or when performance is critical.
No application can modify its state on disk, per OS restrictions.-2- Whilst CPs may be changed at run time their 'state' when closing a MOBILE app will NOT be saved (just like a Global) because of write access rules to the application folder prohibiting data writes ?
-3- CP's WILL be saved on closing DESKTOP apps (Mac, Windows, Linux) ?
To save objects with custom properties, use a separate stack file from the one you build the standalone with.
See the Dictionary entries for the create and clone commands for how to create a stack file.
Personally, I love using stack files for data storage. So convenient, so efficient.
For details on saving data from a standalone, see:
http://livecodejournal.com/tutorials/sa ... ution.html
The save command will save any stack that is not in a read-only state (that is, not the standalone, doesn't have its read-only bit set in the OS, and doesn't have its cantModify property set to true).-4- If CPs are to be used for application preference / persistent data (language choice, date formats etc) then these CPs will need to be 'written out' to a preferences file (or somesuch) before app. close/exit and 'read in' on app. startup ? (If so, is this just a simple file create/write/read operation ?)
You may also want to look at the specialFolderPath function in the dictionary, and these supplemental notes on using it:
http://www.sonsothunder.com/devres/live ... ile010.htm
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: CustomProperties some clarity please
Hey, Thanks Richard.
The stack method sounds right up my street for my particular purpose of option persistence between sessions.
I'm just firing up the tutorial.
Thanks again !
The stack method sounds right up my street for my particular purpose of option persistence between sessions.
I'm just firing up the tutorial.
Thanks again !
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1