We have a rule in Halacha that if a Mitzvah is done by transgressing a law it is not considered a Mitzvah. We call this MITZVA HABA’AH BE’AVERA. For example, we have a Mitzvah of Lulav and Etrog. Suppose one steals them in order to do this Mitzvah; the Halacha is that he did not fulfill his obligation. (Orach Chaim 249;1) However, there are times when you could be doing the wrong thing but your actions still result in a Mitzvah. An example of this is found in today’s Sidra.
When one offers to bring a Korban and he designates the animal that is to be used for that purpose, he is not permitted to substitute a different animal in its place. If, however, he transgresses and he does make a substitute, then the second animal is Kodesh, which means it may be offered as a sacrifice and is a Mitzvah. The first Korban, on the other hand, does not lose its holiness and must also be brought as a sacrifice.
What does all this mean? The implication is clear. There are times when your intentions may be good but in order to accomplish your objective you resort to the wrong means. If without the transgression you cannot perform the goal you have in mind, then your act is not acceptable. At other times, when you do something wrong but the results are commendable, then your acts, though improper, are acceptable. For example, when you give charity to a poor soul but embarrass him in the process, you are wrong and transgress, but the charity is still a Mitzvah.