Board index » cppbuilder » MDIChild and a static bool

MDIChild and a static bool


2005-03-11 05:38:41 PM
cppbuilder87
I was testing some code in chunks by executing it with button
clicks and because it would be destructive to execute a certain
block twice, I added a static bool to the OnClick because
stupid me would end-up clicking the wrong button and {*word*81} it
up.
Anyway, when I moved to a second instance of the child, if I
had clicked the button on the first instance, the static bool
in the second instance was false:
void __fastcall TChildForm::ToolButton1Click(TObject *Sender)
{
static bool flag = true;
if( flag )
{
flag = false;
ShowMessage("Flag Was True");
}
else ShowMessage("Flag Was False");
}
I was able to reproduce this with a new MDI application that
has a child with a single button coded as above and a main form
that does nothing but allocate an instance of the child for
every button click.
Does any one have an explaination for this? This is scary.
~ JD
 
 

Re:MDIChild and a static bool

JD wrote:
Quote
Anyway, when I moved to a second instance of the child, if I
had clicked the button on the first instance, the static bool
in the second instance was false:
Does any one have an explaination for this? This is scary.
This is normal behaviour. The static bool behaves like a global
variable for that function. For other functions it does not.
Better use a private bool.
Or disable the button as first statement.
Hans.
 

Re:MDIChild and a static bool

Hans Galema wrote:
Quote
Or disable the button as first statement.
IOW, instead of using a bool, why not just disable the button?
OnClick()
this->enabled = false;
(I think that's the code)
That way, each form would have it's own "memory" of the event.
And, only know about it's own button, not it's brother's button.
It has the added benefit of the state being visible so you don't keep
clicking and wondering why it doesn't work.
 

{smallsort}

Re:MDIChild and a static bool

A static variable is shared by every instance of a class, even if it is
local to a class method. This is behaving as the language is specified.
"JD" < XXXX@XXXXX.COM >wrote in message
Quote

I was testing some code in chunks by executing it with button
clicks and because it would be destructive to execute a certain
block twice, I added a static bool to the OnClick because
stupid me would end-up clicking the wrong button and {*word*81} it
up.

Anyway, when I moved to a second instance of the child, if I
had clicked the button on the first instance, the static bool
in the second instance was false:

void __fastcall TChildForm::ToolButton1Click(TObject *Sender)
{
static bool flag = true;
if( flag )
{
flag = false;
ShowMessage("Flag Was True");
}
else ShowMessage("Flag Was False");
}

I was able to reproduce this with a new MDI application that
has a child with a single button coded as above and a main form
that does nothing but allocate an instance of the child for
every button click.

Does any one have an explaination for this? This is scary.

~ JD