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

How to validate left argument in my operator == method


Hi!

I have these to methods:

public override bool Equals(object obj)
{
if (obj == null)
return false;

Arg a = obj as Arg;
if (t == null)
return false;

return Id == t.Id;
}
public static bool operator ==(Arg left, Arg right)
{

return left.Equals(right);
}
I want to check if argument left is null in the ==method. But if i do it
like this
if(left == null)
that causes an infinite recursive call. So any suggestions on how to solve
this problem?
May 31 '07 #1
2 1497
On May 30, 10:14 pm, "Johan" <joh...@gmail.comwrote:
Hi!

I have these to methods:

public override bool Equals(object obj)
{
if (obj == null)
return false;

Arg a = obj as Arg;
if (t == null)
return false;

return Id == t.Id;
}
public static bool operator ==(Arg left, Arg right)
{

return left.Equals(right);
}
I want to check if argument left is null in the ==method. But if i do it
like this
if(left == null)
that causes an infinite recursive call. So any suggestions on how to solve
this problem?
How about

if (Object.ReferenceEquals(left, null))

?

May 31 '07 #2
>I want to check if argument left is null in the ==method. But if i do it
>like this
if(left == null)
that causes an infinite recursive call. So any suggestions on how to solve
this problem?
Cast to Object will prevent your operator overload to be called

if ((object)left == null)

Another way is to use Object.ReferenceEquals

if (Object.ReferenceEquals(left, null))
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 31 '07 #3

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

Similar topics

4
by: Sims | last post by:
Hi, This will validate, (http://validator.w3.org/) <form method="POST" action="" name='xForm' style='background:inherit;'> <table style='background:inherit; width:100%;'> <tr width=80%> <td>...
8
by: Elementary Penguin | last post by:
Is there a left add unary operator ( or do I have to overload one ) ? For example: string s1 = "A"; s1 += "B"; Console.WriteLine( s1 )
6
by: RSB | last post by:
Hi Every one Need some help to Validate the Date i have in the Form. ALso how to Convert the Date Data i read from table to yyyy/MMM/dd format. Thanks RSB
3
by: Martin | last post by:
Hi, I am implemeting a form in asp.net. The form is quite large and the validation is reasonably complex, so I have decieded to implement my own validation rather than use any custon...
11
by: jjbutera | last post by:
I know how to use the ErrorProvider in my winforms..or do I? I validate the values and set the ErrorProvider in the validating event. If not valid, I set e.Cancel = True. I clear the ErrorProvider...
0
by: Johan | last post by:
Hi! I have these to methods: public override bool Equals(object obj) { if (obj == null) return false; Arg a = obj as Arg;
2
by: yalbizu | last post by:
#include <iostream> #include <string> #include <fstream> #include <iomanip> using namespace std; const int NO_OF_STUDENTS=20; struct studentType { string studentFName; string studentLName;
2
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Anyone know of any way to get a reference to the argument to the left (lvalue I believe it is called) of the assignment operator? I realize you can't overload the assignment operator in C#, but...
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.