473,385 Members | 1,934 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,385 software developers and data experts.

Testing equality of pens

Hi

I'm trying to test if two Pen objects are equal as shown below

Pen p1 = new Pen(Color.Black)
Pen p2 = new Pen(Color.Black)

if (p1.Equals(p2)

Console.WriteLine("These pens equal")
The problem is that the if statement doesn't return true, I would have thought that p1 and p2 are the same. Is the only way round this to write my own function which compares every single property of Pen? That would be very laborious and time consuming

Help much appreciated

Tristan.
Jul 21 '05 #1
2 1350
Sorry I can't help. I always thought that

if (p1.Equals(p2))

would compare to see if the objects were equivilent (ie refered to 2
distinct objects that had identical properties)

and

if (Pen.Equals(p1,p2))

would check to see if both referenced the same object.
So I would have thought that your comparison would work.

However, on investigation, I found the following: at
http://msdn.microsoft.com/library/de...ualstopic1.asp

Remarks
The default implementation of Equals supports reference equality only, but
derived classes can override this method to support value equality.

So like you say, I guess you will have to write a method that compares all
the properties that are important to you in this situation. What a PITA!

ChrisM

"Tristan" <an*******@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com...
Hi,

I'm trying to test if two Pen objects are equal as shown below:

Pen p1 = new Pen(Color.Black);
Pen p2 = new Pen(Color.Black);

if (p1.Equals(p2))
{
Console.WriteLine("These pens equal");
}

The problem is that the if statement doesn't return true, I would have thought that p1 and p2 are the same. Is the only way round this to write my
own function which compares every single property of Pen? That would be very
laborious and time consuming!
Help much appreciated.

Tristan.

Jul 21 '05 #2
Hi,
both p1 and p2 are two object references. If they were int /
string / long / double / DateTime or other value types, such an equals
test would work.

From the MSDN library, Pen->Public Methods, you have:
Equals (inherited from Object).

The Object.Equals method only test if two object *references* are
equal. If you did the following:
Pen p1 = new Pen(Color.Black);
Pen p2 = p1; //sets the object reference p2 to p1
then
p1.Equals(p2) ===> TRUE

hope this helps,
Noah

Hi,

I'm trying to test if two Pen objects are equal as shown below:

Pen p1 = new Pen(Color.Black);
Pen p2 = new Pen(Color.Black);

if (p1.Equals(p2))
{
Console.WriteLine("These pens equal");
}

The problem is that the if statement doesn't return true, I would have thought that p1 and p2 are the same. Is the only way round this to write my own function which compares every single property of Pen? That would be very laborious and time consuming!

Help much appreciated.

Tristan.

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
by: Ike Naar | last post by:
In K&R "The C++ programming language (2nd ANSI C edition), the reference manual states (paragraphs 7.9 and 7.10) that pointer comparison is undefined for pointers that do not point to the same...
2
by: Stu Smith | last post by:
A feature we'd like to see in VS 2004 is a compiler warning for equality comparisons on floating values. Why? Because the behaviour allowed under the ECMA spec is somewhat suprising. (Well it...
4
by: Matt Burland | last post by:
I'm a little confused about the way the default equality operator works with classes. Here's the situation, I have two comboboxes that are each filled with different object (i.e. ComboBox1 contains...
2
by: Tristan | last post by:
Hi I'm trying to test if two Pen objects are equal as shown below Pen p1 = new Pen(Color.Black) Pen p2 = new Pen(Color.Black) if (p1.Equals(p2) Console.WriteLine("These pens equal")
2
by: Tristan | last post by:
Hi, I'm trying to test if two Pen objects are equal as shown below: Pen p1 = new Pen(Color.Black); Pen p2 = new Pen(Color.Black); if (p1.Equals(p2)) { Console.WriteLine("These pens...
2
by: John | last post by:
I created a number of pictureboxes in a panel, and want to draw lines in those pictureboxes but I cannot. Please see the following code and make corrections. Thanks. Private Sub...
6
by: onetitfemme | last post by:
Hi *, I have been looking for a definition or at least some workable concept of "XML equality". Searching on "XML equality" in comp.text.xml, microsoft.public.xsl and microsoft.public.xml...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
9
by: raylopez99 | last post by:
Just an observation: pens for drawing lines in Win Forms are tricky when assignment is inside the paint handler. inside of the Paint handler, but not inside a "using" brace (that is, outside of...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.