Явное преобразование своего класса в int - C#
Формулировка задачи:
Всем привет! Есть такой класс:
Служит для расчета позиции шашки в нардах. Нужно чтобы он явно преобразовывался в int. Временно заколхозил свойство под эти нужды
Студия вещает, что оператор as работает только с ссылочными типами.. Не могу придумать как это сделать..
public class Address
{
private int _Field;
private int _position;
private Chip chip;
private int Field
{
get { return _Field; }
set
{
_Field = value;
if (this.chip == Chip.Black)
{
if (this._Field == 5) this._Field = 1;
if (this._Field == 6) this._Field = 2;
}
}
}
private int position
{
get { return _position; }
set
{
int temp = value - _position;
if (temp> 6)
{
int tt = temp / 6;
Field += tt;
int t = temp % 6;
_position += t;
}
else _position = value;
}
}
public int Position { get { return _Field * 10 + _position; } }
public static Address operator + (Address cur, int a)
{
cur.position += a;
return cur;
}
public Address(Chip ChipAdr)
{
this.chip = ChipAdr;
if (chip == Chip.White) { this._Field = 1; this._position = 1; }
else { this._Field = 3; this._position = 1; }
}
}public int Position { get { return _Field * 10 + _position; } }Решение задачи: «Явное преобразование своего класса в int»
textual
Листинг программы
public static explicit operator int(Address cur)
{
return cur._Field * 10 + cur._position;
}