Click or drag to resize

HostFunctionsnewVarT Method

Creates a host variable of the specified type.

Namespace: Microsoft.ClearScript
Assembly: ClearScript.Core (in ClearScript.Core.dll) Version: 7.4.5
Syntax
public Object newVar<T>(
	T initValue = null
)

Parameters

initValue  T  (Optional)
An optional initial value for the variable.

Type Parameters

T
The type of variable to create.

Return Value

Object
A new host variable of the specified type.
Remarks

A host variable is a strongly typed object that holds a value of the specified type. Host variables are useful for passing method arguments by reference. In addition to being generally interchangeable with their stored values, host variables support the following properties:

PropertyAccessDescription
valueread-writeThe current value of the host variable.
outread-onlyA reference to the host variable that can be passed as an out argument.
refread-onlyA reference to the host variable that can be passed as a ref argument.

Example
The following code demonstrates using a host variable to invoke a method with an out parameter. It assumes that an instance of ExtendedHostFunctions is exposed under the name "host" (see AddHostObject).
JavaScript
// import a dictionary type
var StringT = host.type("System.String");
var StringDictT = host.type("System.Collections.Generic.Dictionary", StringT, StringT);
// create and populate a dictionary
var dict = host.newObj(StringDictT);
dict.Add("foo", "bar");
dict.Add("baz", "qux");
// look up a dictionary entry
var result = host.newVar(StringT);
var found = dict.TryGetValue("baz", result.out);
See Also