xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

HSetAutoAdd()

Changes the AutoAdd attribute of a hash.

Syntax

HSetAutoAdd( <hHash>, <lOnOff> ) --> hHash

Arguments

<hHash>
A variable referencing the hash whose AutoAdd attribute is changed.
<lOnOff>
A logical value. .T. (true) enables automatic creation of key/value pairs, and .F. (false) disables it.

Return

The function returns a reference to <hHash>.

Description

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.

Info

See also:Hash(), HGetAutoAdd(), HGetCaseMatch(), HSet(), HSetCaseMatch()
Category: Hash functions , xHarbour extensions
Source:vm\hash.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

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

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe