What messed up the code formatting in the linked question is the indenting. Indenting a line with 4 spaces causes it to be formatted as code. When a line is flush against the left margin, it's treated as regular text.
Clicking the "Code Sample" button attempts to indent each line of an entire block of text 4 spaces, but it doesn't always work when you copy and paste from your IDE. If you notice a formatting problem, go back and ensure that each line is properly padded. The "preview" pane at the bottom of the page can help you make sure that everything looks right. And remember, you can go back and edit your question or answer even after you've submitted it if you only notice the code formatting later.