3
Answers

Passing a static var by reference

Esteban Francisco

Esteban Francisco

13y
3.3k
1
I am having trouble updating a static variable by reference. I have a parent class passing a static variable into new class constructor by reference. The parent class is the primary executable. The new class is in a separate DLL. I can easily change the value of the static variable in the new class's constructor and it's reflected in the parent. Unfortunately, I cannot update this static variable from another method in the new class.

The flow of my program is something like the following:

[code]
namespace Somewhere
{
  public partial class TestReporter : Form
  {
    private TestForm tForm;
    public static bool TestInProgress = false;

    void someMethod()
    {
      tForm = new TestForm(ref TestInProgress);
    }
  }
}
[/code]

[code]
namespace OverTheRainbow
{
  public class Test
  {
    public bool RainbowProgress = false;

    public Test(ref bool prog)
    {
      RainbowProgress = prog;
    }

    void someOtherMethod()
    {
      RainbowProgress = true;
    }
  }
}
[/code]

So you see, I was hoping that calling someOtherMethod() in the TestForm class would update the parent's TestInProgress value. Unfortunately, I was wrong. Now, I am a C/C++ guy and my first though to solve this is pointers. I even tried to use pointers with C#, but with all the unsafe keywords and having to tell the compiler to use unsafe code, it just seemed way inappropriate. There has to be a way to update that boolean value in the parent class using something more natural to C#.

Can anyone help with this? Thank you so much!

Answers (3)