Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

Database integer type

Virtual, sealed, override, and abstract accessors | Indexer overloading | Conversion operators | Instance constructors | Default constructors | Static constructors | The MoveNext method | Implementation example | Value semantics | Boxing and unboxing |


Читайте также:
  1. Architecture of Deductive Database Systems
  2. Basic features of database programs
  3. Creating the standby database.
  4. Database boolean type
  5. Database management programs;
  6. Dim m, n, nr, ns, i, j As Integer

The DBInt struct below implements an integer type that can represent the complete set of values of the int type, plus an additional state that indicates an unknown value. A type with these characteristics is commonly used in databases.

using System;

public struct DBInt
{
// The Null member represents an unknown DBInt value.

public static readonly DBInt Null = new DBInt();

// When the defined field is true, this DBInt represents a known value
// which is stored in the value field. When the defined field is false,
// this DBInt represents an unknown value, and the value field is 0.

int value;
bool defined;

// Private instance constructor. Creates a DBInt with a known value.

DBInt(int value) {
this.value = value;
this.defined = true;
}

// The IsNull property is true if this DBInt represents an unknown value.

public bool IsNull { get { return!defined; } }

// The Value property is the known value of this DBInt, or 0 if this
// DBInt represents an unknown value.

public int Value { get { return value; } }

// Implicit conversion from int to DBInt.

public static implicit operator DBInt(int x) {
return new DBInt(x);
}

// Explicit conversion from DBInt to int. Throws an exception if the
// given DBInt represents an unknown value.

public static explicit operator int(DBInt x) {
if (!x.defined) throw new InvalidOperationException();
return x.value;
}

public static DBInt operator +(DBInt x) {
return x;
}

public static DBInt operator -(DBInt x) {
return x.defined? -x.value: Null;
}

public static DBInt operator +(DBInt x, DBInt y) {
return x.defined && y.defined? x.value + y.value: Null;
}

public static DBInt operator -(DBInt x, DBInt y) {
return x.defined && y.defined? x.value - y.value: Null;
}

public static DBInt operator *(DBInt x, DBInt y) {
return x.defined && y.defined? x.value * y.value: Null;
}

public static DBInt operator /(DBInt x, DBInt y) {
return x.defined && y.defined? x.value / y.value: Null;
}

public static DBInt operator %(DBInt x, DBInt y) {
return x.defined && y.defined? x.value % y.value: Null;
}

public static DBBool operator ==(DBInt x, DBInt y) {
return x.defined && y.defined? x.value == y.value: DBBool.Null;
}

public static DBBool operator!=(DBInt x, DBInt y) {
return x.defined && y.defined? x.value!= y.value: DBBool.Null;
}

public static DBBool operator >(DBInt x, DBInt y) {
return x.defined && y.defined? x.value > y.value: DBBool.Null;
}

public static DBBool operator <(DBInt x, DBInt y) {
return x.defined && y.defined? x.value < y.value: DBBool.Null;
}

public static DBBool operator >=(DBInt x, DBInt y) {
return x.defined && y.defined? x.value >= y.value: DBBool.Null;
}

public static DBBool operator <=(DBInt x, DBInt y) {
return x.defined && y.defined? x.value <= y.value: DBBool.Null;
}

public override bool Equals(object obj) {
if (!(obj is DBInt)) return false;
DBInt x = (DBInt)obj;
return value == x.value && defined == x.defined;
}

public override int GetHashCode() {
return value;
}

public override string ToString() {
return defined? value.ToString(): “DBInt.Null”;
}
}


Дата добавления: 2015-11-16; просмотров: 58 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Meaning of this| Database boolean type

mybiblioteka.su - 2015-2024 год. (0.006 сек.)