Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Scoping constructs Block, Module, ModuleBlock violate principle of

  • To: mathgroup at smc.vnet.net
  • Subject: [mg111110] Re: Scoping constructs Block, Module, ModuleBlock violate principle of
  • From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
  • Date: Tue, 20 Jul 2010 07:55:09 -0400 (EDT)
  • References: <201007200744.DAA15264@smc.vnet.net>

Hi,

even *in* a DynamicModule you have to use Dynamic to *mark* the that should be
updated dynamically

DynamicModule[{n = 7}, Row[{Dynamic[n], Button["X", --n; Print[n]]}]]

Cheers
Patrick


Am Jul 20, 2010 um 9:44 AM schrieb Michael:

> This statement yields unexpected behavior:
>
> DynamicModule[{n = 7},
>  Row[{n, Button["X", --n; Print[n]]}]
>  ]
>
> Why doesn't the "n" that appears as the first element in Row[] change
> when I click on the button?  The Print[] statement clearly shows it is
> being decremented.  I can look at "n" outside of the DynamicModule[]  
> and
> see that it is not defined, so Button[] can't be working with a
> different "n" than Row is trying to display, can it?
>
>
> n2=7;
> Dynamic[
>  Row[{n2, Button["X", --n2; Print[n2]]}]
>  ]
>
>
> This behaves as expected, but I need localization and this doesn't
> provide it.  Ok, I will use a Block[].  But wait...
>
> Block[{n3 = 7},
>  Dynamic[
>   Row[{n3, Button["X", --n3; Print[n3]]}]
>   ]
>  ]
>
> Mathematica highlights n3 in Red, and n3 appears to lose its value
> inside the Dynamic.  I have looked, but I can't find anything in the
> documentation for Block[] or Dynamic[] suggesting this sort of  
> behavior.
>
> Ok, how about a Module[] instead of a Block[]?
>
> Module[{n3 = 7},
>  Dynamic[
>   Row[{n3, Button["X", --n3; Print[n3]]}]
>   ]
>  ]
>
> This works fine.  So why doesn't DynamicModule[] work???  There is  
> also
> another problem with this approach.  I might want to define a more
> complex interface than just a button:
>
> f[x_] := Button[x, --n3; Print[n3]]
> Module[{n3 = 7},
>  Dynamic[
>   Row[{n3, f["X"]}]
>   ]
>  ]
>
> This is why I wanted to use Block[] or even DynamicModule[], because
> then f[] would see n3 in the context of the Block.  But since Module[]
> creates internally some name like n3$1, f[] can't see it.
>
> I'm using Version 7.
>
> What's going on here?  Any suggestions on how I can accomplish what
> seemed like any easy task but wound up eating up my entire evening  
> with
> nothing to show for it?
>
> Thanks,
>
> Michael
>
>



  • Prev by Date: Re: What's wrong with this assuming?
  • Next by Date: Re: Scoping constructs Block, Module, ModuleBlock violate principle
  • Previous by thread: Scoping constructs Block, Module, ModuleBlock violate principle of
  • Next by thread: Brillouin function for a Ferromagnet