SQLByte.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Data / System / Data / SQLTypes / SQLByte.cs / 1305376 / SQLByte.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
//  
// junfang 
// [....]
// [....] 
//----------------------------------------------------------------------------- 

//************************************************************************* 
// @File: SqlByte.cs
//
// Create by:    JunFang
// 
// Purpose: Implementation of SqlByte which is equivalent to
//            data type "smallint" in SQL Server 
// 
// Notes:
// 
// History:
//
//   11/1/99  JunFang    Created.
// 
// @EndHeader@
//************************************************************************* 
 
using System;
using System.Data.Common; 
using System.Runtime.InteropServices;
using System.Globalization;
using System.Xml;
using System.Xml.Schema; 
using System.Xml.Serialization;
 
namespace System.Data.SqlTypes { 

    ///  
    ///    
    ///       Represents an 8-bit unsigned integer to be stored in
    ///       or retrieved from a database.
    ///     
    /// 
    [Serializable] 
 
    [StructLayout(LayoutKind.Sequential)]
    [XmlSchemaProvider("GetXsdType")] 
    public struct SqlByte : INullable, IComparable, IXmlSerializable {

        private bool    m_fNotNull; // false if null
        private byte    m_value; 

        private static readonly int x_iBitNotByteMax    = ~0xff; 
 
        // constructor
        // construct a Null 
        private SqlByte(bool fNull) {
            m_fNotNull = false;
            m_value = 0;
        } 

        ///  
        ///    [To be supplied.] 
        /// 
        public SqlByte(byte value) { 
            m_value = value;
            m_fNotNull = true;
        }
 
        // INullable
        ///  
        ///    [To be supplied.] 
        /// 
        public bool IsNull { 
            get { return !m_fNotNull;}
        }

        // property: Value 
        /// 
        ///    [To be supplied.] 
        ///  
        public byte Value {
            get { 
                if (m_fNotNull)
                    return m_value;
                else
                    throw new SqlNullValueException(); 
            }
        } 
 
        // Implicit conversion from byte to SqlByte
        ///  
        ///    [To be supplied.]
        /// 
        public static implicit operator SqlByte(byte x) {
            return new SqlByte(x); 
        }
 
        // Explicit conversion from SqlByte to byte. Throw exception if x is Null. 
        /// 
        ///    [To be supplied.] 
        /// 
        public static explicit operator byte(SqlByte x) {
            return x.Value;
        } 

        ///  
        ///    [To be supplied.] 
        /// 
        public override String ToString() { 
            return IsNull ? SQLResource.NullString : m_value.ToString((IFormatProvider)null);
        }

        ///  
        ///    [To be supplied.]
        ///  
        public static SqlByte Parse(String s) { 
            if (s == SQLResource.NullString)
                return SqlByte.Null; 
            else
                return new SqlByte(Byte.Parse(s, (IFormatProvider)null));
        }
 
        // Unary operators
        ///  
        ///    [To be supplied.] 
        /// 
        public static SqlByte operator ~(SqlByte x) { 
            return x.IsNull ? Null : new SqlByte((byte)~x.m_value);
        }

 
        // Binary operators
 
        // Arithmetic operators 
        /// 
        ///    [To be supplied.] 
        /// 
        public static SqlByte operator +(SqlByte x, SqlByte y) {
            if (x.IsNull || y.IsNull)
                return Null; 

            int iResult = (int)x.m_value + (int)y.m_value; 
            if ((iResult & x_iBitNotByteMax) != 0) 
                throw new OverflowException(SQLResource.ArithOverflowMessage);
            else 
                return new SqlByte((byte)iResult);
        }

        ///  
        ///    [To be supplied.]
        ///  
        public static SqlByte operator -(SqlByte x, SqlByte y) { 
            if (x.IsNull || y.IsNull)
                return Null; 

            int iResult = (int)x.m_value - (int)y.m_value;
            if ((iResult & x_iBitNotByteMax) != 0)
                throw new OverflowException(SQLResource.ArithOverflowMessage); 
            else
                return new SqlByte((byte)iResult); 
        } 

        ///  
        ///    [To be supplied.]
        /// 
        public static SqlByte operator *(SqlByte x, SqlByte y) {
            if (x.IsNull || y.IsNull) 
                return Null;
 
            int iResult = (int)x.m_value * (int)y.m_value; 
            if ((iResult & x_iBitNotByteMax) != 0)
                throw new OverflowException(SQLResource.ArithOverflowMessage); 
            else
                return new SqlByte((byte)iResult);
        }
 
        /// 
        ///    [To be supplied.] 
        ///  
        public static SqlByte operator /(SqlByte x, SqlByte y) {
            if (x.IsNull || y.IsNull) 
                return Null;

            if (y.m_value != 0) {
                return new SqlByte((byte)(x.m_value / y.m_value)); 
            }
            else 
                throw new DivideByZeroException(SQLResource.DivideByZeroMessage); 
        }
 
        /// 
        ///    [To be supplied.]
        /// 
        public static SqlByte operator %(SqlByte x, SqlByte y) { 
            if (x.IsNull || y.IsNull)
                return Null; 
 
            if (y.m_value != 0) {
                return new SqlByte((byte)(x.m_value % y.m_value)); 
            }
            else
                throw new DivideByZeroException(SQLResource.DivideByZeroMessage);
        } 

        // Bitwise operators 
        ///  
        ///    [To be supplied.]
        ///  
        public static SqlByte operator &(SqlByte x, SqlByte y) {
            return(x.IsNull || y.IsNull) ? Null : new SqlByte((byte)(x.m_value & y.m_value));
        }
 
        /// 
        ///    [To be supplied.] 
        ///  
        public static SqlByte operator |(SqlByte x, SqlByte y) {
            return(x.IsNull || y.IsNull) ? Null : new SqlByte((byte)(x.m_value | y.m_value)); 
        }

        /// 
        ///    [To be supplied.] 
        /// 
        public static SqlByte operator ^(SqlByte x, SqlByte y) { 
            return(x.IsNull || y.IsNull) ? Null : new SqlByte((byte)(x.m_value ^ y.m_value)); 
        }
 


        // Implicit conversions
 
        // Implicit conversion from SqlBoolean to SqlByte
        ///  
        ///    [To be supplied.] 
        /// 
        public static explicit operator SqlByte(SqlBoolean x) { 
            return x.IsNull ? Null : new SqlByte((byte)(x.ByteValue));
        }

 
        // Explicit conversions
 
        // Explicit conversion from SqlMoney to SqlByte 
        /// 
        ///    [To be supplied.] 
        /// 
        public static explicit operator SqlByte(SqlMoney x) {
            return x.IsNull ? Null : new SqlByte(checked((byte)x.ToInt32()));
        } 

        // Explicit conversion from SqlInt16 to SqlByte 
        ///  
        ///    [To be supplied.]
        ///  
        public static explicit operator SqlByte(SqlInt16 x) {
            if (x.IsNull)
                return Null;
 
            if (x.Value > (short)Byte.MaxValue || x.Value < (short)Byte.MinValue)
                throw new OverflowException(SQLResource.ArithOverflowMessage); 
 
            return x.IsNull ? Null : new SqlByte((byte)(x.Value));
        } 

        // Explicit conversion from SqlInt32 to SqlByte
        /// 
        ///    [To be supplied.] 
        /// 
        public static explicit operator SqlByte(SqlInt32 x) { 
            if (x.IsNull) 
                return Null;
 
            if (x.Value > (int)Byte.MaxValue || x.Value < (int)Byte.MinValue)
                throw new OverflowException(SQLResource.ArithOverflowMessage);

            return x.IsNull ? Null : new SqlByte((byte)(x.Value)); 
        }
 
        // Explicit conversion from SqlInt64 to SqlByte 
        /// 
        ///    [To be supplied.] 
        /// 
        public static explicit operator SqlByte(SqlInt64 x) {
            if (x.IsNull)
                return Null; 

            if (x.Value > (long)Byte.MaxValue || x.Value < (long)Byte.MinValue) 
                throw new OverflowException(SQLResource.ArithOverflowMessage); 

            return x.IsNull ? Null : new SqlByte((byte)(x.Value)); 
        }

        // Explicit conversion from SqlSingle to SqlByte
        ///  
        ///    [To be supplied.]
        ///  
        public static explicit operator SqlByte(SqlSingle x) { 
            if (x.IsNull)
                return Null; 

            if (x.Value > (float)Byte.MaxValue || x.Value < (float)Byte.MinValue)
                throw new OverflowException(SQLResource.ArithOverflowMessage);
 
            return x.IsNull ? Null : new SqlByte((byte)(x.Value));
        } 
 
        // Explicit conversion from SqlDouble to SqlByte
        ///  
        ///    [To be supplied.]
        /// 
        public static explicit operator SqlByte(SqlDouble x) {
            if (x.IsNull) 
                return Null;
 
            if (x.Value > (double)Byte.MaxValue || x.Value < (double)Byte.MinValue) 
                throw new OverflowException(SQLResource.ArithOverflowMessage);
 
            return x.IsNull ? Null : new SqlByte((byte)(x.Value));
        }

        // Explicit conversion from SqlDecimal to SqlByte 
        /// 
        ///    [To be supplied.] 
        ///  
        public static explicit operator SqlByte(SqlDecimal x) {
            return(SqlByte)(SqlInt32)x; 
        }

        // Implicit conversion from SqlString to SqlByte
        // Throws FormatException or OverflowException if necessary. 
        /// 
        ///    [To be supplied.] 
        ///  
        public static explicit operator SqlByte(SqlString x) {
            return x.IsNull ? Null : new SqlByte(Byte.Parse(x.Value, (IFormatProvider)null)); 
        }

        // Overloading comparison operators
        ///  
        ///    [To be supplied.]
        ///  
        public static SqlBoolean operator==(SqlByte x, SqlByte y) { 
            return(x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(x.m_value == y.m_value);
        } 

        /// 
        ///    [To be supplied.]
        ///  
        public static SqlBoolean operator!=(SqlByte x, SqlByte y) {
            return ! (x == y); 
        } 

        ///  
        ///    [To be supplied.]
        /// 
        public static SqlBoolean operator<(SqlByte x, SqlByte y) {
            return(x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(x.m_value < y.m_value); 
        }
 
        ///  
        ///    [To be supplied.]
        ///  
        public static SqlBoolean operator>(SqlByte x, SqlByte y) {
            return(x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(x.m_value > y.m_value);
        }
 
        /// 
        ///    [To be supplied.] 
        ///  
        public static SqlBoolean operator<=(SqlByte x, SqlByte y) {
            return(x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(x.m_value <= y.m_value); 
        }

        /// 
        ///    [To be supplied.] 
        /// 
        public static SqlBoolean operator>=(SqlByte x, SqlByte y) { 
            return(x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(x.m_value >= y.m_value); 
        }
 
        //--------------------------------------------------
        // Alternative methods for overloaded operators
        //--------------------------------------------------
 
        // Alternative method for operator ~
        public static SqlByte OnesComplement(SqlByte x) { 
            return ~x; 
        }
 
        // Alternative method for operator +
        public static SqlByte Add(SqlByte x, SqlByte y) {
            return x + y;
        } 

        // Alternative method for operator - 
        public static SqlByte Subtract(SqlByte x, SqlByte y) { 
            return x - y;
        } 

        // Alternative method for operator *
        public static SqlByte Multiply(SqlByte x, SqlByte y) {
            return x * y; 
        }
 
        // Alternative method for operator / 
        public static SqlByte Divide(SqlByte x, SqlByte y) {
            return x / y; 
        }

        // Alternative method for operator %
        public static SqlByte Mod(SqlByte x, SqlByte y) { 
            return x % y;
        } 
 
        public static SqlByte Modulus(SqlByte x, SqlByte y) {
            return x % y; 
        }

        // Alternative method for operator &
        public static SqlByte BitwiseAnd(SqlByte x, SqlByte y) { 
            return x & y;
        } 
 
        // Alternative method for operator |
        public static SqlByte BitwiseOr(SqlByte x, SqlByte y) { 
            return x | y;
        }

        // Alternative method for operator ^ 
        public static SqlByte Xor(SqlByte x, SqlByte y) {
            return x ^ y; 
        } 

        // Alternative method for operator == 
        public static SqlBoolean Equals(SqlByte x, SqlByte y) {
            return (x == y);
        }
 
        // Alternative method for operator !=
        public static SqlBoolean NotEquals(SqlByte x, SqlByte y) { 
            return (x != y); 
        }
 
        // Alternative method for operator <
        public static SqlBoolean LessThan(SqlByte x, SqlByte y) {
            return (x < y);
        } 

        // Alternative method for operator > 
        public static SqlBoolean GreaterThan(SqlByte x, SqlByte y) { 
            return (x > y);
        } 

        // Alternative method for operator <=
        public static SqlBoolean LessThanOrEqual(SqlByte x, SqlByte y) {
            return (x <= y); 
        }
 
        // Alternative method for operator >= 
        public static SqlBoolean GreaterThanOrEqual(SqlByte x, SqlByte y) {
            return (x >= y); 
        }

        // Alternative method for conversions.
 
        public SqlBoolean ToSqlBoolean() {
            return (SqlBoolean)this; 
        } 

        public SqlDouble ToSqlDouble() { 
            return (SqlDouble)this;
        }

        public SqlInt16 ToSqlInt16() { 
            return (SqlInt16)this;
        } 
 
        public SqlInt32 ToSqlInt32() {
            return (SqlInt32)this; 
        }

        public SqlInt64 ToSqlInt64() {
            return (SqlInt64)this; 
        }
 
        public SqlMoney ToSqlMoney() { 
            return (SqlMoney)this;
        } 

        public SqlDecimal ToSqlDecimal() {
            return (SqlDecimal)this;
        } 

        public SqlSingle ToSqlSingle() { 
            return (SqlSingle)this; 
        }
 
        public SqlString ToSqlString() {
            return (SqlString)this;
        }
 

 
        // IComparable 
        // Compares this object to another object, returning an integer that
        // indicates the relationship. 
        // Returns a value less than zero if this < object, zero if this = object,
        // or a value greater than zero if this > object.
        // null is considered to be less than any instance.
        // If object is not of same type, this method throws an ArgumentException. 
        /// 
        ///    [To be supplied.] 
        ///  
        public int CompareTo(Object value) {
            if (value is SqlByte) { 
                SqlByte i = (SqlByte)value;

                return CompareTo(i);
            } 
            throw ADP.WrongType(value.GetType(), typeof(SqlByte));
        } 
 
        public int CompareTo(SqlByte value) {
            // If both Null, consider them equal. 
            // Otherwise, Null is less than anything.
            if (IsNull)
                return value.IsNull ? 0  : -1;
            else if (value.IsNull) 
                return 1;
 
            if (this < value) return -1; 
            if (this > value) return 1;
            return 0; 
        }

        // Compares this instance with a specified object
        ///  
        ///    [To be supplied.]
        ///  
        public override bool Equals(Object value) { 
            if (!(value is SqlByte)) {
                return false; 
            }

            SqlByte i = (SqlByte)value;
 
            if (i.IsNull || IsNull)
                return (i.IsNull && IsNull); 
            else 
                return (this == i).Value;
        } 

        // For hashing purpose
        /// 
        ///    [To be supplied.] 
        /// 
        public override int GetHashCode() { 
            return IsNull ? 0 : Value.GetHashCode(); 
        }
 
        /// 
        ///    [To be supplied.]
        /// 
        XmlSchema IXmlSerializable.GetSchema() { return null; } 

        ///  
        ///    [To be supplied.] 
        /// 
        void IXmlSerializable.ReadXml(XmlReader reader) { 
            string isNull = reader.GetAttribute("nil", XmlSchema.InstanceNamespace);
            if (isNull != null && XmlConvert.ToBoolean(isNull)) {
                // VSTFDevDiv# 479603 - SqlTypes read null value infinitely and never read the next value. Fix - Read the next value.
                reader.ReadElementString(); 
                m_fNotNull = false;
            } 
            else { 
                m_value = XmlConvert.ToByte(reader.ReadElementString());
                m_fNotNull = true; 
            }
        }

        ///  
        ///    [To be supplied.]
        ///  
        void IXmlSerializable.WriteXml(XmlWriter writer) { 
            if (IsNull) {
                writer.WriteAttributeString("xsi", "nil", XmlSchema.InstanceNamespace, "true"); 
            }
            else {
                writer.WriteString(XmlConvert.ToString(m_value));
            } 
        }
 
        ///  
        ///    [To be supplied.]
        ///  
        public static XmlQualifiedName GetXsdType(XmlSchemaSet schemaSet) {
            return new XmlQualifiedName("unsignedByte", XmlSchema.Namespace);
        }
 
        /// 
        ///    [To be supplied.] 
        ///  
        public static readonly SqlByte Null     = new SqlByte(true);
        ///  
        ///    [To be supplied.]
        /// 
        public static readonly SqlByte Zero     = new SqlByte(0);
        ///  
        ///    [To be supplied.]
        ///  
        public static readonly SqlByte MinValue = new SqlByte(Byte.MinValue); 
        /// 
        ///    [To be supplied.] 
        /// 
        public static readonly SqlByte MaxValue = new SqlByte(Byte.MaxValue);

    } // SqlByte 

} // namespace System 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK