• 0

[C#] Creating struct like bool


Question

I want to create a new struct in C# that can do this:

// I can assign to the struct itself, just like a bool;
NewBool myBool;
myBool = true;
myBool; // Returns true

// I can assign to array elements underneath it using a string identifier
myBool["test"] = true;
myBool["test"] = false;

Currently, i've only been able to do this:

using System;

public struct NewBool
{
 ? ?public bool Value
 ? ?{
 ? ? ? ?get { return false; }
 ? ? ? ?set { }
 ? ?}

 ? ?public bool this[string sourceID]
 ? ?{
 ? ? ? ?get { return true; }
 ? ? ? ?set { }
 ? ?}
}

This code produces a struct that I can assign to array elements underneath it, but I can't assign directly to the struct. (myBool = true;).

Do I need to override the = operator?

EDIT: Just found out I can't override the assignment (=) operator. Now I'm really confused on how to do this.

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Well, I've been looking into it a little more.

The Boolean struct inherits the ValueType class. Unfortuantly, if I try to do this the compiler tells me that I can't extend a "special" class.

I hope this is even possible :p

Link to comment
Share on other sites

  • 0

My gut is telling me what you want is not possible for several reasons, but I'd like to see it work. What do you mean when you say array elements "underneath" it? :huh:

Link to comment
Share on other sites

  • 0

I played around... I don't know if it got you to where you want. I really only skim the messages. :) I really should read more closely.

Anyhoo. I used implicit conversion to allow you to treat it like a bool.

public struct NewBool : IConvertible, IComparable
	{
  private Hashtable mItems;
  private bool mVal;

  public NewBool(bool val)
  {
 	 mItems = new Hashtable(1000);
 	 mVal = val;
  }

  public bool this[string sourceId]
  {
 	 get
 	 {
    if( mItems == null )
   	 mItems = new Hashtable(1000);

    if( mItems.Contains(sourceId) == false )
   	 throw new IndexOutOfRangeException(
      string.Format("The item, {0}, is not stored in this instance.", sourceId));
    return (bool)mItems[sourceId];
 	 }	
 	 set{ mItems[sourceId] = value; }
  }

  public static implicit operator bool(NewBool x)
  {
 	 return x.mVal;
  }

  public static implicit operator NewBool(bool x)
  {
 	 return new NewBool(x);
  }

  public TypeCode GetTypeCode()
  {
 	 return TypeCode.Boolean;
  }

  public bool ToBoolean( IFormatProvider provider )
  {
 	 return mVal;
  }

  public char ToChar( IFormatProvider provider )
  {
 	 throw new NotImplementedException();
  }

  public sbyte ToSByte( IFormatProvider provider )
  {
 	 throw new NotImplementedException();
  }

  public byte ToByte( IFormatProvider provider )
  {
 	 throw new NotImplementedException();
  }

  public short ToInt16( IFormatProvider provider )
  {
 	 throw new NotImplementedException();
  }

  public ushort ToUInt16( IFormatProvider provider )
  {
 	 throw new NotImplementedException();
  }

  public int ToInt32( IFormatProvider provider )
  {
 	 throw new NotImplementedException();
  }

  public uint ToUInt32( IFormatProvider provider )
  {
 	 throw new NotImplementedException();
  }

  public long ToInt64( IFormatProvider provider )
  {
 	 throw new NotImplementedException();
  }

  public ulong ToUInt64( IFormatProvider provider )
  {
 	 throw new NotImplementedException();
  }

  public float ToSingle( IFormatProvider provider )
  {
 	 throw new NotImplementedException();
  }

  public double ToDouble( IFormatProvider provider )
  {
 	 throw new NotImplementedException();
  }

  public decimal ToDecimal( IFormatProvider provider )
  {
 	 throw new NotImplementedException();
  }

  public DateTime ToDateTime( IFormatProvider provider )
  {
 	 throw new NotImplementedException();
  }

  public string ToString( IFormatProvider provider )
  {
 	 return mVal == true ? "true" : "false";
  }

  public object ToType( Type conversionType, IFormatProvider provider )
  {
 	 throw new NotImplementedException();
  }

  #region IComparable Members

  public int CompareTo(object obj)
  {
 	 NewBool tmp;
 	 if( obj is NewBool )
 	 {
    tmp = (NewBool)obj;
    return mVal.CompareTo(tmp.mVal);
 	 }
 	 throw new ArgumentException("Invalid argument type");
  }

  #endregion
	}

Link to comment
Share on other sites

  • 0
I played around... I don't know if it got you to where you want. I really only skim the messages. :) I really should read more closely.

Anyhoo. I used implicit conversion to allow you to treat it like a bool.

<snipped>

584819982[/snapback]

You are a freaking genius. I'll have to look up the implicit keyword :p

Thank you! It worked perfectly!

BTW: I just wrote dummy code for the getter and setter methods of the two properties I had. The real code would use a collection backend :p

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.