Please have a look at this question and my answer to it. The first initial half of my answer address the OP's original problem and explains what's wrong there.
And at the very end, I've given the suggestion of how it could be done in a better way. Isn't this the proper way of answer a question? Or should I just ask the OP to directly use the better way, even without helping the OP with the original problem? It could be a homework/assignment or the OP may not learn the concepts he/she is supposed to if I directly ask the OP to use the better way.
Also, shouldn't solving the OP's original problem be the priority?! Alternates/suggestions are good, but shouldn't they take the back seat generally?! I may be missing something here, which is why I want to clarify it.
Note: I'm not worried about the downvotes to my answer, I just want to know, if what I've done is right or not?!