locked
Extensions... (Yes I'm here again)

    Question

  • Hey experts!

    Any way to use extensions to add implicit operators?

    I want to get rid of my "Cannot convert X to bool" when I use code like:
    if(someString) {}...

    So I was thinking about:

    public static implicit operator bool(this string someString)
    {
        return String.IsNullOrEmpty(someString);

    public static implicit operator bool(this int someInt)
    {
        return (someInt != 0);
    }

    But its not working...

    Can anyone enlighten me?

    Thanks in advance!
    "The improbable we do, the impossible just takes a little longer." (Steven Parker) "public class Answer : ANewCupOfCoffee()" (JannemanRobinson) "My computer goes down on me more often than my girlfriend." (Robert Paul) "Computers will never take the place of books. You can't stand on a floppy disk to reach a high shelf." (Sam Ewing) "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." ( Rich Cook.)
    Friday, September 19, 2008 12:48 PM

Answers

  • Nope.

    You could have a .ToBool() extension method (or similar), but that is it.

    Marc
    Friday, September 19, 2008 1:15 PM
  • I am still trying to figure out how to solve the problem statement you posted above. This is a challenging one janneman (The man with evil laugh. ha! ha! ha).

    Well, when i dipped my hands into my chest of hacks, i could not find any matching solution, but we can still move one with ourlives, assuming we can inherit value types like bool, this would have been possible. But all the same, we need to move on.

    Wait a minute! who says we cannot wrap, yes of cos we can wrap a bool and get the object or struct representation of a bool back, janneman, this is not the best of the best solutions, but a standard which c# users can enjoy if there is a object representation of our value types. Here is what i came accross, (as dirty as it is, here is it) :

    1 public struct BooleanWrapper  
    2 {  
    3     private bool b;  
    4     public BooleanWrapper(bool b)  
    5     {  
    6         this.b = b;  
    7     }  
    8  
    9     public static implicit operator BooleanWrapper(string str)  
    10     {  
    11         return String.IsNullOrEmpty(someString);  
    12     }  
    13  
    14     public static implicit operator bool(BooleanWrapper ex)  
    15     {  
    16         return ex.b;  
    17     }  
    18  
    19     public static implicit operator BooleanWrapper(bool b)  
    20     {  
    21         return new BooleanWrapper(b);  
    22     }  
    23 }  
    24  
    25 Then we can do the dirty instincts here :  
    26  
    27         bool me = false;  
    28         BooleanWrapper ex = new BooleanWrapper(me);  
    29         ex = "ppp";  
    30         me = ex;  

    or

    me = ex = "ppp";
    31  

    Thats it. Cheers.
    Agility. http://salakoahmed.blogspot.com/
    Friday, September 19, 2008 8:59 PM

All replies

  • Nope.

    You could have a .ToBool() extension method (or similar), but that is it.

    Marc
    Friday, September 19, 2008 1:15 PM
  • I am still trying to figure out how to solve the problem statement you posted above. This is a challenging one janneman (The man with evil laugh. ha! ha! ha).

    Well, when i dipped my hands into my chest of hacks, i could not find any matching solution, but we can still move one with ourlives, assuming we can inherit value types like bool, this would have been possible. But all the same, we need to move on.

    Wait a minute! who says we cannot wrap, yes of cos we can wrap a bool and get the object or struct representation of a bool back, janneman, this is not the best of the best solutions, but a standard which c# users can enjoy if there is a object representation of our value types. Here is what i came accross, (as dirty as it is, here is it) :

    1 public struct BooleanWrapper  
    2 {  
    3     private bool b;  
    4     public BooleanWrapper(bool b)  
    5     {  
    6         this.b = b;  
    7     }  
    8  
    9     public static implicit operator BooleanWrapper(string str)  
    10     {  
    11         return String.IsNullOrEmpty(someString);  
    12     }  
    13  
    14     public static implicit operator bool(BooleanWrapper ex)  
    15     {  
    16         return ex.b;  
    17     }  
    18  
    19     public static implicit operator BooleanWrapper(bool b)  
    20     {  
    21         return new BooleanWrapper(b);  
    22     }  
    23 }  
    24  
    25 Then we can do the dirty instincts here :  
    26  
    27         bool me = false;  
    28         BooleanWrapper ex = new BooleanWrapper(me);  
    29         ex = "ppp";  
    30         me = ex;  

    or

    me = ex = "ppp";
    31  

    Thats it. Cheers.
    Agility. http://salakoahmed.blogspot.com/
    Friday, September 19, 2008 8:59 PM
  • Thanks Ahmed!

    I came up with a similar solution, and as long as you keep it simple, it indeed works, be it in a bit dirty way for string to bool comparing.
    However, once you start expanding the from- and to- list of possibilities, it gets harder...

    Here's the assumptions we made...
     String => False when NULL or empty ("") 
     Int => False when 0.

    Here's where we go wrong:

     string MyString = "0";  => true (not null and not empty)
     int MyInt = 0; => false (the value is 0)

     int MyInt = MyString => True? False? 

    Even if we could get it to compile, woult the value still be meaningfull?

    In the language used in the company I work for, this is possible.  IF (statement), where statement could be byte, string, int, long, whatever.  But C# is a stronly-typed language, and in the end were asking for trouble here.

    The solution: work hard, study hard, get a job in MS's programmers team, and create a whole bunch of overloads for the c# if-statement in Framework 8.5 or something.

    None the less, thanks for getting a headache for me ;-).


    "The improbable we do, the impossible just takes a little longer." (Steven Parker) "public class Answer : ANewCupOfCoffee()" (JannemanRobinson) "My computer goes down on me more often than my girlfriend." (Robert Paul) "Computers will never take the place of books. You can't stand on a floppy disk to reach a high shelf." (Sam Ewing) "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." ( Rich Cook.)
    Monday, September 22, 2008 12:42 PM