ClearScript Library
ClearScript Library
ClearScript Library Reference
Microsoft.ClearScript
HostFunctions Class
HostFunctions Methods
asType<T> Method
cast<T> Method
del<T> Method
flags<T> Method
func Method
getElement Method
getProperty Method
isNull Method
isType<T> Method
isTypeObj Method
newArr Method
newObj Method
newVar<T> Method
proc Method
removeElement Method
removeProperty Method
setElement Method
setProperty Method
toByte Method
toChar Method
toDecimal Method
toDouble Method
toInt16 Method
toInt32 Method
toInt64 Method
toSByte Method
toSingle Method
toStaticType Method
toUInt16 Method
toUInt32 Method
toUInt64 Method
tryCatch Method
typeOf Method

# HostFunctionsnewVar T Method

Creates a host variable of the specified type.
Namespace: Microsoft.ClearScript
Assembly: ClearScript.Core (in ClearScript.Core.dll) Version: 7.5.0
Syntax
C#
VB
C++
F#
Copy

    public Object newVar<T>(
    	T initValue = null
    )

    Public Function newVar(Of T) (
    	Optional initValue As T = Nothing
    ) As Object

    public:
    generic<typename T>
    Object^ newVar(
    	T initValue = nullptr
    )

    member newVar :
            ?initValue : 'T
    (* Defaults:
            let _initValue = defaultArg initValue null
    *)
    -> Object

#### 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:

Property | Access | Description
--- | --- | ---
value | read-write | The current value of the host variable.
out | read-only | A reference to the host variable that can be passed as an out argument.
ref | read-only | A 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
Copy

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

#### Reference

HostFunctions Class
Microsoft.ClearScript Namespace
type(String, Object)

Copyright © Microsoft Corporation. All rights reserved.

Send comments on this topic to Microsoft