1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| public class MyString
{
private String _value = String.Empty;
private Int32 _maxLenght;
#region Constructors
public MyString(Int32 maxLenght)
{
this._maxLenght = maxLenght;
}
public MyString(String value)
: this(value.Length)
{
Set(value);
}
public MyString(String value, Int32 maxLenght)
: this(maxLenght)
{
Set(value);
}
#endregion
public void Set(String value)
{
if (value.Length > this._maxLenght)
this._value = value.Substring(0, this._maxLenght - 1);
else
this._value = value;
}
public Int32 MaxLenght
{
get { return this._maxLenght; }
set
{
if (this._value.Length > value)
this._value = this._value.Substring(0, value - 1);
this._maxLenght = value;
}
}
public char this[int index] { get { return this._value[index]; } }
public override string ToString()
{
return this._value;
}
public static implicit operator MyString(String value)
{
return new MyString(value);
}
public static explicit operator String(MyString value)
{
return value.ToString();
}
} |
Partager