How to create a variable and use it anywhere in the code? ( C# WFA ) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

How to create a variable and use it anywhere in the code? ( C# WFA )

I am working in a c# code in windows form application and I need a variable I can use anywhere. Where or how do I need to write it?

24th Oct 2019, 3:38 PM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
2 Answers
+ 6
Create a file for variable declaration and include that file where you want. But variable will be Global. So you may get lots of problem. That's why we have Global and Local variable. But according to this link c# doesn't support global variable directly. https://www.arclab.com/en/kb/csharp/global-variables-fields-functions-static-class.html
24th Oct 2019, 4:33 PM
A͢J
A͢J - avatar
+ 2
If you want this variable to be the same variable used everywhere and all parts of your code to see the same value for this variable , meaning a single instance of this variable then create a public static class with a public static variable. You can then use the class name.variable name to access it.
26th Oct 2019, 12:43 AM
Matengele Kaira
Matengele Kaira - avatar