Archive: Is Variable Declared?


Is Variable Declared?
Is there any way to determine in a script if a variable has been declared or not? I want to be able to declare a variable using a macro, but only if it hasn't already been declared.


Variables aren't so much declared as that they either contain data, or they don't. So you could just use :
StrCmp $Var "" +2 0
MessageBox MB_OK "Var is 'declared'"


Yes but if a variable hasn't been declared from within a macro, then that will throw a compiler warning.
You should define a constant of the same name of that variable and check if that constant exists with !ifdef.

E.g.

!macro Var VarName
Var "${Var}"
!define "${Var}"
!macroend

!insertmacro Var MyVar

Function MyFunc
!ifdef MyVar
StrCpy $MyVar "blah"
!endif
FunctionEnd


-Stu

ah, good point!


!ifndef VAR_DECLARED
!define VAR_DECLARED
Var VAR_NAME
!endif


-dandaman32

Thanks for the suggestions. That's certainly a clever and viable workaround!

But not exactly what I was looking for as it pollutes the macro namespace and is more apt to cause collisions when using headers from multiple sources. :(

OTOH, I rearchitected what I was doing so I didn't end up needing it anyway. At least not this time. :)