>Howdy all, maybe I'm missing something in my knowledge of Structures but I
can't
>seem to figure out how to make an array of structures (or maybe I want a
>structure of arrays?).


I think maybe you want a Structure of Structures (SOS... get it? sorry...)

Seriously, I would use nested structures here because then you can access
any user's preferences directly.  An array of structures wouldn't let you do
this because you would have to loop through the array to find the struct
relating to a given users.

Thankfully, I love using nested structures, so I am well suited to help you.
The only thing that you need is some sort of unique identifier for each
user... the concatenation of CFID and CFTOKEN *should* work, but I've never
tried it.

<!-- appliction.cfm --->
<!--- init the top level struct. Need to lock access to application
vars --->
<CFLOCK NAME="#Application.Name#" TYPE="exclusive">
    <CFIF NOT IsDefined("Application.stcUserPrefs")>
        <CFSET Application.stcUserPrefs = StructNew()>
    </CFIF>
    <!--- now see if this user already has a pref struct in memory --->
    <!--- the Key value must be unique to each user --->
    <CFSET strKey = CFID & CFTOKEN>
    <CFIF NOT StructKeyExists(Application.stcUserPrefs, strKey)>
        <!--- create a blank/default pref struct for user --->
        <CFSET stcThisPref = StructNew()>
        <CFSET StructInsert(stcThisPref, "Language", "")>
        <CFSET StructInsert(stcThisPref, "Font", "")>
        <!--- add it to top level using the unique key --->
        <CFSET StructInsert(Application.stcUserPrefs, strKey, stcThisPref)>
    <CFELSE>
        <!--- user already has a pref struct in memory, do nothing --->
    </CFIF>
</CFLOCK>

<!--- some_other_file.cfm --->
<CFSET strKey = CFID & CFTOKEN>

<!--- retrieving the pref struct --->
<CFLOCK NAME="#Application.Name#" TYPE="readonly">
    <!--- create stcPrefs that contains the user's prefs --->
    <CFSET stcPrefs = StructCopy(Application.stcUserPrefs[strKey])>
</CFLOCK>

<!--- updating a user's prefs --->
<!--- first make changes to the local stcPrefs ---.
<CFSET stcPrefs.Language = "new language">
<CFSET stcPrefs.Font = "new font">
<!--- now save to the application structure --->
<CFLOCK NAME="#Application.Name#" TYPE="exclusive">
    <CFSET Application.stcUserPrefs[strKey] = StructCopy(stcPrefs)>
</CFLOCK>

<!--- removing a user's prefs --->
<CFLOCK NAME="#Application.Name#" TYPE="exclusive">
    <CFSET StructDelete(Application.stcUserPrefs, strKey)>
</CFLOCK>

<!--- getting a count of users --->
<CFLOCK NAME="#Application.Name#" TYPE="readonly">
    <CFSET intCount = StructCount(Application.stcUserPrefs)>
</CFLOCK>

Lets say you have 3 users with IDs "ABC", "DEF", and "XYZ".  You will end up
with the following:
Application.stcUserPrefs["ABC"].Language
Application.stcUserPrefs["ABC"].Font

Application.stcUserPrefs["DEF"].Language
Application.stcUserPrefs["DEF"].Font

Application.stcUserPrefs["XYZ"].Language
Application.stcUserPrefs["XYZ"].Font

If you were to use an array of structs then you would have to know which
array element a given user's preferences lie in before you can access the
prefs.  Using the SOS method all you need to know is their ID.

I hope that makes some sense.  If you need any clarification please let me
know.

Regards,
Seth Petry-Johnson
Argo Enterprise and Associates


------------------------------------------------------------------------------
Archives: http://www.eGroups.com/list/cf-talk
To Unsubscribe visit 
http://www.houseoffusion.com/index.cfm?sidebar=lists&body=lists/cf_talk or send a 
message to [EMAIL PROTECTED] with 'unsubscribe' in the body.

Reply via email to