Character reference not saving as a variable after casting