Posted by & filed under Developer Blog.

PROBLEM  Today I ran into a rather perplexing bug in Flash when I tried to create/write to a shared object. I needed to create an array named after an ID and then store 3 values into that array. The idea is that each ID would have its own array with its own 3 values. Since the name of the array would depend on which ID we were dealing with, I resorted to using the set variable command in order to use a variable as the name of the array like this:

myData = sharedobject.getLocal(“myData”, “/”);
set(“myData.data.”+ID, new Array());
eval(“myData.data.”+ID)[0] = value1;
eval(“myData.data.”+ID)[1] = value2;
eval(“myData.data.”+ID)[2] = value3;

But to my dismay, not only was nothing being written to the shared object, the shared object wasn’t even being created!

SOLUTION:  The solution that I came up with isn’t great, but it works. I figured out through much trial and error that unless something else is written to the shared object without using Set Variable, Set Variable won’t work. Not only that but if you set, for example:

myPersonalData.data.test = true

you will only be able to write to the shared object a single time because next time, since myPersonalData.data.test is already set to true, nothing changes. In order to work around this I used the following solution:

myData = sharedobject.getLocal(“myData”, “/”);
myData.data.lastIDStored=ID;
set(“myData.data.”+ID, new Array());
eval(“myData.data.”+ID)[0] = value1;
eval(“myPersonalData.data.”+ID)[1] = value2;
eval(“myData.data.”+ID)[2] = value3;

The reason this works so well is because if I’m creating a new array, I’m sure the ID has never been used before and therefore will never be the last ID stored.

The problem only exists when creating a new array, not for changing the array, so if the last ID is the same as the new ID it won’t matter.