PC SOFT

FOROS PROFESIONALES
WINDEVWEBDEV y WINDEV Mobile

Inicio → WINDEV 2024 → [WD20] How to check if an element exist in an Associative Array with structures
[WD20] How to check if an element exist in an Associative Array with structures
Iniciado por guest, 11,may. 2016 19:14 - 6 respuestas
Publicado el 11,mayo 2016 - 19:14
Hello All:

I have an Associative Array with Structures and I have problem when I try to access an element which doesn't exist. For example, if I do;
myArray[mykey].member
gives me an error if mykey is not yet in the array...

The problem, however, is how to check if the key element exist before trying the access. I'm using a FOR EACH loop with Key = mykey and checking if there is any iteration after it, but I would like to have a more efficient way to check if the element exist.

I would appreciate any help or comment.

Thanks in advance
Gus
Publicado el 11,mayo 2016 - 20:54
More code please! :xcool:
Publicado el 11,mayo 2016 - 20:56
Hello Gus,

Something like this :

I am look for a Techs Name by a job number. Seek the element is the array first nRow is int = ArraySeek(:m_arrCalData,asLinearFirst,"JobNumber",nJn) Then : IF nRow > -1 THEN sTechName = :m_arrCalData[nRow].SFieldTech END
Hope this helps

Dennis
Publicado el 11,mayo 2016 - 21:03
Hi Gus,

I use :

IF NOT myArray[MyKey]..empty then
// do your stuff
END

but I don't know if it is usable with associative arrays of structures ...
Publicado el 15,mayo 2016 - 19:42
No problebly not

You would have to loop through

FOR EACH ELEMENT OF Associative array

END

just type in for in de code editor intelli stuff will help a lot


regards
Allard
Publicado el 16,mayo 2016 - 10:00
Hi Gus,

If it becomes too complicated to prevent an error you can always use "WHEN Exception IN" to handle it.
Other option would be change the associative array into a normal one with MyKey as a member and use ArraySeek.

Best regards,
Piet
Publicado el 17,mayo 2016 - 16:03
Hi Gus

With Associative Array with structures I have been working like this :

if myAssArray[idx]..Occurence = 0 then
// There's not this idx instance
end

I have associative arrays of structures with normal idx as integer or structures.

Best regards
Marco Machado