I’ve created a WindowsForm Application with 3 buttons. A click on any of the buttons reads c:\data.txt and performs a different “analysis” of the data. Button 1 might add the numbers in data.txt, Button 2 might average them, and Button 3 might multiply them.
Under each private void button1_Click I have a line of code that reads in the data from data.txt. This seems inefficient to me since the same “ReadLines” code is in each of the 3 button click events.
Is there a way to read the data just ONCE, and then have my buttons perform their analysis?
This way, the “ReadLines” code is only used once, and the data is somehow shared with any of the button1_Click events.
This is not difficult – you need to have a field to hold the values you read – the field will be available to all of your functions in the class.
Another option is to have a
LoadDatamethod that checks ifdataisnullor not and call it from your methods: