Dialog variables not saved?

I'm trying to have persistent dialog variables, but I can't manage to do it.
I took the example from the documentation, and modified it as follows:

Function OnClick(ByRef clickData)

    Set dlg = DOpus.Dlg
    dlg.window = clickData.func.sourcetab
    dlg.template = "person"
    dlg.detach = true
    dlg.Show

    Set vars = dlg.Vars("myUniqueId")
    DOpus.Output "saved: vars.count = " & vars.Count
    vars.Set "one", 1
    DOpus.Output "now: vars.count = " & vars.Count

    Do
       Set msg = dlg.GetMsg()

       Select Case msg.control

           Case "add"

              ' get the string the user has entered
              str = dlg.Control("title", "books").value

              if Len(str) > 0 Then

                  ' add to the list box and clear the edit field
                  dlg.Control("books", "books").AddItem(str)
                  dlg.Control("title", "books").value = ""

              End If

           Case "del"

               ' get the selection from the list box
              sel = dlg.Control("books", "books").value
              if sel > -1 Then

                  ' remove it from the list
                  dlg.Control("books", "books").RemoveItem(sel)

              End If

       End Select

    Loop While msg

End Function

Whatever the number of times I run the dialog, there are initially zero variables.
What am I doing wrong?

Variables aren't persistent by default.

After the vars.Set "one", 1 line, add a line which runs vars("one").persist = True and I think you should be good.

Thank you.
I do not seem to have to do that for Script.Vars: is this a difference in behavior depending on the object?

It should work the same, but the scope of the object itself is probably different. The dialog gets destroyed and recreated each time, while the script may continue running for a while until it becomes idle for a while.

Restarting Opus (File > Exit Directory Opus) is the way to test if variables are really persistent.

Aaaah! Now I understand. Thank you!

1 Like