Wolfgang Roth
Wolfgang Roth

Reputation: 483

C# : Convert an object to Boolean depending on a class variable

My class looks something like this:

public class Testclass 
{
    public int myValue;
}

In another context I want to simply check the value of myValue against 0. So I would write:

Testclass tc = new Testclass();
tc.myValue = 13;
if (tc.myValue == 0)
{ 
}

How would it be possible to simplify this so that the Testclass object knows when it is compared to a boolean? (or used as a boolean value) to write:

Testclass tc = new Testclass();
tc.myValue = 13;
if (tc)
{
}

To be more precise, the Testclass will be the result of another method that is included in a library, so the code would look like this:

anotherClass ac =new anotherClass();
// if (ac.AMethod().myValue == 0) 
// should be
if (ac.AMethod())
{

}

Where AMethod would look like this:

public Testclass AMethod()
{
    return new Testclass();
}

[Edit at 2016-04-13]:

Like Dennis wrote, i am using

public static implicit operator bool(TestClass value)

to get the "Boolean Value" of my class. To be a bit more precise and to stick more to my real application i would like to change the signature to

public static implicit operator UInt64(FlexComDotNetFehler fehler)

public static implicit operator Boolean(FlexComDotNetFehler fehler)

So these two methods of class FlexComDotNetFehler return the internal UInt64 field in the first case as the real representation as UInt64and in the second one as a Boolean value, that is true, when the UInt64 value is > 0.

But now, when i code

FlexComDotNetFehler x;
FlexComDotNetFehler y;
if (x == y)

where x and y are both of type FlexComDotNetFehler

the compiler cant know if it should use the Boolean or the UInt64 operator.

So i wrote

if ((UInt64)x != (UInt64)y)

but then those two type casts are greyed out.

@Ɖiamond ǤeezeƦ: thanks for your reformatting and editing. But I think now i got i right?

Greetings Wolfgang

BTW is there a playground where i can test the formatting and its output? And how do i send private messages to other users?

Upvotes: 3

Views: 3298

Answers (2)

Anas Saad
Anas Saad

Reputation: 59

you can use extension methods to implement method you can use any time not only for this class Testclass

  public static class IntExtension
{
    public static bool IsBool(this int number)
    {
        bool result = true;
        if (number == 0)
        {
            result = false;
        }
        return result;
    }
}

and then yo can

if ((ac.AMethod()).IsBool())
{}

Upvotes: 0

Dennis
Dennis

Reputation: 37770

Define implicit cast operator for TestClass:

class TestClass
{
    public int myValue;

    public static implicit operator bool(TestClass value)
    {
        // assuming, that 1 is true;
        // somehow this method should deal with value == null case
        return value != null && value.myValue == 1;
    }
}

Also think about converting TestClass from class to structure (see this reference). If you'll decide to convert it, avoid mutable structures.

Upvotes: 4

Related Questions