| xHarbour Reference Documentation > Function Reference |
![]() |
![]() |
![]() |
Changes the AutoAdd attribute of a hash.
HSetAutoAdd( <hHash>, <lOnOff> ) --> hHash
The function returns a reference to <hHash>.
The function changes an attribute of a hash that identifies whether or not automatic creation of key/value pairs is permitted in the context of inline assignments or the Hset() function.
By default, this attribute is .T. (true) when a new hash is created. This causes the assignment of a value to a non-existent key to create a new key/value pair in the hash. This default behaviour can be switched off to prevent automatic creation of new key/value pairs.
Use function HGetAutoAdd() to retrieve the current setting of this attribute.
| See also: | Hash(), HGetAutoAdd(), HGetCaseMatch(), HSet(), HSetCaseMatch() |
| Category: | Hash functions , xHarbour extensions |
| Source: | vm\hash.c |
| LIB: | xhb.lib |
| DLL: | xhbdll.dll |
// The example demonstrates the automatic creation of key/value
// pairs when a value is assigned to a non-existent key, and how
// to prevent this.
PROCEDURE Main
// create an empty hash
LOCAL hHash := {=>}
// Populating the hash:
hHash[ 'first key' ] := 'first value'
hHash[ 'second key' ] := 'second value'
hHash[ 'third key' ] := 'third value'
? Len( hHash ) // result: 3
HSetAutoAdd( hHash, .F. )
hHash[ 'fourth key' ] := 'fourth value' // runtime error
RETURN
http://www.xHarbour.com