I'm sure I know the answer to this, but I just want to double check because I'm doing some debugging and I want to rule this out.

If I set the value of a variable with something like bool MyValue = MyMethod(); and initially the return value of MyMethod is False, but a few seconds later the value changes to True, and I refer to MyValue later, does it keep it's inital value of False, or does it re-evaluate it each time?

I'm under the impression that it does not change and should stay False, and that only if I use get; set; and in the get accessor I return MyMethod() will the value update itself...is that right? Because it doesn't seem to be doing that unless I missed something else that is changing the value.

No, variable ONLY holds one value. So the last passed to it. If you "overwrite" false with true, there is no way to get false back. Only true is available.

any why would you like to get previous value? There is no sence.
Anyway, if you wanna hold multiple values in one variable, I would suggest you to use Dictionary (key will be index (like counter from 0,1,2, and up), while value will be an actual state of variable (in your case true and false will be chaging).
Example:

Dictionary<int, bool> dic = new Dictionary<int, bool>();
int counter = 0;
//inside your method:
private void DoWork()
{
     dic = MyMethod();
}

private Dictionary<int, bool> MyMethod()
{
    bool bFlag = true, or false; //swapping values...
    dic.Add(counter, bFlag);
    counter++; //rise it by 1 for next time round
    return dic;
}

Even MyMethod could return void, since dic variable is accessible in the whole class. Anyway, I only wanted to show an exmaple.
One more last thing: you can simply then loop through the dictionary and check the boolean states:

foreach(KeyValuePair<int, bool> kvp in dic)
{
    //in each iteration:
    int key = kvp.Key;
    bool bValue = kvp.Value;
}

Hope it helps.

No I don't think you understand...here's an example:

Assume:

private bool MyMethod()
{
   if (MyInteger > 0)
      {
         return true;
      }
   else
      {
         return false;
      }
}

if I call the following in a method:

private void SomeOtherMethod()
{
   // earlier code
   
   int MyInteger = -100;

   bool MyValue = MyMethod();  // MyValue should be false here

   MyInteger = 100;  // Is Myvalue still false now?

   // more code
}

Because the value of MyInteger changed, and would, therefore change the return value of MyMethod(), does it actually change it, or is MyValue still false after MyInteger gets a new value?

No I don't want to hold multiple values.

I guess what I am asking is does a variable that get's a value via a method get the actual value or does it basically have a reference to the method that provides the value?

Because the value of MyInteger changed, and would, therefore change the return value of MyMethod(), does it actually change it, or is MyValue still false after MyInteger gets a new value?

I understand completely.
When your "MyValue" variable get new value of true, false is no longer available.
The MyValue get changed instantly when you assign to it in another method - if this is what you would like to know (ones again, its get overwritten in the moment you assign new value to "return true", or "return false".

It will remain false because you are SETTING the value of the bool and not resetting (or re-evaluating):

using System;

namespace DW_418381_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         int intMyValue = -100;
         bool blnMyValue = (intMyValue > 0); // right now only
         Console.WriteLine(blnMyValue);
         intMyValue = 33;
         Console.WriteLine(blnMyValue); // still false
         
         // will only change to true if RESET
         blnMyValue = (intMyValue > 0);
         Console.WriteLine(blnMyValue); // NOW true
      }
   }
}

If you remove the setting of the bool and go straight for the evaluation, it MUST change:

using System;

namespace DW_418381_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         int intMyValue = -100;
         Console.WriteLine((intMyValue > 0));

         intMyValue = 33;
         Console.WriteLine((intMyValue > 0)); // now true
      }
   }
}

...even if it is a function call (without the individual bool variable):

using System;

namespace DW_418381_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         Func<int, bool> GreaterThanZero = i => i > 0;

         int intMyValue = -100;
         Console.WriteLine(GreaterThanZero(intMyValue));

         intMyValue = 33;
         Console.WriteLine(GreaterThanZero(intMyValue)); // now true
      }
   }
}

Hmmm then something else is going on somewhere...back to bug hunting :/

Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.