-1

Goal:

I am having some issues in C# when ensuring that a variable retains its assigned value for the duration / runtime of a program. In particular, I am attempting to:

  1. Read a number in from some input
  2. Save it to a variable of type double
  3. Print the variable
  4. Ensure that this value stays the same for the duration of the program, so that on the next iteration when a new value is read in, the previous value from step (2) is still printed.

Attempts:

I have attempted to use a global static variable such as public static double foo;, however, whenever the method that retrievs new values is called again, a new value is printed.

In C, for example, I believe the closest functionality to this would be to use the static keyword, however, I have read that this functionality has been intentionally excluded from C# and I am struggling to learn why this is the case.

void foo()
{
     static int j;
     int i = calculateSomeValue() + j;
     j = i;
}

Please note that before deciding to post this question, I have consulted the following resources:

  1. https://softwareengineering.stackexchange.com/questions/141871/false-friends-keyword-static-in-c-compared-to-c-c-and-java
  2. https://docs.microsoft.com/en-us/answers/questions/240965/retain-previous-value-of-text-box-till-the-form-is.html
  3. Retain local variable across method calls
  4. How to retain old value when setting variable on c# class
  5. https://social.msdn.microsoft.com/Forums/en-US/46243c7b-959e-4753-a173-34f52a7f8922/variables-retain-value-from-first-pass?forum=csharpgeneral

Q1. How can a value assigned to a variable be retained for the entire runtime of a 'C#' program?

Apologies if I am missing something extremely obvious, but it seems that the static keyword in C is used in a different manner compared to that of C#.

p.luck
  • 524
  • 2
  • 6
  • 29
  • 1
    What do you mean by "on the next iteration"? Why don't you just make sure that you only read the value from the input (and set the variable) once? A [mcve] would really help here... – Jon Skeet Mar 11 '22 at 19:11
  • 1
    If this is a multi-threaded application you could use the attribute [Threadstatic] – Paul Sinnema Mar 11 '22 at 19:14
  • Thanks @JonSkeet , I have reworded my question for clarity on what I meant by "next iteration"- I meant to say that each time the method is called that retrieves values, the variable that I would like to retain its value changes. I will update my question with an example. – p.luck Mar 11 '22 at 19:15
  • Variables aren't associated with methods. Why not just change your code to only call "the method that retrieves values" once, if that's the method that sets the variable? Hopefully it'll be clearer when you've provided a [mcve]... – Jon Skeet Mar 11 '22 at 19:19

1 Answers1

1

According to your description, the variable is re-assigned at step #2. It is expected that step #3 (Print the variable) will output the changed variable value at each iteration.

Your understanding of "static" meaning in C# is correct, at least in the scope that you have described.

user488399
  • 19
  • 2