Set a variable variable

Questions or information that don't quite fit anywhere else

Moderator: Neosoft Support

Locked
User avatar
HPW
Posts: 2571
Joined: Fri Apr 01, 2005 11:24 pm
Location: Germany
Contact:

Set a variable variable

Post by HPW »

I run across the problem to set a variable variable.
It is logical but I did not get it first. ;-)

A sample code from a loop:

Code: Select all

  ...
  SetVar "[NewVarArt]" "[Variable[vCounter]]"
  SetVar "[[NewVarArt]]" "[VarValue]"
  ...
You have to use the double brackets.

Regards
Hans-Peter
Neosoft Support
NeoSoft Team
Posts: 5628
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA
Contact:

Re: Set a variable variable

Post by Neosoft Support »

I think it works doesn't it?
NeoSoft Support
User avatar
dpayer
Posts: 1394
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Re: Set a variable variable

Post by dpayer »

Neosoft Support wrote:I think it works doesn't it?
What Hans-Peter shows does work for me:

Code: Select all

SetVar "[NewVarArt]" "[Variable[vCounter]]"
  SetVar "[[NewVarArt]]" "[VarValue]"
But I did find that initially I had some issues when I had not given the 'naming' variable ( HPW's example: [NewVarArt] ) a value prior to using it. IOW my coding was poorly structured.

I notice in NB that if a variable is set to be empty

Code: Select all

setvar "[test]" ""
the variable is considered non-existent and does not appear in the debugger. This can be confusing if you are attempting to determine if a variable has a value or not. I would think it would show in the debugger in the variable section with an identifier that it is empty.

But, if I DEFINE the variable with a global attribute,

Code: Select all

DefineVar "[test]" "String" "" "Global" ""
it does display in the debugger. With a local attribute, it displays when it is called but then is gone from the debugger afterward.
David Payer
Des Moines, Iowa
USA
User avatar
HPW
Posts: 2571
Joined: Fri Apr 01, 2005 11:24 pm
Location: Germany
Contact:

Re: Set a variable variable

Post by HPW »

It did work, I only wants to show that it can be used this way.
Hans-Peter
Neosoft Support
NeoSoft Team
Posts: 5628
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA
Contact:

Re: Set a variable variable

Post by Neosoft Support »

I notice in NB that if a variable is set to be empty

setvar "[test]" ""

the variable is considered non-existent and does not appear in the debugger. This can be confusing if you are attempting to determine if a variable has a value or not. I would think it would show in the debugger in the variable section with an identifier that it is empty.
We could have gone either way, but as it is setting a variable to a null value "" removes the variable from memory. Without the debugger window you wouldn't even realize this fact, because NeoBook treats non-existent and empty variables exactly the same. The downside to showing empty variables is the need to keep them in memory somewhere. The DefineVar action was introduced in part to provide the ability to declare typed variables in advance. Declared variables stick around even when they are set to a null value. This was an evolution of NeoBook over time that is no doubt confusing to new users since it's not really necessary except in specific situations.
NeoSoft Support
Locked