473,322 Members | 1,431 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,322 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 1493
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:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.