1 // System.Data.SqlTypes.SqlByte
\r
4 // Konstantin Triger <kostat@mainsoft.com>
\r
5 // Boris Kirzner <borisk@mainsoft.com>
\r
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
\r
11 // Permission is hereby granted, free of charge, to any person obtaining
\r
12 // a copy of this software and associated documentation files (the
\r
13 // "Software"), to deal in the Software without restriction, including
\r
14 // without limitation the rights to use, copy, modify, merge, publish,
\r
15 // distribute, sublicense, and/or sell copies of the Software, and to
\r
16 // permit persons to whom the Software is furnished to do so, subject to
\r
17 // the following conditions:
\r
19 // The above copyright notice and this permission notice shall be
\r
20 // included in all copies or substantial portions of the Software.
\r
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
31 namespace System.Data.SqlTypes
\r
40 public struct SqlByte : INullable, IComparable
\r
42 private byte _value; // = -1;
\r
43 private bool _isNull;
\r
45 public static readonly SqlByte MaxValue = new SqlByte((byte)0xFF);
\r
46 public static readonly SqlByte MinValue = new SqlByte((byte)0);
\r
47 public static readonly SqlByte Zero = new SqlByte((byte)0);
\r
48 public static readonly SqlByte Null = new SqlByte(true);
\r
51 private SqlByte(bool isNull)
\r
57 * Initializes a new instance of the SqlByte instance using the specified byte value.
\r
58 * @param value A byte value to be stored in the Value property of the new SqlByte instance.
\r
60 public SqlByte(byte value)
\r
67 public override int GetHashCode()
\r
76 * Indicates whether or not Value is null.
\r
77 * @return true if Value is null, otherwise false.
\r
88 * Gets the value of the SqlByte instance.
\r
89 * @return the value of this instance
\r
97 throw new SqlNullValueException();
\r
105 * Computes the sum of the two specified SqlByte instances.
\r
106 * @param x A SqlByte instance.
\r
107 * @param y A SqlByte instance.
\r
108 * @return A SqlByte instance whose Value property contains the results of the addition.
\r
110 public static SqlByte Add(SqlByte x, SqlByte y)
\r
112 if (x.IsNull || y.IsNull)
\r
113 return SqlByte.Null;
\r
115 int res = x._value + y._value;
\r
118 throw new OverflowException("Arithmetic Overflow");
\r
120 return new SqlByte((byte)res);
\r
124 * Computes the bitwise AND of its SqlByte operands.
\r
125 * @param x A SqlByte instance.
\r
126 * @param y A SqlByte instance.
\r
127 * @return The results of the bitwise AND operation.
\r
129 public static SqlByte BitwiseAnd(SqlByte x, SqlByte y)
\r
131 if (x.IsNull || y.IsNull)
\r
132 return SqlByte.Null;
\r
134 int res = x._value & y._value;
\r
135 return new SqlByte((byte)res);
\r
139 * Computes the bitwise OR of its SqlByte operands.
\r
140 * @param x A SqlByte instance.
\r
141 * @param y A SqlByte instance.
\r
142 * @return The results of the bitwise OR operation.
\r
144 public static SqlByte BitwiseOr(SqlByte x, SqlByte y)
\r
146 if (x.IsNull || y.IsNull)
\r
147 return SqlByte.Null;
\r
149 int res = x._value | y._value;
\r
150 return new SqlByte((byte)res);
\r
154 * Compares this instance to the supplied object and returns an indication of their relative values.
\r
155 * @param obj The object to compare.
\r
156 * @return A signed number indicating the relative values of the instance and the object.
\r
157 * Less than zero This instance is less than object.
\r
158 * Zero This instance is the same as object.
\r
159 * Greater than zero This instance is greater than object -or-
\r
160 * object is a null reference.
\r
162 public int CompareTo(Object obj)
\r
167 if ((obj is SqlByte) == false)
\r
169 throw new ArgumentException("Wrong value type " +
\r
170 obj.GetType().Name + "in SqlByte.CompareTo");
\r
173 SqlByte val = (SqlByte)obj;
\r
181 else if (val.IsNull)
\r
184 if (_value > val._value)
\r
187 if (_value < val._value)
\r
195 * The division operator divides the first SqlByte operand by the second.
\r
196 * @param x A SqlByte instance.
\r
197 * @param y A SqlByte instance.
\r
198 * @return A SqlByte instance containing the results of the division operation.
\r
199 * If one of the parameters is null or null value - return SqlDouble.Null.
\r
201 public static SqlByte Divide(SqlByte x, SqlByte y)
\r
203 if (x.IsNull || y.IsNull)
\r
204 return SqlByte.Null;
\r
207 throw new DivideByZeroException("Divide by zero error encountered.");
\r
209 int val = x._value / y._value;
\r
210 return new SqlByte((byte)val);
\r
213 public override bool Equals(Object obj)
\r
218 if (obj is SqlByte)
\r
220 if (((SqlByte)obj)._value == this._value)
\r
228 * Performs a logical comparison on two instances of SqlByte to determine if they are equal.
\r
229 * @param x A SqlByte instance.
\r
230 * @param y A SqlByte instance.
\r
231 * @return true if the two values are equal, otherwise false.
\r
232 * If one of the parameters is null or null value return SqlBoolean.Null.
\r
234 public static SqlBoolean Equals(SqlByte x, SqlByte y)
\r
236 if (x.IsNull || y.IsNull)
\r
237 return SqlBoolean.Null;
\r
239 if (x._value == y._value)
\r
240 return SqlBoolean.True;
\r
242 return SqlBoolean.False;
\r
246 * Compares two instances of SqlByte to determine if the first is greater than the second.
\r
247 * @param x A SqlByte instance
\r
248 * @param y A SqlByte instance
\r
249 * @return A SqlBoolean that is True if the first instance is greater than the second instance, otherwise False.
\r
250 * If either instance of SqlByte is null, the Value of the SqlBoolean will be Null.
\r
252 public static SqlBoolean GreaterThan(SqlByte x, SqlByte y)
\r
254 if (x.IsNull || y.IsNull)
\r
255 return SqlBoolean.Null;
\r
257 if (x._value > y._value)
\r
258 return SqlBoolean.True;
\r
260 return SqlBoolean.False;
\r
264 * Compares two instances of SqlByte to determine if the first is greater than or equal to the second.
\r
265 * @param x A SqlByte instance
\r
266 * @param y A SqlByte instance
\r
267 * @return A SqlBoolean that is True if the first instance is greaater than or equal to the second instance, otherwise False.
\r
268 * If either instance of SqlByte is null, the Value of the SqlBoolean will be Null.
\r
270 public static SqlBoolean GreaterThanOrEqual(SqlByte x, SqlByte y)
\r
272 if (x.IsNull || y.IsNull)
\r
273 return SqlBoolean.Null;
\r
275 if (x._value >= y._value)
\r
276 return SqlBoolean.True;
\r
278 return SqlBoolean.False;
\r
282 * Compares two instances of SqlByte to determine if the first is less than the second.
\r
283 * @param x A SqlByte instance
\r
284 * @param y A SqlByte instance
\r
285 * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.
\r
286 * If either instance of SqlByte is null, the Value of the SqlBoolean will be Null.
\r
288 public static SqlBoolean LessThan(SqlByte x, SqlByte y)
\r
290 if (x.IsNull || y.IsNull)
\r
291 return SqlBoolean.Null;
\r
293 if (x._value < y._value)
\r
294 return SqlBoolean.True;
\r
296 return SqlBoolean.False;
\r
300 * Compares two instances of SqlByte to determine if the first is less than or equal to the second.
\r
301 * @param x A SqlByte instance
\r
302 * @param y A SqlByte instance
\r
303 * @return A SqlBoolean that is True if the first instance is less than or equal to the second instance, otherwise False.
\r
304 * If either instance of SqlByte is null, the Value of the SqlBoolean will be Null.
\r
306 public static SqlBoolean LessThanOrEqual(SqlByte x, SqlByte y)
\r
308 if (x.IsNull || y.IsNull)
\r
309 return SqlBoolean.Null;
\r
311 if (x._value <= y._value)
\r
312 return SqlBoolean.True;
\r
314 return SqlBoolean.False;
\r
318 * Computes the remainder after dividing its first SqlByte operand by its second.
\r
319 * @param x A SqlByte instance
\r
320 * @param y A SqlByte instance
\r
321 * @return A SqlByte instance whose Value contains the remainder.
\r
323 public static SqlByte Mod(SqlByte x, SqlByte y)
\r
325 if (x.IsNull || y.IsNull)
\r
326 return SqlByte.Null;
\r
329 throw new DivideByZeroException("Divide by zero error encountered.");
\r
331 int mod = x._value % y._value;
\r
332 return new SqlByte((byte)mod);
\r
336 * The multiplication operator computes the product of the two SqlByte operands.
\r
337 * @param x A SqlByte instance
\r
338 * @param y A SqlByte instance
\r
339 * @return The product of the two SqlByte operands.
\r
341 public static SqlByte Multiply(SqlByte x, SqlByte y)
\r
343 if (x.IsNull || y.IsNull)
\r
344 return SqlByte.Null;
\r
346 int res = x._value * y._value;
\r
349 throw new OverflowException("Arithmetic Overflow.");
\r
351 return new SqlByte((byte)res);
\r
355 * Compares two instances of SqlByte to determine if they are equal.
\r
356 * @param x A SqlByte instance
\r
357 * @param y A SqlByte instance
\r
358 * @return A SqlBoolean that is True if the two instances are not equal or False if the two instances are equal.
\r
359 * If either instance of SqlByte is null, the Value of the SqlBoolean will be Null.
\r
361 public static SqlBoolean NotEquals(SqlByte x, SqlByte y)
\r
363 if (x.IsNull || y.IsNull)
\r
364 return SqlBoolean.Null;
\r
366 if (x._value != y._value)
\r
367 return SqlBoolean.True;
\r
369 return SqlBoolean.False;
\r
373 * The ones complement operator performs a bitwise one's complement operation on its SqlByte operand.
\r
374 * @param x A SqlByte instance
\r
375 * @return A SqlByte instance whose Value property contains the ones complement of the SqlByte parameter.
\r
377 public static SqlByte OnesComplement(SqlByte x)
\r
380 return SqlByte.Null;
\r
382 int res = x._value ^ 0xFF; /*@TODO Ma ze ??? */
\r
384 return new SqlByte((byte)res);
\r
388 * Converts the String representation of a number to its byte equivalent.
\r
389 * @param s The String to be parsed.
\r
390 * @return A SqlByte containing the value represented by the String.
\r
392 public static SqlByte Parse(String s)
\r
394 byte val = byte.Parse(s);
\r
395 return new SqlByte(val);
\r
399 * The subtraction operator the second SqlByte operand from the first.
\r
400 * @param x A SqlByte instance
\r
401 * @param y A SqlByte instance
\r
402 * @return The results of the subtraction operation.
\r
404 public static SqlByte Subtract(SqlByte x, SqlByte y)
\r
406 if (x.IsNull || y.IsNull)
\r
407 return SqlByte.Null;
\r
409 int res = x._value - y._value;
\r
412 throw new OverflowException("Arithmetic Overflow.");
\r
414 return new SqlByte((byte)res);
\r
419 * Converts this SqlByte instance to SqlBoolean.
\r
420 * @return A SqlBoolean instance whose Value will be True if the SqlByte instance's Value is non-zero,
\r
421 * False if the SqlByte is zero
\r
422 * and Null if the SqlByte instance is Null.
\r
424 public SqlBoolean ToSqlBoolean()
\r
427 return SqlBoolean.Null;
\r
429 return new SqlBoolean(Value);
\r
433 * Converts this SqlByte instance to SqlDecimal.
\r
434 * @return A SqlDecimal instance whose Value equals the Value of this SqlByte instance.
\r
436 public SqlDecimal ToSqlDecimal()
\r
439 return SqlDecimal.Null;
\r
441 return new SqlDecimal(Value);
\r
445 * Converts this SqlByte instance to SqlDouble.
\r
446 * @return A SqlDouble instance whose Value equals the Value of this SqlByte instance.
\r
448 public SqlDouble ToSqlDouble()
\r
451 return SqlDouble.Null;
\r
453 return new SqlDouble(Value);
\r
457 * Converts this SqlByte instance to SqlSingle.
\r
458 * @return A SqlSingle instance whose Value equals the Value of this SqlByte instance.
\r
460 public SqlSingle ToSqlSingle()
\r
463 return SqlSingle.Null;
\r
465 return new SqlSingle(Value);
\r
469 * Converts this SqlByte structure to SqlInt32.
\r
470 * @return A SqlInt32 structure whose Value equals the Value of this SqlByte structure.
\r
472 public SqlInt32 ToSqlInt32()
\r
475 return SqlInt32.Null;
\r
477 return new SqlInt32(Value);
\r
481 * Converts this SqlByte structure to SqlInt32.
\r
482 * @return A SqlInt32 structure whose Value equals the Value of this SqlByte structure.
\r
484 public SqlInt64 ToSqlInt64()
\r
487 return SqlInt64.Null;
\r
489 return new SqlInt64(Value);
\r
493 * Converts this SqlByte structure to SqlInt16.
\r
494 * @return A SqlInt16 structure whose Value equals the Value of this SqlByte structure.
\r
496 public SqlInt16 ToSqlInt16()
\r
499 return SqlInt16.Null;
\r
501 return new SqlInt16(Value);
\r
505 * Converts this SqlByte instance to SqlDecimal.
\r
506 * @return A SqlDecimal instance whose Value equals the Value of this SqlByte instance.
\r
508 public SqlMoney ToSqlMoney()
\r
511 return SqlMoney.Null;
\r
513 return new SqlMoney(Value);
\r
517 * Converts this SqlByte structure to SqlString.
\r
518 * @return A SqlString structure whose value is a string representing the date and time contained in this SqlByte structure.
\r
520 public SqlString ToSqlString()
\r
523 return SqlString.Null;
\r
525 return new SqlString(_value.ToString());
\r
529 * Performs a bitwise exclusive-OR operation on the supplied parameters.
\r
530 * @param x A SqlByte instance
\r
531 * @param y A SqlByte instance
\r
532 * @return The results of the XOR operation.
\r
534 public static SqlByte Xor(SqlByte x, SqlByte y)
\r
536 if (x.IsNull || y.IsNull)
\r
537 return SqlByte.Null;
\r
539 int res = x._value ^ y._value;
\r
541 return new SqlByte((byte)res);
\r
544 public override String ToString()
\r
549 return _value.ToString();
\r
552 public static SqlByte op_Implicit(byte x)
\r
554 return new SqlByte(x);
\r
557 public static int op_Explicit(SqlByte x)
\r
562 public static SqlByte op_OnesComplement(SqlByte x)
\r
564 return OnesComplement(x);
\r
567 public static SqlByte op_Addition(SqlByte x, SqlByte y)
\r
572 public static SqlByte op_Subtraction(SqlByte x, SqlByte y)
\r
574 return Subtract(x, y);
\r
577 public static SqlByte op_Multiply(SqlByte x, SqlByte y)
\r
579 return Multiply(x, y);
\r
582 public static SqlByte op_Division(SqlByte x, SqlByte y)
\r
584 return Divide(x, y);
\r
587 public static SqlByte op_Modulus(SqlByte x, SqlByte y)
\r
592 public static SqlByte op_BitwiseAnd(SqlByte x, SqlByte y)
\r
594 return BitwiseAnd(x, y);
\r
597 public static SqlByte op_BitwiseOr(SqlByte x, SqlByte y)
\r
599 return BitwiseOr(x, y);
\r
602 public static SqlByte op_ExclusiveOr(SqlByte x, SqlByte y)
\r
607 public static SqlByte op_Explicit(SqlBoolean x)
\r
610 return SqlByte.Null;
\r
612 return new SqlByte((byte)x.ByteValue);
\r
615 public static SqlByte op_Explicit(SqlMoney x)
\r
618 return SqlByte.Null;
\r
620 int val = x.ToSqlInt32().Value;
\r
621 if (val < 0 || val > 255)
\r
622 throw new OverflowException("Arithmetic Overflow.");
\r
624 return new SqlByte((byte)val);
\r
627 public static SqlByte op_Explicit(SqlInt16 x)
\r
630 return SqlByte.Null;
\r
632 short val = x.Value;
\r
633 if (val < 0 || val > 255)
\r
634 throw new OverflowException("Arithmetic Overflow.");
\r
636 return new SqlByte((byte)val);
\r
639 public static SqlByte op_Explicit(SqlInt32 x)
\r
642 return SqlByte.Null;
\r
645 if (val < 0 || val > 255)
\r
646 throw new OverflowException("Arithmetic Overflow.");
\r
648 return new SqlByte((byte)val);
\r
651 public static SqlByte op_Explicit(SqlInt64 x)
\r
654 return SqlByte.Null;
\r
656 long val = x.Value;
\r
657 if (val < 0 || val > 255)
\r
658 throw new OverflowException("Arithmetic Overflow.");
\r
660 return new SqlByte((byte)val);
\r
663 public static SqlByte op_Explicit(SqlSingle x)
\r
666 return SqlByte.Null;
\r
668 float val = x.Value;
\r
669 if (val < 0 || val > 255)
\r
670 throw new OverflowException("Arithmetic Overflow.");
\r
672 return new SqlByte((byte)val);
\r
675 public static SqlByte op_Explicit(SqlDouble x)
\r
678 return SqlByte.Null;
\r
680 double val = x.Value;
\r
681 if (val < 0 || val > 255)
\r
682 throw new OverflowException("Arithmetic Overflow.");
\r
684 return new SqlByte((byte)val);
\r
687 public static SqlByte op_Explicit(SqlDecimal x)
\r
690 return SqlByte.Null;
\r
692 double val = x.ToDouble();
\r
693 if (val < 0 || val > 255)
\r
694 throw new OverflowException("Arithmetic Overflow.");
\r
696 return new SqlByte((byte)val);
\r
699 public static SqlByte op_Explicit(SqlString x)
\r
702 return SqlByte.Null;
\r
704 return Parse(x.Value);
\r
707 public static SqlBoolean op_Equality(SqlByte x, SqlByte y)
\r
709 return Equals(x, y);
\r
712 public static SqlBoolean op_Inequality(SqlByte x, SqlByte y)
\r
714 return NotEquals(x, y);
\r
717 public static SqlBoolean op_LessThan(SqlByte x, SqlByte y)
\r
719 return LessThan(x, y);
\r
722 public static SqlBoolean op_GreaterThan(SqlByte x, SqlByte y)
\r
724 return GreaterThan(x, y);
\r
727 public static SqlBoolean op_LessThanOrEqual(SqlByte x, SqlByte y)
\r
729 return LessThanOrEqual(x, y);
\r
732 public static SqlBoolean op_GreaterThanOrEqual(SqlByte x, SqlByte y)
\r
734 return GreaterThanOrEqual(x, y);
\r
737 public static void main(String[] args)
\r
739 SqlByte b = new SqlByte((byte)1);
\r
740 Console.WriteLine(b);
\r
742 Console.WriteLine(OnesComplement(b));
\r