How to create a variable that works in all WinForms? (C#) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How to create a variable that works in all WinForms? (C#)

I need a variable that I can call no matter in which Form I am writing. Do I need to use a class, a static or an object? Because I tried to use the class and static and I only can call it once in one form

12th Mar 2018, 7:28 PM
SebGM2018
SebGM2018 - avatar
2 Answers
+ 6
make variable public in some class and then you can take value of that variable by creating object for that class and call method that return you value of that variable
12th Mar 2018, 8:08 PM
Vukan
Vukan - avatar
+ 1
You can add a class to your project and make the class static static class TheGlobalClass { public static GlobalVariable1 {get; set} } I would recommend not to do it. It is bad practice. If a variable is not anywhere it can be changed anywhere. This can cause unexpected behaviour. If you need to now about a variable in a form, make a property and assign the value to this propertie.
13th Mar 2018, 9:19 PM
sneeze
sneeze - avatar