Is there any approach that works with any class including different variable ?

This is tedious because you need to write this every time for every class.