How do I dynamically set the type of PowerShell variables? -


is possible system.type string , apply variable?

the following function tried convert make safer (i.e. not involve invoking string):

function parseline($type, $variablename, $value){     invoke-expression "[$type] `$$variablename = $value" } 

i looked @ new-variable , set-variable, there no type-setting-related parameter in definition.

i expected looks below, couldn't find parameter type or equivalent:

function parseline($type, $variablename, $value){     new-variable -name $variablename -value $value -type ([type] $type) } 

context: experimenting create simple parser definition looks like:

$responselogformat = new-inputformat {     parseline -type int -variablename recordlength     repeatedsection -repeatcount $recordlength {         parseline string +computername         parseline double +averageresponse     } }  $responselogformat.parse( $filepath ) 

you can cast variables specific type using -as operator:

function parseline($type, $variablename, $value){     set-variable $variablename -scope 1 -value ($value -as ($type -as [type])) } 

that use -as create type $type string, , use cast $value.

i not sure of intent variable, if want persist after function finished, need set in parent scope.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -