Syntax
Creates a fresh record of a TYPE and passes the arguments to its New method, the constructor, if it has one.
NEW <Type>[(<arguments>)]
<name> AS <Type> = NEW <Type>(<arguments>)
METHOD New([<parameter> = <default>, …])Parts
| <Type> | A TYPE declared in the program. An unknown name is a compile error: "Unknown BASIC type". |
|---|---|
| <arguments> | Values passed to the New method. Without parentheses, New runs with no argument. |
| <name> | The variable that receives the record. |
| <parameter> | A parameter of the constructor. |
| <default> | Its default value. Give one to every parameter of New. |
Good to know
NEWworks in a declaration, in a later assignment (b = NEW Counter(2)) and as a call argument.- Every other creation calls
Newwith no argument:x AS T, each cell ofDIM a(n) AS T, AddElement() on a record list, a new map entry, a record field of that type. - So give every parameter of
Newa default value. A typed parameter without a default stops the program ("BASIC integer requires an integral value"), and an untyped one leaves the field without a value. - A copy does not run
New:b = aandx AS T = othercopy the fields as they are. - There is no
AllocateStructure(PureBasic): writep AS PointorNEW Point().
Example
TYPE Counter value AS integer METHOD New(start AS integer = 0) SELF.value = start ENDMETHODENDTYPEa AS Counterb AS Counter = NEW Counter(7)a = NEW Counter(2)Console(Str(a.value) + " " + Str(b.value)) ' 2 7In the manual : Records and methods