Associative Array Problem

Simen Kjaeraas simen.kjaras at gmail.com
Mon Mar 24 12:52:39 PDT 2008


On Mon, 24 Mar 2008 20:29:23 +0100, Wolftousen Frozenwind  
<eliot.darkwolf at gmail.com> wrote:

> I'm trying to have an associative array with the key value of type  
> ushort.
> \
> struct struct_name
> {
>      int variable_name;
> }
>
> ushort[some_number] keys;
>
> //fill keys
>
> //declare my associative array
> struct_name[ushort] data;
>
> //in a loop, filling data
> for(x = 0; x < keys.length; x++)
>      data[keys[x]].variable_name = some_int;
>
> I get an array out of bounds error when running this.  Any one help?


My guess would be that data[keys[x]] does not yet exist when you attempt  
to set its member variable_name.
If I am correct, this should work instead:

for(x = 0; x < keys.length; x++)
{
      data[keys[x]] = struct_name(); // call static opCall or other init  
function here
      data[keys[x]].variable_name = some_int;
}


More information about the Digitalmars-d-learn mailing list