2
$\begingroup$

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!

$\endgroup$
2
  • $\begingroup$ use ConstantArray[0, ToExpression@nm] or modify your InputField[...] to InputField[Dynamic[nm], Number]? $\endgroup$
    – kglr
    Commented Feb 3, 2018 at 18:04
  • $\begingroup$ Then you have some odd cached definition. As you have it it works for me. $\endgroup$
    – b3m2a1
    Commented Feb 3, 2018 at 19:52

1 Answer 1

3
$\begingroup$

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

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.