In the below script, there is passed var1
by reference to myDosFunc
, so it can change the value.
How can the value of the referenced variable be get within the function without having to pass the variable and its value?
set "var1=Hello world"
Echo var1 before: %var1%
call :myDosFunc var1 "%var1%"
Echo var1 after : %var1%
goto :eof
:myDosFunc -- passing a variable by reference, and changing value
echo Notice that a variable passed by reference does NOT echo the value, and it echos the variable name instead.
echo Arg1 variable name = '%~1' and Arg2 value = '%~2'
set "%~1=Good bye world!!!"
goto :eof
I tried the following variations, but none of them worked to get the value.
set "var1=Hello world"
Echo var1 before: %var1%
call :myDosFunc var1
Echo var1 after : %var1%
goto :eof
:myDosFunc -- passing a variable by reference, and changing value
set "VarValue=%~1"
set "VarVal=%~1%"
echo Arg1 variable name = '%~1' and to get value, tried this '%%%VarValue%%%' and this '%%VarValue%%' and this '%VarValue%' and this '%%%VarVal%%%' and this '%%VarVal%%' and this '%VarVal%'
set "%~1=Good bye world!"
goto :eof