473,387 Members | 1,453 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,387 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.
Nov 15 '05 #1
2 1275
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.

Nov 15 '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.

Nov 15 '05 #3

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

Similar topics

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: 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")
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: 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: 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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.