That code is only valid within a constructor. The question was specifically asking how to do it dynamically, outside of a constructor.
That code is only valid within a constructor. The question was specifically asking how to do it dynamically, outside of a constructor.