2
$\begingroup$

I have a question about the differences in SetDelayed assignment functions in a Module and in a DynamicModule. In all likelihood, the answer is pretty basic as I am rather new to Mathematica. Nevertheless, I have failed to find the answer in the docs nor in this forum, so I thought I would ask.

The basic problem is the following: SetDelayed does not seem to "delay" when used in DynamicModule, whereas it does in Module. Below I provide code for four different functions that show the behavior.

  • func1 pops up two dialog windows, once for the SetDelayed assignment of insidefunc and once when insidefunc is called (DynamicModule)

  • func2 pops up one dialog window, once when insidefunc is called (Module)

  • func3 pops up one dialog window, once when insidefunc is SetDelayed (DynamicModule)

  • func4 pops up no dialog window


Clear[func1,func2,func3,func4];


func1:=DynamicModule[{insidefunc},
         insidefunc:=DialogInput[CancelButton[]];
         insidefunc];

func2:=Module[{insidefunc},
         insidefunc:=DialogInput[CancelButton[]];
         insidefunc];

func3:=DynamicModule[{insidefunc},
         insidefunc:=DialogInput[CancelButton[]];];

func4:=Module[{insidefunc},
         insidefunc:=DialogInput[CancelButton[]];];

Row[{
    Button["func1", func1["This is function 1"], Method->"Queued"],
    Button["func2", func2, Method->"Queued"],
    Button["func3", func3, Method->"Queued"],
    Button["func4", func4, Method->"Queued"]}]

So, any ideas what I am doing wrong here?

$\endgroup$
10
  • $\begingroup$ Button evaluation inside DynamicModule probably a duplicate $\endgroup$
    – Kuba
    Feb 20, 2014 at 11:33
  • 1
    $\begingroup$ SetDelayed is interpreted as Set within DynamicModule. there is really no point in using it. You need to use Initialization to get it to work correctly. $\endgroup$ Feb 20, 2014 at 12:00
  • $\begingroup$ Additionally there is nothing dynamic about what you are doing. You should use Module and Button $\endgroup$ Feb 20, 2014 at 12:35
  • $\begingroup$ The example I provided was to simplify the problem down to the basic idea. I have plenty of dynamic stuff in the real code. Thanks for your help on this. I'll take a look Initialization and the link by Kuba. $\endgroup$ Feb 20, 2014 at 13:03
  • $\begingroup$ @Kuba, thanks, your link hit on the question exactly. Sorry that I didn't find it before posting. $\endgroup$ Feb 20, 2014 at 13:09

0

Browse other questions tagged or ask your own question.