2

Writing:

CreateDialog[{TextCell["Enter number: "], 
              InputField[Dynamic[nm], Number], 
              DefaultButton[DialogReturn[ret = nm]]}];

ConstantArray[0, nm];

I get:

ConstantArray::ilsmn: Single or list of non-negative machine-sized integers expected at position 2 of ConstantArray[0,1].

and I do not know how to correct. Some idea? Thank you!

πρόσεχε
  • 4,452
  • 1
  • 12
  • 28
  • use ConstantArray[0, ToExpression@nm] or modify your InputField[...] to InputField[Dynamic[nm], Number]? – kglr Feb 03 '18 at 18:04
  • Then you have some odd cached definition. As you have it it works for me. – b3m2a1 Feb 03 '18 at 19:52

1 Answers1

3

In your example the ConstantArray[0, nm] is evaluated immediately after the creation of the dialog window, i.e. before you enter any input. What you need to do is have evaluations "pause" until you enter a number into the input window and the dialog closes. DialogInput is your friend.

DialogInput[
  DialogNotebook[{TextCell["Enter number: "], 
    InputField[Dynamic[nm], Number], 
    DefaultButton[DialogReturn[nm]]}]];

ConstantArray[0, nm]

While the above will "work" you need to add some checks of the input:

If[IntegerQ[nm],
 ConstantArray[0, nm],
 Print["Hey you need to enter an integer"]
 ]

enter image description here

Mike Honeychurch
  • 37,541
  • 3
  • 85
  • 158