PC SOFT

FOROS PROFESIONALES
WINDEVWEBDEV y WINDEV Mobile

Inicio → WINDEV 2024 → [WD21] Comparing instances of Structure variables
[WD21] Comparing instances of Structure variables
Iniciado por guest, 20,sep. 2016 01:39 - 14 respuestas
Publicado el 20,septiembre 2016 - 01:39
Hi,

If I have 2 instances of a Structure (both created from the same Structure variable); i.e. StructOLD and StructNEW and I want to compare them to see if they are different, what is the syntax?

I've tried:

If StructOLD <> StructNEW THEN . . . END
... I've traced my code, looked at the values and they look identical, but the flow of the code goes INTO the IF statement which I don't expect.

Am I using the correct syntax to compare 2 Structures? ...or do I need to compare each Member of both Structure instances to deduce their difference(s)?

Thanks in advance...
Publicado el 20,septiembre 2016 - 01:58
Darren,
This page shows how dynamic structures can be compared
http://doc.windev.com/en-US/…

HTH
Steve H
Publicado el 20,septiembre 2016 - 09:35
Hi Darren,
change your logic and use "=" instead of "<>"

Steven Sitas
www.alpha360.biz
Publicado el 20,septiembre 2016 - 09:51
Hi Steve,

Yep, I saw that. My structure isn't defined as dynamic. I've never used structures before and Dynamic structures are just overly complex for what I'm trying to achieve - just thought I'd try to keep things simple.

So if I say:

MyStruct is Structure Fld1 is string Fld2 is Int Fld3 is Numeric END StructOLD is MyStruct StructNEW is MyStruct
I then set the values of StructOLD and StructNEW - to begin with, just so I can test the syntax, I set both instances of the structure to the same values. When I look in trace in both Text and Hex - both instances of the structure are identical ...then later in the code I do (as above):

If StructOLD <> StructNEW THEN . . . END
...why doesn't it work as expected? ...OR is it just NOT possible to compare the 2 instances in the way I'm trying to? :confused:
Publicado el 20,septiembre 2016 - 09:55
Hi Steven,

Now I'm officially confused!

If I change the logic to "=", it skips over the IF statement even tho they are =. It's doing the opposite to what I expect :confused:

Is it looking at the structure names when doing the comparison and not the contents of the structures??? :confused:
Publicado el 20,septiembre 2016 - 10:16
Hi Darren,

interesting, so I did a small test and is does look like comparison isn't working at all !!
Maybe you can ask pcsoft if it is a bug or not supported?

One way to overcome this is to use the Serialize() function and transform you structure into a xml or json string. Then you can compare it as expected. This looks like a bit of overkill if you want, on the other hand the Windev framework has to do something similar behind the scenes. I use this myself to do a search in 1000+ instances of a structure, looking for a specific text, and there is no significant delay.
Publicado el 20,septiembre 2016 - 11:26
Hi Arie,

Thanks for taking the time to check this out; I thought I might have to go back to programming school! ;)

I'll set up a quick project (as you probably did) just to put my mind at rest if nothing else and I will indeed contact PCSoft with regards to this... in all my spare time! :rolleyes:

Thanks again...
Publicado el 20,septiembre 2016 - 11:54
Hi Darren,
I just checked my notes and found that I had also seen this sometime ago.
It doesn't work the way you would expect it ...
So use Steve Hodgmans and Aries solutions - for a workaround.

BUT if you are using Record Structures you can use "myRecord..content" and do comparisons.

Steven Sitas
www.alpha360.biz
Publicado el 20,septiembre 2016 - 12:17
Steven,

Thanks for that... In my naivety, I was just looking for (what I thought would be) a simple way to compare 2 separate structures with a simple IF statement and without having to compare each member; that'll teach me for trying to use "advanced" techniques!? ;)

Ah well, back to old-school checking each member I suppose! :rolleyes: I don't have time to ponder on this and I won't be using dynamic structures; not worth the effort in my opinion + checking each member keeps it easy to read and maintainable.

As always - thanks again guys...
Publicado el 20,septiembre 2016 - 12:37
Darren,

something like this :

buObjA, bufObjC are Buffers //Serialize puts it in a format so we can compare the 2 arrays Serialize(:m_arrCalData,buObjA,psdBinary) Serialize(:m_arrCalDataComp,bufObjC,psdBinary) //Compare the arrays IF buObjA <> bufObjC THEN :m_bNeedsUpCal = True END
The arrays are structures.
Publicado el 20,septiembre 2016 - 13:01
Thanks - I might give Serialize a quick try :spos:
Publicado el 20,septiembre 2016 - 16:19
Darren
Late to the game here and a pretty similar theme but this is my solution...

Declare 2 variants on your window
v1,v2 is Variant

Assign the structures to these and pass as parameters
v1 = StructOLD; v2 = StructNEW

to a global procedure (or Class method)
IF gpCompare(v1,v2) = True THEN
//Do something
ELSE
//Do something else
END

Global Proc

PROCEDURE Compare(V1,V2 is Variant)
r1 is boolean
j1,j2 is string

j1 = VariantToJSON(V1); j2 = VariantToJSON(V2)

IF StringCompare(j1,j2) = 0 THEN
r1 = True
END

RESULT r1

Note that if you have set up arrays of the structures these can also be sent as parameters for comparison.
Publicado el 20,septiembre 2016 - 19:11
Thanks guys - using Serialize worked! ...only needed 3 new lines of code and a change to the IF statement :xcool:

Still don't think it should be this difficult to compare 2 structures , so I'll ask PCS if a straight comparison of Structures "should" work... :confused:
Publicado el 22,septiembre 2016 - 01:51
Darren,
Just been following the posts on this so I decided to try the code as per the documentation.
Not only is the syntax wrong but the code does not work and seems incorrect anyway.
I have posted a question with an example window to Tech Support to get clarification.
I will let you know the outcome.
---
Steve H.
Publicado el 22,septiembre 2016 - 09:20
Cheers Steve :cheers: