Results 1 to 9 of 9

Thread: overloading true and false operators?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    overloading true and false operators?

    uuh what the heck would that mean? if a class overloads true and false, what does it really mean? how would you use those operators for that class then?

    (I'm an idiot as far as operator overloading is concerned, so please bear with me)
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: overloading true and false operators?

    Don't know really if we can overload it, provided that they have the same type (of type bool). Just go for a condition.
    VB Code:
    1. class test
    2. {
    3.     bool boolean;
    4.     public test(bool boolean)
    5.     {
    6.         if(boolean) true_statement();
    7.         else false_statement();
    8.     }
    9.     void true_statement(){}
    10.     void false_statement(){}
    11. }

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: overloading true and false operators?

    this is what I meant

    public static bool operator true (myClass val)
    {
    return true;
    }
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: overloading true and false operators?

    Oppss.

    Yes, you're right. Can't seem to find a good example why true/false operator is overloaded.

  5. #5
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: overloading true and false operators?

    Just found this. Don't really understand it coz I don't really understand English well. But hopes this might have a use for you.

  6. #6

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: overloading true and false operators?

    hmmmmmmmmmmmmmm
    thanks, but I still dont understand it
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: overloading true and false operators?

    Let's say you've created a class, and you want to perform value based comparisons between two instances (the default is reference comparisons) - you overload the ==, != and .Equals operators and methods to perform those comparisons.

  8. #8

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: overloading true and false operators?

    hmm ok but this seems to be different. This is overriding TRUE and FALSE
    public static bool operator true (myClass val)
    {
    return true;
    }
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  9. #9
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: overloading true and false operators?

    Oh, sorry - misunderstood

    From MSDN:
    The DBBool struct below implements a three-valued logical type. The possible values of this type are DBBool.True, DBBool.False, and DBBool.Null, where the Null member indicates an unknown value. Such three-valued logical types are commonly used in databases.

    Code:
    using System;
    public struct DBBool
    {
       // The three possible DBBool values.
       public static readonly DBBool Null = new DBBool(0);
       public static readonly DBBool False = new DBBool(-1);
       public static readonly DBBool True = new DBBool(1);
       // Private field that stores –1, 0, 1 for False, Null, True.
       sbyte value;
       // Private instance constructor. The value parameter must be –1, 0, or 1.
       DBBool(int value) {
          this.value = (sbyte)value;
       }
       // Properties to examine the value of a DBBool. Return true if this
       // DBBool has the given value, false otherwise.
       public bool IsNull { get { return value == 0; } }
       public bool IsFalse { get { return value < 0; } }
       public bool IsTrue { get { return value > 0; } }
       // Implicit conversion from bool to DBBool. Maps true to DBBool.True and
       // false to DBBool.False.
       public static implicit operator DBBool(bool x) {
          return x? True: False;
       }
       // Explicit conversion from DBBool to bool. Throws an exception if the
       // given DBBool is Null, otherwise returns true or false.
       public static explicit operator bool(DBBool x) {
          if (x.value == 0) throw new InvalidOperationException();
          return x.value > 0;
       }
       // Equality operator. Returns Null if either operand is Null, otherwise
       // returns True or False.
       public static DBBool operator ==(DBBool x, DBBool y) {
          if (x.value == 0 || y.value == 0) return Null;
          return x.value == y.value? True: False;
       }
       // Inequality operator. Returns Null if either operand is Null, otherwise
       // returns True or False.
       public static DBBool operator !=(DBBool x, DBBool y) {
          if (x.value == 0 || y.value == 0) return Null;
          return x.value != y.value? True: False;
       }
       // Logical negation operator. Returns True if the operand is False, Null
       // if the operand is Null, or False if the operand is True.
       public static DBBool operator !(DBBool x) {
          return new DBBool(-x.value);
       }
       // Logical AND operator. Returns False if either operand is False,
       // otherwise Null if either operand is Null, otherwise True.
       public static DBBool operator &(DBBool x, DBBool y) {
          return new DBBool(x.value < y.value? x.value: y.value);
       }
       // Logical OR operator. Returns True if either operand is True, otherwise
       // Null if either operand is Null, otherwise False.
       public static DBBool operator |(DBBool x, DBBool y) {
          return new DBBool(x.value > y.value? x.value: y.value);
       }
       // Definitely true operator. Returns true if the operand is True, false
       // otherwise.
       public static bool operator true(DBBool x) {
          return x.value > 0;
       }
       // Definitely false operator. Returns true if the operand is False, false
       // otherwise.
       public static bool operator false(DBBool x) {
          return x.value < 0;
       }
       public override bool Equals(object obj) {
          if (!(obj is DBBool)) return false;
          return value == ((DBBool)obj).value;
       }
       public override int GetHashCode() {
          return value;
       }
       public override string ToString() {
          if (value > 0) return "DBBool.True";
          if (value < 0) return "DBBool.False";
          return "DBBool.Null";
       }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width