You must set a variable that can be used in any class method. Where do I need to make an announcement?

those. I created a public class in it I have several public static void subroutines. I need to set a variable that I can use in each subroutine. if I just set this variable in the first subroutine as public double a; the rest of the code immediately turns red.
Moreover, I need to calculate the value of this variable in one of the subroutines, and in the following subroutines, in order to substitute the calculated value.

  • 3
    in terms of where? wherever there is always - Deno
  • Clarify the question. There are nuances: for example, if a variable should be used in a static method, then it should also be static, but this does not affect the place of declaration of the variable. - zverkov

1 answer 1

Try to create a private class variable. Since the methods (subroutines in your understanding, although in Sharp there is no such concept in relation to a class), the static variable must also be static.

 public static class Foo { private static int a = 0; public static void Inc() { a++; ... } public static void Dec() { a--; ... } } 
  • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky