# Make a slider controlling a generated character's code value

If I execute the following code:

char = 97;
Slider[Dynamic@ToExpression@FromCharacterCode@char, {0, 5}]


the slider will fail to change the value of a.

How do you make this work, when the expression has to be generated with FromCharacterCode or similar?

Note:

ToExpression@FromCharacterCode@97 == a

-
you have commented about a further below but you should fix up this question describing what a is and how it relates to char. –  Mike Honeychurch Dec 5 '12 at 20:44

Perhaps the simplest thing would be to put the string "Dynamic@" inside ToExpression:

Slider[ToExpression["Dynamic@" <> FromCharacterCode@char], {0, 5}]


This creates a slider which controls the value of the symbol a, which I think is what you are asking for.

-
Very slick. :-) –  Mr.Wizard Dec 6 '12 at 11:31

How about using With?

char = 97;
With[{var = ToExpression@FromCharacterCode@char},
Slider[Dynamic@var, {0, 5}]
]


or, if a already exists, following Simon Woods' suggestion:

char = 97;
With[{var = ToExpression[FromCharacterCode@char, InputForm, Unevaluated]},
Slider[Dynamic@var, {0, 5}]
]

-
You might want to use ToExpression[FromCharacterCode@char, InputForm, Unevaluated] in case a already has a value. –  Simon Woods Dec 6 '12 at 10:11
@SimonWoods Good point! –  Sjoerd C. de Vries Dec 6 '12 at 10:40

I think you mean something like this:

Clear[char];
{Slider[Dynamic[char], {67, 97, 1}], Dynamic[FromCharacterCode[char]]}


The syntax in your question was incorrect because the dynamic variable in the first argument of Slider can't be a protected function name.

Edit

You also had a ToExpression in your question. That can be added in this answer too, obviously by writing ToExpression@FromCharacterCode. Then you just have to make sure your character range doesn't include characters that yield an error when fed into ToExpression.

Edit 2 In response to the comment, I think what you should do is to write

a = Dynamic[ToExpression@FromCharacterCode[char]]


with the above slider (or some version of it).

-
No I want it to change a value generated by FromCharacterCode and in my example it should modify a. –  Tyilo Dec 5 '12 at 19:24
But your example code does not contain any variable a! What is the variable a, and what is it supposed to be changed to? –  Jens Dec 5 '12 at 19:26
ToExpression@FromCharacterCode@97 == a –  Tyilo Dec 5 '12 at 19:26
Slider[Dynamic @@ ToHeldExpression @ FromCharacterCode @ char, {0, 5}]