在做设计类的软件中,有时候会遇到米和英寸的转化,这里自己写的这两种类型的隐式转换方法。
以米为单位的长度类型定义
///
<summary>
/// 以米为单位的长度
/// </summary>
public class MeterLength
{
public float Value { get; set; }
public UnitType Unit { get { return UnitType.Meter; } }
public MeterLength(InchLength value)
{
Value = ( float)(value.Value * 0.3048);
}
public MeterLength( float value)
{
Value = value;
}
public override string ToString()
{
return Value.ToString(CultureInfo.InvariantCulture) + " " + Unit;
}
/// <summary>
/// 米隐式转换为英寸
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static implicit operator InchLength(MeterLength value)
{
return new InchLength(value);
}
}
/// 以米为单位的长度
/// </summary>
public class MeterLength
{
public float Value { get; set; }
public UnitType Unit { get { return UnitType.Meter; } }
public MeterLength(InchLength value)
{
Value = ( float)(value.Value * 0.3048);
}
public MeterLength( float value)
{
Value = value;
}
public override string ToString()
{
return Value.ToString(CultureInfo.InvariantCulture) + " " + Unit;
}
/// <summary>
/// 米隐式转换为英寸
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static implicit operator InchLength(MeterLength value)
{
return new InchLength(value);
}
}
以英寸为单位的长度类型定义
///
<summary>
/// 以英寸为单位的长度
/// </summary>
public class InchLength
{
public float Value { get; set; }
public UnitType Unit { get { return UnitType.Meter; } }
public InchLength( float value)
{
Value = value;
}
public InchLength(MeterLength value)
{
Value = ( float)(value.Value / 0.3048);
}
public override string ToString()
{
return Value.ToString(CultureInfo.InvariantCulture) + " " + Unit;
}
/// <summary>
/// 英寸隐式转换为米
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static implicit operator MeterLength(InchLength value)
{
return new MeterLength(value);
}
}
/// 以英寸为单位的长度
/// </summary>
public class InchLength
{
public float Value { get; set; }
public UnitType Unit { get { return UnitType.Meter; } }
public InchLength( float value)
{
Value = value;
}
public InchLength(MeterLength value)
{
Value = ( float)(value.Value / 0.3048);
}
public override string ToString()
{
return Value.ToString(CultureInfo.InvariantCulture) + " " + Unit;
}
/// <summary>
/// 英寸隐式转换为米
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static implicit operator MeterLength(InchLength value)
{
return new MeterLength(value);
}
}
长度单位类型定义
public
class UnitType
{
/// <summary>
/// 长度单位类型:米
/// </summary>
public static readonly UnitType Meter = new UnitType() { Name = " Meter " };
/// <summary>
/// 长度单位类型:英寸
/// </summary>
public static readonly UnitType Inch = new UnitType() { Name = " Inch " };
private UnitType() { }
public string Name { get; private set; }
/// <summary>
/// 国际化文本显示
/// </summary>
public string Text
{
get
{
// 国际化的key
string key = this.GetType().ToString() + " . " + Name;
// 此处应根据key返回相应的国际化之后的文本,目前在没有国际化处理的情况下暂时返回Name
return Name;
}
}
public override string ToString()
{
return Text;
}
}
{
/// <summary>
/// 长度单位类型:米
/// </summary>
public static readonly UnitType Meter = new UnitType() { Name = " Meter " };
/// <summary>
/// 长度单位类型:英寸
/// </summary>
public static readonly UnitType Inch = new UnitType() { Name = " Inch " };
private UnitType() { }
public string Name { get; private set; }
/// <summary>
/// 国际化文本显示
/// </summary>
public string Text
{
get
{
// 国际化的key
string key = this.GetType().ToString() + " . " + Name;
// 此处应根据key返回相应的国际化之后的文本,目前在没有国际化处理的情况下暂时返回Name
return Name;
}
}
public override string ToString()
{
return Text;
}
}
InchLength length =
new MeterLength(
1);
MessageBox.Show(length.ToString());
MessageBox.Show(length.ToString());
本文章的目的是告诉大伙,除了float a=(float)0.111233;之外,还可以对用户定义的不同类型进行转化。