Within a Winform app, I would like data in an instantiated class to be accessible by multiple form controls.
For example, if I create Class Foo, which has a string property of name, I’d like to instantiate Foo a = new a() by clicking Button1, and when I click Button2, I’d like to be able to MessageBox.Show(a.name). There may be multiple instances of Foo, if that matters at all.
What is my best option for being able to use class instances in such a way?
A private field or property of a class satisfies the requirement – such field can be accessed by all methods of the class.
If you want this variable to be accessible to other forms you’d need to make it public (preferably as property) – C# winform: Accessing public properties from other forms & difference between static and public properties