Diese Seite mit anderen teilen ...

Informationen zum Thema:
Forum:
WinDev Forum
Beiträge im Thema:
7
Erster Beitrag:
vor 1 Jahr, 3 Monaten
Letzter Beitrag:
vor 1 Jahr, 3 Monaten
Beteiligte Autoren:
Piet van Zanten, Marco Antonio Machado, Allard, Stefan Bentvelsen, Curtis, DW, Gus

[WD20] How to check if an element exist in an Associative Array with structures

Startbeitrag von Gus am 11.05.2016 17: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

Antworten:

More code please! :xcool:

von Curtis - am 11.05.2016 18:54
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

von DW - am 11.05.2016 18:56
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 ...

von Stefan Bentvelsen - am 11.05.2016 19:03
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

von Allard - am 15.05.2016 17:42
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

von Piet van Zanten - am 16.05.2016 08:00
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

von Marco Antonio Machado - am 17.05.2016 14:03
Zur Information:
MySnip.de hat keinen Einfluss auf die Inhalte der Beiträge. Bitte kontaktieren Sie den Administrator des Forums bei Problemen oder Löschforderungen über die Kontaktseite.
Falls die Kontaktaufnahme mit dem Administrator des Forums fehlschlägt, kontaktieren Sie uns bitte über die in unserem Impressum angegebenen Daten.