473,394 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 7857
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
4
by: Gaby | last post by:
Hi all, What is the best way to compare 2 (large) ArrayLists filled with an object. Can you please help me? Gaby
8
by: Turbot | last post by:
Hello, Anyone know how to compare two byte arrays without going through each item in the array. I actually want to compare two bitmap objects to see if they both contain the same picture bit...
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
4
by: Lamis | last post by:
Hi, what is the best way to compare 2 haschtables contatining objects. the objects has 2 property, name & value. I need to print out the differences -- LZ
11
by: inpuarg | last post by:
I have 2 datatables. They are identical. I want to compare them by cell's content. They are all same. But dt1 == dt2 or dt1.GetHashCode() == dt2.GetHashCode() doesn 't work. There are big...
50
by: titan nyquist | last post by:
I wish to compare two structs via == but it does not compile. I can overload and create my own == but am I missing something that c# already has implemented? ~titan
7
by: Bryan | last post by:
If I have two classes derived from the same base class, how can I check if they are the same type? class Base : CObject { public: Base() {}; ~Base() {}; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.