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
public Object newVar<T>(
	T initValue = null


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

Type Parameters

The type of variable to create.

Return Value

A new host variable of the specified type.

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:

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.

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).
// 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