Create and User an Array

Create and User an Array

  
How can I create and use an internal array? Such as, an array to store and use the statuses:
1: Open
2: Approved
3: Closed

I'd like to create this array as a Session Variable and use it to change the status value stored in the database (1, 2 or 3) into its corresponding value (Open, Approved, Closed) for display. The same we do in regular VBScript with Dim Array(1,2).

Thanks for any help

Fernando Dias
Dear Fernando,

In regards to the problem you posed, we can suggest three different courses of action:

1) Create an application table that translates these indices to the strings they represent. The Entity that would serve as a base for this translation (let's call it "StateDictionary") could be made up of a single Text attribute (let's call it "State") as well as the Id.
When doing the query where you currently retrieve the index, you could then add this Entity as well and link both your table and this new one with some sort of "YourTable.State_Id = StateDictionary.Id" condition. You would then use the result of the query in your expression, something like "Query1.current.StateDictionary.State". You would have to make sure that the application fills this table once (and only once), but on the other hand you could then use this translation method from several application screens.

2) Create a hard-wired translation in the Expression widget. For this matter, you could use the FormatBoolean() function. In your case, assuming the State id is in an Integer variable called "StateId", you could have
FormatBoolean(StateId=1, "Open", FormatBoolean(StateId=2, "Approved", "Closed")
This is possibly the cleanest option from the viewpoint of the whole eSpace, but you would have to live with having the same constants potentially spread among many expressions...

3) Create a local (screen-wide) Record List in preparation and index it using the '[]' format. You could start by creating a "StateDictionary" Structure with a "State" attribute. Then you would add a Record List Variable composed of Structure "StateDictionary" to the screen and, in the Screen Preparation, add the three strings to this "StateDicRecordList" variable using the ListAppend Built-in action. From there on the Expression Widget could then be "StateDicRecordList[MyIndex].StateDictionary.State" (assuming your State index is in variable MyIndex - note that indices in Service Studio are zero-based).

Of course if you want to display this info as a drop-down, you can use the Select widget directly.

Hope this helps.

Regards,


Miguel
Thanks Miguel.
That's what I needed. The third option is good enough.
One little question... to avoid repeating all of it in every screen I need, I tried to create a Session variable with the structure. I was warned that it is not advisable to do so. Can you tell me what can be wrong?

Thanks for your help.

Fernando
Regarding the use of constant values in eSpaces: to avoid the use of literal values spread out through the code you should use Site Properties - it's the most correct way to keep them. You can define Status_Open_Idx = 1 / Status_Open = "Open" ... or just the indexes or just the strings.

Dear Fernando,

The warning you received is meant to highlight the fact that the usage of potentially large data types as session variables could impact on performance.

For very small Record Lists, which I believe is the case you are considering, this is not a huge consideration. This is simply meant as a "please make sure this is really required" warning, so that people don't mistakingly use a session variable to store things like huge logs etc.

Hope this helps on deciding if you want to pursue this approach or not.

Regards,


Miguel
Thanks Miguel and Nuno.

Fernando