Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 107,668 VB Programmers for FREE! Ask your question and get quick answers from experts. There are 1,043 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



VB6 user defined types comparison

2 Pages V  1 2 >  
Reply to this topicStart new topic

VB6 user defined types comparison, How to evaluate if two sets of a user defined type are equal

Rating  5
jens
post 6 Aug, 2008 - 04:31 AM
Post #1


New D.I.C Head

Group Icon
Joined: 9 May, 2008
Posts: 49



Thanked 1 times

Dream Kudos: 50
My Contributions


I have defined a datatype

CODE

Public Type palletType
   palletFileVersion As Integer
   typeOfPallet As Integer
   Assembly  As assemblyType
End Type


assemblyType is also a type that I have defined. The definition is not of interest here why I omit it.

Now, suppose I do this:
CODE

dim pallet1 as palletType
dim pallet2 as palletType

pallet1.palletFileVersion = 1
pallet1.typeOfPallet = ....
'And all other fields too

pallet2.palletFileVersion = 1
pallet2.typeOfPallet = ....
'And all other fields too


Later on in my code I want to know if the contents of pallet1 are the same as the contents of pallet2 so I do this:
CODE

If (pallet1 = pallet2) then.... 'Do somthing


This (above) doesn't work!
I get a "compile error, type mismatch" which points to the equal sign.

How can I, if possible at all, compare the two sets of data without explicitly comparing each data field?

Regards
Jens
User is offlineProfile CardPM

Go to the top of the page


PsychoCoder
post 6 Aug, 2008 - 06:46 PM
Post #2


DIC.Rules == true;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,143



Thanked 50 times

Dream Kudos: 7700

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI

My Contributions


Try using the Is keyword instead of a comparison operator. Something along the lines of

vb

If pallet1 Is pallet2 then.... 'Do somthing
User is online!Profile CardPM

Go to the top of the page

jens
post 7 Aug, 2008 - 02:19 AM
Post #3


New D.I.C Head

Group Icon
Joined: 9 May, 2008
Posts: 49



Thanked 1 times

Dream Kudos: 50
My Contributions


My problem is that my types are not objects (this is in VB6) and the help says
QUOTE

If object1 and object2 both refer to the same object, result is True; if they do not, result is False. Two variables can be made to refer to the same object in several ways.


Also, if I understand the above correctly the Is thingy compares references while I want to do a "deep camparison" i.e. I want to compare all fields of my type.

I guess I'll have to do it the hard way and compare field by field.

Maybe I'm approaching this the wrong way. The task at hand is actually to determine wheter or not a user have changed any data in the data type or not. The user is presented with a screen where current data is displayed and has the option to change any data in the type by changing fields in a form. When done with the form the user presses "exit" or somthing like that. Right then I have to know (and take care) of any changes made.

Could I somehow traverse the type with all its subtypes and make a checksum and compare checksum before with checksum after to know wheter or not data has changed? Other ideas?

Thanks for your time!
/Jens
User is offlineProfile CardPM

Go to the top of the page

AdamSpeight2008
post 7 Aug, 2008 - 02:36 AM
Post #4


D.I.C Regular

Group Icon
Joined: 29 May, 2008
Posts: 493



Thanked 31 times

Dream Kudos: 1900
My Contributions


vb

' Enter the following Declare statement entirely as one, single line:
Declare Sub hmemcpy Lib "kernel" (hpvDest As Any, hpvSource As Any,
ByVal cbCopy As Long)

Public Type palletType
palletFileVersion As Integer
typeOfPallet As Integer
Assembly As assemblyType
End Type

' type2str converts a user-defined type variable to a string.
Function type2str (t As myType) As String
Dim s As String
s = Space$(Len(t))
Call hmemcpy(ByVal s, t, Len(t))
type2str = s
End Function



vb

if type2str(pallet1)=type2str(pallet2) Then
' etc
endif


I claim not to have written to code as it was lifted straight from this page.
How to compare User-Defined Types In Visual Basic (Microsoft)

Edited Text: Select view plain to show the correct structure of the code. (Has been reported)

This post has been edited by AdamSpeight2008: 7 Aug, 2008 - 03:02 AM
User is offlineProfile CardPM

Go to the top of the page

jens
post 7 Aug, 2008 - 05:11 AM
Post #5


New D.I.C Head

Group Icon
Joined: 9 May, 2008
Posts: 49



Thanked 1 times

Dream Kudos: 50
My Contributions


Thank you very much!

With your information and MS Win32 replacement för hmemcopy which states that
CODE

Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" ( hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

can be used with KERNEL32 instead of hmemcopy with KERNEL my problem is solved! Great!

smile.gif

/Jens
User is offlineProfile CardPM

Go to the top of the page

jens
post 7 Aug, 2008 - 07:51 AM
Post #6


New D.I.C Head

Group Icon
Joined: 9 May, 2008
Posts: 49



Thanked 1 times

Dream Kudos: 50
My Contributions


crazy.gif

Unfortunately I was a little bit too quick in my judgement....

I started to make a snippet out of this but... As I tested this I found that changes in the string parts of the user defined type doesn't get detected. If I change any integer it's detected. Any clues?

CODE

'============================================================
' The code below is for VB6
' It is an example of how you can compare two variables of
' a user defined type to see if they contain the same data.
' (This can not be done by using "=" or "Is")
' Note that it works with nested user defined types! :-)
' You need one form and one module
'============================================================


'============================================================
'  Put the following code in the main module, e.g. "Module1"
'============================================================

''============================================================
'' http://support.microsoft.com/kb/88551
'' http://support.microsoft.com/kb/129947/en-us
'' This declaration is needed for the function type2str below
'' to work.
''------------------------------------------------------------
Declare Sub mcpy Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)


''============================================================
'' Definition of user defined types
''------------------------------------------------------------
Public Type InnerTestType
  innerNumber As Integer
  innerString As String
End Type

Public Type TestType
  innerVar As InnerTestType
  outerNumber As Integer
  outerString As String
End Type


''============================================================
'' Public Function type2str(indata As TestType) As String
'' Converts a variable of the user defined type "TestType"
'' to a string.
''------------------------------------------------------------
Public Function type2str(indata As TestType) As String
      Dim s As String
      s = Space(Len(indata))
      Call mcpy(ByVal s, indata, Len(indata))
      type2str = s
End Function


''============================================================
''  Put the following code in the Form_Load event for your form
''============================================================
Private Sub Form_Load()
  Dim first As TestType
  Dim second As TestType

  first.innerVar.innerNumber = 11
  first.innerVar.innerString = "Hello "
  first.outerNumber = 12
  first.outerString = "all!"

  second = first
  If type2str(first) = type2str(second) Then MsgBox ("1: first = second") Else MsgBox ("1: first <> second")
  
  second.innerVar.innerNumber = 1
  If type2str(first) = type2str(second) Then MsgBox ("2: first = second") Else MsgBox ("2: first <> second")

  second.innerVar.innerNumber = 11
  If type2str(first) = type2str(second) Then MsgBox ("3: first = second") Else MsgBox ("3: first <> second")

  second.outerString = "Tjoppelibopp"
  If type2str(first) = type2str(second) Then MsgBox ("4: first = second") Else MsgBox ("4: first <> second")

  second.outerString = "all!"
  If type2str(first) = type2str(second) Then MsgBox ("5: first = second") Else MsgBox ("5: first <> second")

  Unload Me
End Sub


/Jens
User is offlineProfile CardPM

Go to the top of the page

jens
post 7 Aug, 2008 - 08:05 AM
Post #7


New D.I.C Head

Group Icon
Joined: 9 May, 2008
Posts: 49



Thanked 1 times

Dream Kudos: 50
My Contributions


Seems like the string conversion doesn't work correctly. Anyone has any ideas of how to convert a user defined type to string or any other type (BLOB?) that can be compared in an easy way?

/Jens
User is offlineProfile CardPM

Go to the top of the page

AdamSpeight2008
post 7 Aug, 2008 - 08:45 AM
Post #8


D.I.C Regular

Group Icon
Joined: 29 May, 2008
Posts: 493



Thanked 31 times

Dream Kudos: 1900
My Contributions


QUOTE(jens @ 7 Aug, 2008 - 04:05 PM) *

Seems like the string conversion doesn't work correctly. Anyone has any ideas of how to convert a user defined type to string or any other type (BLOB?) that can be compared in an easy way?

/Jens

It works for me.
User is offlineProfile CardPM

Go to the top of the page

jens
post 7 Aug, 2008 - 01:12 PM
Post #9


New D.I.C Head

Group Icon
Joined: 9 May, 2008
Posts: 49



Thanked 1 times

Dream Kudos: 50
My Contributions


Have you tried my code (above)?
These lines
CODE

second.outerString = "Tjoppelibopp"
  If type2str(first) = type2str(second) Then MsgBox ("4: first = second") Else MsgBox ("4: first <> second")

should report "4: first <> second" but when I run it I get "4: first = second"

Now I'm really lost. sad.gif

/Jens
User is offlineProfile CardPM

Go to the top of the page

AdamSpeight2008
post 7 Aug, 2008 - 03:42 PM
Post #10


D.I.C Regular

Group Icon
Joined: 29 May, 2008
Posts: 493



Thanked 31 times

Dream Kudos: 1900
My Contributions


QUOTE(jens @ 7 Aug, 2008 - 09:12 PM) *

Have you tried my code (above)?
These lines
CODE

second.outerString = "Tjoppelibopp"
  If type2str(first) = type2str(second) Then MsgBox ("4: first = second") Else MsgBox ("4: first <> second")

should report "4: first <> second" but when I run it I get "4: first = second"

Now I'm really lost. sad.gif

/Jens

I get the same as you.
Know the cause now as well, would you like you like to know?

The problem is caused by you defining in type with variable length string, by changing to fixed length it will work correctly.
vb

Public Type InnerTestType
innerNumber As Integer
innerString As String * 40
End Type

Public Type TestType
innerVar As InnerTestType
outerNumber As Integer
outerString As String * 40
End Type

This will set the string to have a length of 40 characters.
User is offlineProfile CardPM

Go to the top of the page

Ken Halter
post 7 Aug, 2008 - 09:42 PM
Post #11


New D.I.C Head

*
Joined: 18 Nov, 2007
Posts: 31



Thanked 4 times
My Contributions


Try using LenB instead of Len to get the size of the UDT.

CODE

   Dim ThisString As String
  
   ThisString = "This is a test"
   Debug.Print Len(ThisString), LenB(ThisString)
   'the line above shows 14 (Len) and 28 (LenB)


Strings became "hard to handle" with the introduction of Unicode (aka Unimess)

As a last resort, you can use a byte array everywhere, instead of the string... but that requires changes thru-out (can be worth it in the long run)

CODE

   Dim ThisString As String
   ThisString = "This is a test"

   'then... sometime later....

   Dim theByteArray() As Byte
   'Create the byte array - one byte per character
   theByteArray = StrConv(ThisString, vbFromUnicode)
  
   'whenever you need to, show the byte array as a string
   Dim ThatString As String
   ThatString = StrConv(theByteArray, vbUnicode)
   MsgBox ThatString, vbInformation

   'I realize you're asking about UDTs but the process is the same, so...

User is online!Profile CardPM

Go to the top of the page

jens
post 8 Aug, 2008 - 12:10 AM
Post #12


New D.I.C Head

Group Icon
Joined: 9 May, 2008
Posts: 49



Thanked 1 times

Dream Kudos: 50
My Contributions


Hello!

Ken: Thank you for your time. Unfortunately the LenB thing didn't help and the other alternative you suggested would complicate my project too much.

Adam: First of all: Thanks - I'm impressed. This made my day. smile.gif
Would you care to elaborate a little on the "* 40" part of your solution? I do understand that it changes from variable to fixed length but I've never seen this done like this before.

I'll put the complete solution up at DIC as a snippet.

/Jens
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 8/29/08 10:53PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month