NULL string assignment

Hello-

This is probably a simple fix, but I'm stuck.

When I create a new form, I want the text in a few edit boxes to reflect certain global variables. It doesn't work the way it should; I traced the problem to this section of code:


void __fastcall TOptions_Form::FormCreate(TObject *Sender)
{

AnsiString temp = DefaultTemplateDirectory;

Options_Form->Default_Template_Directory_Edit->Text = temp;

}

The debugger tells me that DefaultTemplateDirectory has the right value ("C:\"), but temp is assigned a value of NULL.

Why!?

I've created local variables, and those get copied just fine. On top of that, nearly identical code in another function works just fine:

void __fastcall TTemplateDirForm::DirOKButtonClick(TObject *Sender)
{
DefaultTemplateDirectory = TemplateDirForm->DirectoryLabel->Caption;
Options_Form->Default_Template_Directory_Edit->Text =
DefaultTemplateDirectory;
TemplateDirForm->Hide();
}


What am I missing? Any help would be appreciated.

Comments

  • Of course, ten minutes after I break down and ask for help, I get it to work. Changing my global to be a public var contained in my main form's class did the trick.

    Still, I don't understand the mechanics behind what happened. Could someone explain it? Thanks.
  • : Of course, ten minutes after I break down and ask for help, I get it
    : to work. Changing my global to be a public var contained in my main
    : form's class did the trick.
    :
    : Still, I don't understand the mechanics behind what happened. Could
    : someone explain it? Thanks.
    :
    Sure, you just discovered one of the reasons never to use globals.

    In section A a local variable 'temp' was assigned 'C:\'. This because when writing to some identical-named variables, the one most local is chosen.
    In section B there was no local variable 'temp', so it read the global variable 'temp'.

    See ya,
    bilderbikkel
  • Thanks for the quick reply.

    Actually, I broke the single assignment statement into two parts so I could check the values in the debugger. Before temp existed, I still had the same problem. I wasn't aware of any other variables with the same names as my globals, so I didn't think scope would be a problem. But yeah, I should know by now that globals are always a poor idea...

  • : Thanks for the quick reply.
    :
    : Actually, I broke the single assignment statement into two parts so
    : I could check the values in the debugger. Before temp existed, I
    : still had the same problem. I wasn't aware of any other variables
    : with the same names as my globals, so I didn't think scope would be
    : a problem. But yeah, I should know by now that globals are always a
    : poor idea...
    :
    :
    Note that while debugging, if you are at the line

    [code]
    String s = "A string";
    [/code]

    the value of 's' will be 'NULL', because when you are on a certain line, the statements of that line have not been executed yet. The next line, 's' will be 'A string'.
    bilderbikkel
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories