Expand my Community achievements bar.

How Can I Dynamically Name and Set Variable Values?

Avatar

Level 2

First one with a correct answer will have the FUNCTION named after them......

What I want to do is create 'new' variables based on the index of the parent.

So, if I have 4 subforms called 'Panel' (Panel[0], Panel[1], Panel[2], Panel[3]), I want do declare a local variable in my function to count from 0 to n  for each Panel.

We won't know how many Panels we will have until runtime. So, I need the variables to keep track of the count (it's tracking the number of errors) - for each Panel.

Something like this:

if(obj.rawValue==null || obj.rawValue=="")

{

var errorCount[obj.parent.parent.index]=errorCount[obj.parent.parent.index]+1;

app.alert(errorCount[obj.parent.parent.index]);

}

I am assuming it has something to do with Arrays.  But I suck at Arrays.

Thanks for the help.

1 Reply

Avatar

Level 6

You're correct, arrays are the way to go.  Define your error array like this:

var errorCounts = [];

Then, understand that when an array entry is not set it will have the value 'undefined', so you can know if an entry has been initialized.  So to increment the count for a particular index (subform occurance) use:

// If entry is being set for the first time, set it to 1.  Otherwise increment count.

if ( errorCounts[index] == 'undefined' )

    errorCounts[index] = 1;

else

     errorCounts[index]++;

You can also loop through and initialize the array to zero; then you can remove the check for undefined.  How and where you do this and how you figure out what 'index' is depends on how your form is set up and which event you're using to kick off the error check.  Give me a little more detail on what you're trying to do.