Serializing Dictionary Selection

On working with dictionaries, the user need to save specified selection and restore it, for example, at the next start of form. The selection can be stored in different places (file on disk, registry, table on server and so on). Consider methods for saving and restoring the selection.

A range of methods used to save and restore the selection is implemented in the IDimSelection interface:

The methods are based on working with array of the Variant type, containing values of the selected element attribute.

Consider possible variant of saving and restoring the selection from OS registry. Selection is saved as character string in the registry parameter Selection1 in the registry section HKEY_CURRENT_USER\UserProject\Selection.

Procedure for Recording Selection to Registry:

Sub WriteSelToRegistry(s: String);
Var
    CurrentKey, ProjKey: IRegistryKey;
Begin
    CurrentKey := RegistryClass.CurrentUser;
    //Open the registry section, in which selection is stored
    If CurrentKey.SubKeyExists("UserProject\Selection"Then
        ProjKey := CurrentKey.OpenSubKey("UserProject\Selection"True);
    Else
        ProjKey := CurrentKey.CreateSubKey("UserProject\Selection");
    End If;
    //Record of selection in the registry parameter Selection1
    ProjKey.WriteString("Selection1", s);
End Sub WriteSelToRegistry;

Function for Reading Selection from Registry:

Function ReadSelFromRegistry: String;
Var
    CurrentKey, ProjKey: IRegistryKey;
    s: String;
Begin
    CurrentKey := RegistryClass.CurrentUser;
    //Open the registry section, in which selection is stored
    If CurrentKey.SubKeyExists("UserProject\Selection"Then
        ProjKey := CurrentKey.OpenSubKey("UserProject\Selection"True);
    Else
        ProjKey := CurrentKey.CreateSubKey("UserProject\Selection");
    End If;
    //Read selection from the registry parameter Selection1
    s := ProjKey.ReadStringDef("Selection1""");
    Return s;
End Function ReadSelFromRegistry;

To form selection as character string, the IDimSelection.ToString method can be used, or the method  String.Join:

Var
    Selection: IDimSelection;
    Sel: String;
Begin
    //Receive selection
    Selection := UiDimension1.Selection;
    //For compatibility with the ToVariant method, save selection by values of the ID identifier
    Sel := Selection.ToString("ID");
    WriteSelToRegistry(Sel);

or:

Var
    Selection: IDimSelection;
    VarSel: Array Of Variant;
    StrSel: Array Of String;
    Sel: String;
    i: Integer;
Begin
    Selection := //Receive selection
    VarSel := Selection.ToVariant As Array;
    StrSel := New String[VarSel.Length];
    For i := 0 To VarSel.Length - 1 Do
        //Create symbol array, for further integration to the string
        StrSel[i] := VarSel[i] As String;
    End For;
    //For matching with work of method ToString, save selection with delimiter ","
    Sel := String.Join(",", StrSel);
    WriteSelToRegistry(Sel);

To restore selection from the registry, the ReadSelFromRegistry function described above reads saved value from the registry, and parses the received string into array of the Variant type containing number values of identifiers:

Var
    Selection: IDimSelection;
    Sel: String;
    StrSel: Array Of String;
    VarSel: Array Of Variant;
    i: Integer;
Begin
    Selection := //Receive selection
    //Read character value of selection
    Sel := ReadSelFromRegistry;
    //Converting of selection in symbol array
    StrSel := Sel.Split(",");
    //Create array of the Variant type, in which number values of identifiers are typed in
    //The Variant type is required for the method IDimSelection.Parse
    VarSel := New Variant[StrSel.Length];
    For i := 0 To StrSel.Length - 1 Do
        VarSel[i] := Integer.Parse(StrSel[i]);
    End For;
    //Restore selection
    Selection.Parse(VarSel);

The received selection can be used where necessary.

See also:

Examples