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!
Loading
VulpesPosted Jun 4, 2011, 9:34 AM
Esteban FranciscoPosted Jun 6, 2011, 1:12 PM
I am glad you noticed that this was a DLL communicating information to an executable. I was afraid I wasn't clear. The usage of "unsafe" pointers is pretty ridiculous in C#, so I am glad to have avoided it. In the end, I can now prevent a user from reviewing test data in the parent executable before the test is complete in the DLL.
As for the namespaces... yeah. Not sure where my head was. *grin* Thanks again.
P.S. Since the client and the dll use a shared common library, I created something like the following to make usage prettier. Now they can both access the object and everyone can live happily ever after.
public class RainbowProgress
{
private static bool[] TestInProgress = new bool[1];
public static bool Status
{
get
{
return TestInProgress[0];
}
set
{
TestInProgress[0] = value;
}
}
}
VulpesPosted Jun 4, 2011, 6:43 AM
BTW, some nice names for your namespaces ;)