·
Have you noticed a lot of code in your application that does the same thing?
Why not encapsulate a method for this?
For example:
It would be a lot easier and much clearer to encapsulate all this code in a separated class and method:
Consider if need to keep state of your object, if so, instantiate it!
Remember, the most important thing here, is to encapsulate your solution!