Figure 5 Address Class in C#
กก
using System;
using System.Data.SqlTypes;
using System.Data.SqlServer;
using System.Data.Sql;
 
[Serializable]
[SqlUserDefinedType (Format.UserDefined, MaxByteSize = 150)]
public class Address: INullable, IComparable, IBinarySerialize {
    private bool is_Null;
    private string m_address1;
    private string m_address2;
    private string m_address3;
    private string m_city;
    private string m_state;
    private string m_zip;

    #region INullable Members

    public bool IsNull {
        get {return (is_Null);}
    }

    public static Address Null {
        get {
            Address addr = new Address();
            addr.is_Null = true;
            return (addr);
        }
    }

    #endregion

    #region Constructor
    public Address() {
        this.is_Null = true;
        this.m_address1 = "";
        this.m_address2 = "";
        this.m_address3 = "";
        this.m_city = "";
        this.m_state = "";
        this.m_zip = "";

    }
    #endregion

    #region String Conversion Members
    public override string ToString ()
    {
        if (this.IsNull) 
            return "null";
        else
        {
            string delim = new string( (new char[] {';'}));
            return (this.m_address1 + delim + this.m_address2 
                    + delim + this.m_address3 + delim 
                    + this.m_city + delim + this.m_state 
                    + delim + this.m_zip);
        }
    }
    public static Address Parse(SqlString s)
    {
        if(s.IsNull)
            return null;
        else
        {
            Address addr = null;
            string str = Convert.ToString(s);
            string[] a = null;
            a = str.Split(new char[] {';'} );
            addr.m_address1 = a[0] == null ? "" : a[0];
            addr.m_address2 = a[1];
            addr.m_address3 = a[2];
            addr.m_city = a[3];
            addr.m_state = a[4];
            addr.m_zip = a[5];
            addr.is_Null = false;
            return (addr);
        }
    }
    #endregion
    
    #region Class Properties
    public string address1
    {
        get {return (this.m_address1);}
        set 
        {
            this.m_address1 = value;
            this.is_Null = false;
        }
    }
    public string address2
    {
        get {return (this.m_address2);}
        set 
        {
            this.m_address2 = value;
            this.is_Null = false;
        }
    }
    public string address3
    {
        get {return (this.m_address3);}
        set 
        {
            this.m_address3 = value;
            this.is_Null = false;
        }
    }
    public string city
    {
        get {return (this.m_city);}
        set 
        {
            this.m_city = value;
            this.is_Null = false;
        }
    }
    public string state
    {
        get {return (this.m_state);}
        set 
        {
            this.m_state = value;
            this.is_Null = false;
        }
    }
    public string zip
    {
        get {return (this.m_zip);}
        set 
        {
            this.m_zip = value;
            this.is_Null = false;
        }
    }

    public string cityStateZip()
    {
        return this.m_city + ", " + this.m_state + " "
                           + this.m_zip;
    }
    #endregion

    #region IComparable Members

    //Override the Equals method        
    public override bool Equals (object other)
    {
        return this.CompareTo (other) == 0;
    }

    //Override the GetHashCode method
    public override int GetHashCode ()
    {
        if (this.IsNull)
            return 0;

        return this.ToString ().GetHashCode ();
    }

    public int CompareTo (object other)
    {
        if (other == null)
            return 1; //by definition

        Address addr = other as Address;
    
        if (addr == null)
            throw new ArgumentException ("the argument to 
                compare is not a Address");

        if (this.IsNull)
        {
            if (addr.IsNull)
                return 0;

            return -1;
        }

        if (addr.IsNull)
            return 1;

        return this.ToString().CompareTo(addr.ToString());
    }
    
    #endregion

    #region IBinarySerialize Members
    public void Write (System.IO.BinaryWriter w)
    {
        byte header = (byte)(this.IsNull ? 1 : 0);

        w.Write (header);
        if (header == 1)
        {
            return;
        }

        w.Write(this.address1);
        w.Write(this.address2);
        w.Write(this.address3);
        w.Write(this.city);
        w.Write(this.state);
        w.Write(this.zip);

    }

    public void Read (System.IO.BinaryReader r)
    {
        byte header = r.ReadByte();

        if (header == 1)
        {
            this.is_Null = true;
            return;
        }

        this.is_Null = false;
        this.m_address1 = r.ReadString();
        this.m_address2 = r.ReadString();
        this.m_address3 = r.ReadString();
        this.m_city = r.ReadString();
        this.m_state = r.ReadString();
        this.m_zip = r.ReadString();
    }
    #endregion

}

//}