|
-
January 12th, 2008, 10:37 PM
#1
comparing all fields of a type
suppose I have a user type suche as
Private Type Bogus
value1 as Integer
value2 as Long
stingbean as String
End Type
'....
Dim test1 as Bogus
Dim test2 as Bogus
test1.value1=123
test1.value2=345
test1.stringbean="hello"
test2.value1=123
test2.value2=345
test2.stringbean="hello"
Now obviously test1=test2 , but how can I check this in a program??
trying: if test1=test2.... gives an error. How can you compare all of the components of test1 & 2 (without having to spell each name separately)...I may have a good dozen or so pices in my type definition. I think there is a way to do it in a loop, but don't know the syntax.
-
January 13th, 2008, 01:10 AM
#2
Re: comparing all fields of a type
How could they be the same? You could have different texts in each. I'd say there would be no way to compare without doing it manually.
-
January 13th, 2008, 01:53 AM
#3
Re: comparing all fields of a type
I want to have some code to test whether or not they are the same!!!! ---in this example they are obviously set to the same values. If the values of any of the components differ, then the comparision "fails".
...so my basic question is what is an easy way of comparing all of the components of these defined varaibles (especially in the cases where there are many portions to compare).
-
January 13th, 2008, 03:46 PM
#4
Re: comparing all fields of a type
Is the solution to use some sort of automatic index parameter---to somehow list all of the associated type componets & do a compare of each?
-
January 13th, 2008, 04:25 PM
#5
Re: comparing all fields of a type
You would need arrays, but you'd have to split all the info into data type, because you can't share in an array. I was thinking a db record, but same problem.
-
January 13th, 2008, 04:40 PM
#6
Re: comparing all fields of a type
Code:
Private Type Bogus
value1 as Integer
value2 as Long
stingbean as String
End Type
'....
Dim test1 as Bogus
Dim test2 as Bogus
Dim bSame as boolean
test1.value1=123
test1.value2=345
test1.stringbean="hello"
test2.value1=123
test2.value2=345
test2.stringbean="hello"
bSame = False
' Assumes that all you want to do is test that both are the same, if any one value is different, then the result will report false
With test1
if .value1 = test2.value1 then
bsame = true
end if
if bSame then
if .value2 <> test2.value2 then
bsame = false
endif
end if
if bSame then
if .stringbean <> test2.stringbean then
bsame = false
end if
end if
End With
If Not bSame Then
MsgBox "Types are different", vbOkOnly, "Compare Types"
Else
MsgBox "Types are the same", vbOkOnly, "Compare Types"
End If
Be nice to Harley riders...
-
January 13th, 2008, 04:48 PM
#7
Re: comparing all fields of a type
(without having to spell each name separately)
makes it a bit harder to do
-
January 13th, 2008, 04:56 PM
#8
Re: comparing all fields of a type
Or you could create a collection or property bag, then do a "For Each x In xxxx"
Be nice to Harley riders...
-
January 13th, 2008, 05:26 PM
#9
Re: comparing all fields of a type
I think having variable length strings or objects in the Type would make it impossible.
My thought is that you can read the block of memory occupied by each UDT into a string and do a simple string comparison. The problem is that a variable length string would be stored in the UDT as a pointer. The two might be the same if you dereference the string pointer, but would not be otherwise (there would be 2 copies of the string in different memory locations).
I theory you could do it with fixed length strings (and anything else that doesn't store a pointer in the UDT). But realistically, unless you were comparing large structures it wouldn't be worth it. Too bad VB6 doesn't have operator overloading.
-
January 13th, 2008, 05:57 PM
#10
Re: comparing all fields of a type
To expand on the above, take a look at the following code:
Code:
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Sub Example()
Dim uOne As Test, uTwo As Test
uOne.TestInt = 123
uOne.TestSin = 123.456
uOne.TestStr = "Hello"
uTwo.TestInt = 123
uTwo.TestSin = 123.456
uTwo.TestStr = "Hello"
Debug.Print AreEqual(uOne, uTwo)
End Sub
Private Function AreEqual(uOne As Test, uTwo As Test) As Boolean
Dim sOne As String, sTwo As String
sOne = String$(Len(uOne), Chr$(0))
sTwo = String$(Len(uTwo), Chr$(0))
Call CopyMemory(ByVal StrPtr(sOne), ByVal VarPtr(uOne), LenB(uOne))
Call CopyMemory(ByVal StrPtr(sTwo), ByVal VarPtr(uTwo), LenB(uTwo))
If sOne = sTwo Then AreEqual = True
End Function
If the type declaration is like this...
Code:
Private Type Test
TestInt As Integer
TestSin As Single
TestStr As String
End Type
...the code fails, because the last parameter in the type is a pointer to a string. With a fixed length string...
Code:
Private Type Test
TestInt As Integer
TestSin As Single
TestStr As String * 5
End Type
...it works fine. Note that this will also work with arrays in the Type, but only if they are fixed size. You could probably modify the function to take arbitrary UDTs (as long as they fit the "no pointers" criteria) by passing Variants to it and dereferencing the pointer in the Variant structure.
-
January 14th, 2008, 01:50 AM
#11
Re: comparing all fields of a type
Thanks for the creative suggestions...I didn't realize how tough it would be to simple compare two "variables" (composed of multiple components).
I was surmizing there would be some way to automatically generate all of the components (.test1, .test2, .stringbean....etc) & compare them one by one until all were automatically accounted for. I guess converting the whole thing to one gigantic string is another way to slice it!!
What I did NOT want to do was to manually have to list all of the components names--so you have succeeed!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|