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

operator overloading true and false

Hi, there. I need two simple things explained to me:

1.) I want to know why the "true" overloaded operator method gets
called with a test to a null-initialized instance:

public class AnimalClass
{
public static bool operator true( AnimalClass lhs )
{
Console.WriteLine("In true operator");
return true;
}

public static bool operator false( AnimalClass lhs )
{
Console.WriteLine("In false operator");
return false;
}
}

public class MainClass
{
public static void Main( )
{
AnimalClass A = null;

if ( A )
Console.WriteLine("true");
}
}

// output: In true operator
// is true

2.) How can you call the "false" operator method? because I've tried to
in many ways and I cannot get it to be invoked - only "true" works.

Thanks,
relient.

Jan 15 '06 #1
2 1922
I think this is because the overloaded operator is a static method, so
it needs no instance to work. secondly you do not use the parameter lhs
for this operator. Compare it to calling a static member return a fixed
value like
public static int GetInt()
{
return 0;
}

as far as i know, this must solve your problem:
public static bool operator true( AnimalClass lhs)
{
if (lhs != null)
{
Console.WriteLine("In true operator : return true");
return true;
}
else
{
Console.WriteLine("In true operator: return false");
return false;
}
}

xl***************@gmail.com wrote:
Hi, there. I need two simple things explained to me:

1.) I want to know why the "true" overloaded operator method gets
called with a test to a null-initialized instance:

public class AnimalClass
{
public static bool operator true( AnimalClass lhs )
{
Console.WriteLine("In true operator");
return true;
}

public static bool operator false( AnimalClass lhs )
{
Console.WriteLine("In false operator");
return false;
}
}

public class MainClass
{
public static void Main( )
{
AnimalClass A = null;

if ( A )
Console.WriteLine("true");
}
}

// output: In true operator
// is true

2.) How can you call the "false" operator method? because I've tried to
in many ways and I cannot get it to be invoked - only "true" works.

Thanks,
relient.

Jan 15 '06 #2
<xl***************@gmail.com> wrote:
Hi, there. I need two simple things explained to me:

1.) I want to know why the "true" overloaded operator method gets
called with a test to a null-initialized instance:
<snip>

The compiler sees an AnimalClass expression which needs converting to
true/false, so it calls the true operator with the value of the
expression - which is null in this case.

<snip>
2.) How can you call the "false" operator method? because I've tried to
in many ways and I cannot get it to be invoked - only "true" works.


Hmm... looking at the spec, I can't see any examples of where it's
called. Odd.

I must say, I've *never* seen these operators used in production code -
I would suggest you steer clear of them providing an explicit method
call so that it's more obvious what's going on.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 16 '06 #3

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

Similar topics

2
by: ryan.fairchild | last post by:
I have a problem I am trying to create a MyInt class to hanlde very large ints. Its for a class, therefore I can only do what the teach tells me. I want to be able to overload the insertion...
2
by: emma middlebrook | last post by:
Hi Having difficulty getting myself clear on how a type's operator== fits in with Object.Equals. Let's just consider reference types. The default operator== tests for object identity...
5
by: relient | last post by:
Hi, there. I need two simple things explained to me: 1.) I want to know why the "true" overloaded operator method gets called with a test to a null-initialized instance: public class...
2
by: Constantine | last post by:
Hi, I have developed one class called CProductInfo providing == and != operator features. I have one problem. When I use this class in my program, say CProductInfo product = null; and...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.