VB6 user defined types comparison

How to evaluate if two sets of a user defined type are equal

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 9262 Views - Last Post: 08 August 2008 - 12:31 AM Rate Topic: ***** 1 Votes

#1 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

VB6 user defined types comparison

Post icon  Posted 06 August 2008 - 04:31 AM

I have defined a datatype

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

Is This A Good Question/Topic? 0
  • +

Replies To: VB6 user defined types comparison

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: VB6 user defined types comparison

Posted 06 August 2008 - 06:46 PM

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

If pallet1 Is pallet2 then.... 'Do somthing


Was This Post Helpful? 1
  • +
  • -

#3 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: VB6 user defined types comparison

Posted 07 August 2008 - 02:19 AM

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
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is online

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: VB6 user defined types comparison

Posted 07 August 2008 - 02:36 AM

   ' 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




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: 07 August 2008 - 03:02 AM

Was This Post Helpful? 1
  • +
  • -

#5 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: VB6 user defined types comparison

Posted 07 August 2008 - 05:11 AM

Thank you very much!

With your information and MS Win32 replacement fr hmemcopy which states that
 
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!

:)

/Jens
Was This Post Helpful? 0
  • +
  • -

#6 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: VB6 user defined types comparison

Posted 07 August 2008 - 07:51 AM

:crazy:

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?

'============================================================
' 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
Was This Post Helpful? 0
  • +
  • -

#7 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: VB6 user defined types comparison

Posted 07 August 2008 - 08:05 AM

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
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is online

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: VB6 user defined types comparison

Posted 07 August 2008 - 08:45 AM

View Postjens, on 7 Aug, 2008 - 04:05 PM, said:

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.
Was This Post Helpful? 1
  • +
  • -

#9 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: VB6 user defined types comparison

Posted 07 August 2008 - 01:12 PM

Have you tried my code (above)?
These lines
 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. :(

/Jens
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is online

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: VB6 user defined types comparison

Posted 07 August 2008 - 03:42 PM

View Postjens, on 7 Aug, 2008 - 09:12 PM, said:

Have you tried my code (above)?
These lines
 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. :(

/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.
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.
Was This Post Helpful? 1
  • +
  • -

#11 Ken Halter  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 35
  • Joined: 18-November 07

Re: VB6 user defined types comparison

Posted 07 August 2008 - 09:42 PM

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

   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)

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



Was This Post Helpful? 0
  • +
  • -

#12 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: VB6 user defined types comparison

Posted 08 August 2008 - 12:10 AM

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. :)
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
Was This Post Helpful? 0
  • +
  • -

#13 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: VB6 user defined types comparison

Posted 08 August 2008 - 12:17 AM

Finally, to all:

I rated this thread 5 stars since I think it is a great example of what sharing knowledge is about. I had this irritating and quite nasty problem. I asked, got help, found new problems and got more help and so forth until the problem was solved. I think even I my self contributed a little, which just makes it feel even better.

I hereby express my gratitude to all who help others solving their problems, big or small. Being lost and getting good help is such a treat!

Thanks guys! This includes the DIC team too! :)
/Jens

This post has been edited by jens: 08 August 2008 - 12:24 AM

Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is online

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: VB6 user defined types comparison

Posted 08 August 2008 - 12:20 AM

The 40 was just chosen at random.
Other Examples
Dim 1CharStr as string * 1
Dim 2CharStr as String * 2
Dim 3CharStr as string * 3
etc
Dim 40CharStr As string *40
etc
Dim 256CharStr as string *256


Was This Post Helpful? 0
  • +
  • -

#15 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: VB6 user defined types comparison

Posted 08 August 2008 - 12:22 AM

Yeah, I get that. I just wonder where/how you found the syntax using "*" with strings. Never saw that before. :)

/Jens
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2