Diese Seite mit anderen teilen ...

Informationen zum Thema:
Forum:
WinDev Forum
Beiträge im Thema:
17
Erster Beitrag:
vor 1 Jahr, 2 Monaten
Letzter Beitrag:
vor 1 Jahr, 1 Monat
Beteiligte Autoren:
DarrenF, Steve Hodgman, Steven Sitas, DerekT, DW, Arie

[WD21] Comparing instances of Structure variables [Solved'ish]

Startbeitrag von DarrenF am 19.09.2016 23: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...

Antworten:

Re: [WD21] Comparing instances of Structure variables

Darren,
This page shows how dynamic structures can be compared
http://doc.windev.com/en-US/?1512006&product=WM&productversion=XXA150.&verdisp=210

Relevant piece:
....
// Declare the structures
O is ST1
P is ST2

// Declare and instantiate the dynamic structures
P1 is dynamic Structure ST1
P1 = O
P2 is dynamic Structure ST2
P2 = O

// Comparison
IF P1 = P2 THEN...
.....

---------
Steve H

von Steve Hodgman - am 19.09.2016 23:58

Re: [WD21] Comparing instances of Structure variables

Hi Darren,
change your logic and use "=" instead of ""

Steven Sitas
www.alpha360.biz

von Steven Sitas - am 20.09.2016 07:35

Re: [WD21] Comparing instances of Structure variables

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:

von DarrenF - am 20.09.2016 07:51

Re: [WD21] Comparing instances of Structure variables

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:

von DarrenF - am 20.09.2016 07:55

Re: [WD21] Comparing instances of Structure variables

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.

von Arie - am 20.09.2016 08:16

Re: [WD21] Comparing instances of Structure variables

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

von DarrenF - am 20.09.2016 09:26

Re: [WD21] Comparing instances of Structure variables

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

von Steven Sitas - am 20.09.2016 09:54

Re: [WD21] Comparing instances of Structure variables

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

von DarrenF - am 20.09.2016 10:17

Re: [WD21] Comparing instances of Structure variables

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.

von DW - am 20.09.2016 10:37

Re: [WD21] Comparing instances of Structure variables

Thanks - I might give Serialize a quick try :spos:

von DarrenF - am 20.09.2016 11:01

Re: [WD21] Comparing instances of Structure variables

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.

von DerekT - am 20.09.2016 14:19

Re: [WD21] Comparing instances of Structure variables

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:

von DarrenF - am 20.09.2016 17:11

Re: [WD21] Comparing instances of Structure variables

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.

von Steve Hodgman - am 21.09.2016 23:51

Re: [WD21] Comparing instances of Structure variables

Cheers Steve :cheers:

von DarrenF - am 22.09.2016 07:20

Re: [WD21] Comparing instances of Structure variables

Darren,
Sorry for delay in updating you. I missed PC Soft reply.
They have confirmed that comparing Structures does not work as documented and have passed it on to the developers.
They say "I'll keep you posted" so here's hoping.
---
Steve H

von Steve Hodgman - am 04.10.2016 23:49

Re: [WD21] Comparing instances of Structure variables

Steve,

No problem - thanks for doing that.
Looks like we'll need an upgrade to the new v22 for the fix? ;)

von DarrenF - am 05.10.2016 07:35
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.