I am trying to assign a variable that can be used in any other function, inside a function.
It is gonna be the path for my program, and it's important.
I don't know how to set/get global
s.
Thank you for any help!
This might be a duplicate, but I'm not sure what post to go on, or either what post does work.
Asked
Active
Viewed 190 times
-3
-
Does this answer your question? [How to use Global Variables in C#?](https://stackoverflow.com/questions/14368129/how-to-use-global-variables-in-c) – Progman Apr 01 '20 at 21:37
-
No, sorry. I don't work with those other .cs files. I work in one single file, with probably a reference inside it. – Ceyhun Apr 01 '20 at 21:40
-
1With one single file, I don't see why you need to do anything other than declare a class member at the top of your class and assign it in your function? – Klaycon Apr 01 '20 at 21:41
-
Can you give pseudo-code example? I can't understand why you can't achieve this by simple declaration. – eocron Apr 01 '20 at 21:41
-
`public void createJAVM(string path, bool logging, string logFileNameWithoutExtension = "bootlog.log") { public static string JAVMPath = path;` It doesn't work. I'm sorry for being such a noob in C#. – Ceyhun Apr 01 '20 at 21:45
-
1you cannot declare a class member inside a method. just *declare* the string `JAVMPath` at the top of the class (outside the function) and *assign* the string in the function – Klaycon Apr 01 '20 at 21:46
-
Oh, that might be correct. – Ceyhun Apr 01 '20 at 21:46
-
Yes! Thank you very much. – Ceyhun Apr 01 '20 at 21:47
1 Answers
2
There are no global variables in C#. At least not in the "traditional" sense. In C# evertthing happens inside classes, so the closest thing is a public static
field in a class. And, really - it's just a global variable in disguise.

Vilx-
- 104,512
- 87
- 279
- 422
-
-
Also, I need to put a variable inside the variable, so technically I would make a function argument completely global. – Ceyhun Apr 01 '20 at 21:42
-