You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lj_plc/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Entity/plc/Value.cs

164 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mesnac.Action.ChemicalWeighing.Entity
{
/// <summary>
/// PLC设备变量值
/// </summary>
[Serializable]
public class Value
{
#region 字段定义
private object value = null;
#endregion
#region 参数构造方法
public Value(object obj)
{
this.value = obj;
}
#endregion
#region 方法定义
/// <summary>
/// 转为Object
/// </summary>
/// <returns></returns>
public object ToObject()
{
return this.value;
}
/// <summary>
/// 转为整型
/// </summary>
/// <param name="defaultValue">默认值</param>
/// <returns></returns>
public int ToInt(int defaultValue)
{
int iResult = defaultValue;
if (this.value != null && this.value != DBNull.Value && int.TryParse(this.value.ToString(), out iResult))
{
return iResult;
}
return defaultValue;
}
/// <summary>
/// 转为整型
/// </summary>
/// <returns></returns>
public int ToInt()
{
return ToInt(0);
}
/// <summary>
/// 转为float类型
/// </summary>
/// <param name="defaultValue">默认值</param>
/// <returns></returns>
public float ToFloat(float defaultValue)
{
float fResult = defaultValue;
if (this.value != null && this.value != DBNull.Value && float.TryParse(this.value.ToString(), out fResult))
{
return fResult;
}
return defaultValue;
}
/// <summary>
/// 转为float类型
/// </summary>
/// <returns></returns>
public float ToFloat()
{
return ToFloat(0.0f);
}
/// <summary>
/// 转为double类型
/// </summary>
/// <param name="defaultValue">默认值</param>
/// <returns></returns>
public double ToDouble(double defaultValue)
{
double iResult = defaultValue;
if (this.value != null && this.value != DBNull.Value && double.TryParse(this.value.ToString(), out iResult))
{
return iResult;
}
return defaultValue;
}
/// <summary>
/// 转为double类型
/// </summary>
/// <returns></returns>
public double ToDouble()
{
return ToDouble(0.0);
}
/// <summary>
/// 转为日期类型
/// </summary>
/// <param name="defaultValue">默认值</param>
/// <returns></returns>
public DateTime ToDateTime(DateTime defaultValue)
{
DateTime iResult = defaultValue;
if (this.value != null && this.value != DBNull.Value && DateTime.TryParse(this.value.ToString(), out iResult))
{
return iResult;
}
return defaultValue;
}
/// <summary>
/// 转为日期类型
/// </summary>
/// <returns></returns>
public DateTime ToDateTime()
{
return ToDateTime(DateTime.Now);
}
/// <summary>
/// 转为字符串
/// </summary>
/// <param name="defaultValue">默认值</param>
/// <returns></returns>
public string ToString(string defaultValue)
{
string iResult = defaultValue;
if (this.value != null && this.value != DBNull.Value)
{
return this.value.ToString();
}
return defaultValue;
}
/// <summary>
/// 转为字符串
/// </summary>
/// <returns></returns>
public override string ToString()
{
return ToString(string.Empty);
}
#endregion
}
}