469,148 Members | 1,503 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,148 developers. It's quick & easy.

Compare to objects of the same class?

Is it possible to compare two objects of the same class directly to each
other?

In the code below I'm comparing "Search" against "oneForm.thisForm"... Both
of these objects are of the class "FormID". The IF statement never evaluates
to TRUE even if the three strings in the FormID object match. Trying to use
"=" instead of "IS" results in a error 438.

Basically I want to know if URL, Name and ID match. I could use AND and just
compare each in three evalations but that seems like more work than
necessary.
FormID:
- "URL" as string
- "Name" as string
- "ID" as string

HeldForm:
- "thisForm" as FormID
- "fields" as a collection of field data (not relevant to code below)
- "submit" as boolean

-- Code snippet --

'Find a form
'
' - Returns a HeldForm object
'
Public Function FindForm(Search As FormID) As HeldForm
Dim oneForm As HeldForm 'Holds data for one form

'Loop through all memorized forms for a match
For Each oneForm In Forms

'If our Search form matches a stored form...
If oneForm.thisForm is Search Then '<---This is never true.
'...set our return value to that form
Set FindForm = oneForm
End If
Next

End Function
Jul 17 '05 #1
7 7581
Grahammer wrote:
Basically I want to know if URL, Name and ID match. I could use AND and just
compare each in three evalations but that seems like more work than
necessary.


You'll need to compare the properties. The 'Is' keyword only tells you
if two or more object variables point to the same object...
'============
Private Sub Command1_Click()
Dim X As CommandButton
Dim Y As CommandButton

Set X = Command1
Set Y = Command1

If X Is Y Then
MsgBox "Same Object" '<--should see this
Else
MsgBox "Not the same"
End If
Set X = Command1
Set Y = Command2

If X Is Y Then
MsgBox "Same Object"
Else
MsgBox "Not the same" '<--should see this
End If

'Compare properties
If X.Caption = Y.Caption Then
MsgBox "Same" '<--should see this if they match
Else
MsgBox "Not the same"
End If
End Sub
'============

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep all discussions in the groups..
Jul 17 '05 #2

"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:u4**************@TK2MSFTNGP11.phx.gbl...
Grahammer wrote:
Basically I want to know if URL, Name and ID match. I could use AND and just compare each in three evalations but that seems like more work than
necessary.
You'll need to compare the properties. The 'Is' keyword only tells you
if two or more object variables point to the same object...

Dim X As CommandButton
Dim Y As CommandButton

Set X = Command1
Set Y = Command1

If X Is Y Then
MsgBox "Same Object" '<--should see this
Else
MsgBox "Not the same"
End If <snip> 'Compare properties
If X.Caption = Y.Caption Then
MsgBox "Same" '<--should see this if they match
Else
MsgBox "Not the same"
End If


But what if the properties are other objects?
i.e.

if form1.X = form1.Y then
msgbox "Command button X has the same values as command button Y"
end if

....not a great example, but basically what I'm looking for?
Jul 17 '05 #3

"Grahammer" <po********@127.0.0.1> wrote in message
news:_afrc.520176$Pk3.272778@pd7tw1no...
if form1.X = form1.Y then
msgbox "Command button X has the same values as command button Y"
end if

...not a great example, but basically what I'm looking for?


To compare two objects, you need to define a comparison method. In your
example, not all of the properties of button X and button Y can ever be
equal. They can't have the same tab order value, or the same name and
index value, for instance.

Going back to your original FormID class, you can define a comparison
function in it that accepts another FormID to compare to:

'in class FormID:
Public Function IsEqualTo(Other as FormID) As Boolean
If Other.URL = Me.URL _
And Other.Name = Me.Name _
And Other.ID = Me.ID Then
IsEqualTo = True
End If
End Function

Then you could write in your FindForm method:

If oneForm.thisForm.IsEqualTo(Search) Then

Jul 17 '05 #4
> > if form1.X = form1.Y then
msgbox "Command button X has the same values as command button Y"
end if

...not a great example, but basically what I'm looking for?


To compare two objects, you need to define a comparison method. In your
example, not all of the properties of button X and button Y can ever be
equal. They can't have the same tab order value, or the same name and
index value, for instance.

Going back to your original FormID class, you can define a comparison
function in it that accepts another FormID to compare to:

'in class FormID:
Public Function IsEqualTo(Other as FormID) As Boolean
If Other.URL = Me.URL _
And Other.Name = Me.Name _
And Other.ID = Me.ID Then
IsEqualTo = True
End If
End Function

Then you could write in your FindForm method:

If oneForm.thisForm.IsEqualTo(Search) Then

This makes sense. I was just wondering if this functionality was already
present in VB.

Thanks!
Jul 17 '05 #5
..NET can override the == operator, making it easier to do
this with easy to read code.
But in VB6, you can write a Compare method
e.g. (in Class1)
public function Compare(comparison as Class1) as boolean
if class1.URL = URL and class1.Name = Name and class1.ID = ID
end function
for additional readability (as we are struggling a bit from the off),
you can prefix the RHS expressions with 'Me.'
Jul 17 '05 #6
On Fri, 21 May 2004 18:55:19 +0100, "songie D" <so****@d.com> wrote:
.NET can override the == operator, making it easier to do
this with easy to read code.


Operator over-rides are Evil
- they disguise funcnionality
Jul 17 '05 #7
They CAN do, if used wrong, or simply out of
eagerness to use them for the sake of it.
If used correctly, they can be very good.
You need to have a clear impression of what
is a value type and what is a reference type
and how you treat them to get the most out of
it, I think.
At the base level though, it's really just choosing another
way of writing the line of code that will call a member
function.

"J French" <er*****@nowhere.com> wrote in message
news:40***************@news.btclick.com...
On Fri, 21 May 2004 18:55:19 +0100, "songie D" <so****@d.com> wrote:
.NET can override the == operator, making it easier to do
this with easy to read code.


Operator over-rides are Evil
- they disguise funcnionality

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by laniik | last post: by
19 posts views Thread by David zhu | last post: by
4 posts views Thread by Gaby | last post: by
8 posts views Thread by Turbot | last post: by
7 posts views Thread by Prabhudhas Peter | last post: by
4 posts views Thread by Lamis | last post: by
50 posts views Thread by titan nyquist | last post: by
7 posts views Thread by Bryan | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.