CValueSet:new


The new method creates an instance of the CValueSet class and returns a reference to the object. Each member of CValueSet is a pair consisting of a string key and a value of type "number", "integer", "string", or "boolean". The new CValueSet may be initialized to a specific value type, an existing CValueSet, a lua table of pairs, or to the default value type of "number". Note that the object can also be created using new_valueset.

Note: This method's name uses all lower case to present it as the analog of the C++ new operator. Generally, the names of class methods begin with an uppercase letter but new and delete are different.

Syntax

S = CValueSet:new()

bullet.gif    Default constructor. The value type is set "number" by default.

S = CValueSet:new("string")

bullet.gif    Constructs a CValueset object and sets the value type to "string".

S = CValueSet:new( CValueSetOld )

bullet.gif    Constructs a new CValueSet S and initializes it to the members of the existing CValueSetOld argument.

S = CValueSet:new( tbl )

bullet.gif    Constructs a new CValueSet S from a lua table of pairs. The first member of each pair is the string kley and the second member is the value of type "number", "integer", "string", or "boolean".

bullet.gif    On failure, nil is returned.

Example

The following example shows the use of a default constructor. This defaults the value type to "number":

S = CValueSet:new()

-- create a new instance S of the CValueSet class.

  ...

-- other uses of the class go here, between new and delete.

S:delete()

-- deletes the object and its associated memory.

 

The following example creates a CValueSet S, adds members to it, then creates a second CValueSet containing the same members as the first set:

S = new_valueset()

-- creator, default to values of type "number"

S:Add( "my first string", 15.5 )

-- add a key and value

S:Add( "my second string", -7125.23 )

-- add a key and value

S:Add( "my third string", 0.923 )

-- add a key and value

Printf("Count= %d", S:Count())

-- result: Count = 3

S2 = CValueSet:new(S)

-- create S2 and initialize it to members of S

Printf("Count= %d", S2:Count())

-- result: Count = 3

Printf("Type= %s", S:GetType())

-- result: "number"

The following example creates a new CValueSet from a lua table of pairs. Each pair contains a key and a value. In this example, the values are strings. The new CValueSet:new() method determines that the values are strings by determining the type of value in the first member of the lua table:

tbl = {}

-- create a lua table

tbl[1] = {"first key", "Michael"}

--Assign a key and value to table[1]

tbl[2] = {"second key", "Rachel"}

-- Assign a key and value to table[2]

tbl[3] = {"third key", "Reginald"}

-- Assign a key and value to table[3]

tbl[4] = {"fourth key", "Cameron"}

-- Assign a key and value to table[4]

S = CValueSet:new(tbl)

-- create S from the lua table

Printf("Count= %d", S:Count())

-- result: Count = 4

Printf("Type= %s", S:GetType())

-- result: "string"

Related Topics

CValueSet Class, Copy, delete, CSet, new_valueset


Mira Pro x64 Script User's Guide, Copyright Ⓒ 2024 Mirametrics, Inc. All Rights Reserved.