Skip to content
⌘ NSIS Forum Archive

Is Variable Declared?

6 posts

Guest#

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.
Animaether#
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'"
Afrow UK#
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
Guest#
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. 🙂