T
teel
Hi there,
I'm trying to apply "less than" and "more than" operators on the
Generics (class template-like) type objects. Below is the code of my
class representing a parameter that can be any type, but only if it's
float or int the comparison results in true/false-like result, else
its INVALID. I get error:
Operator '<' cannot be applied to operands of type 'T' and 'T' at
line:
set { m_Value = value < m_MinValue ? m_MinValue : (value >
m_MaxValue ? m_MaxValue : value); }
Could anyone take a look and help me with that? I'd be grateful.
Best regards
teel
public class Parameter<T>
{
public Parameter(T value, T minValue, T maxValue)
{
m_Value = value;
m_MinValue = minValue;
m_MaxValue = maxValue;
}
public T Value
{
get { return m_Value; }
set { m_Value = value < m_MinValue ? m_MinValue : (value >
m_MaxValue ? m_MaxValue : value); }
}
public static ParameterComparisonResult operator
<(Parameter<T> lhs, Parameter<T> rhs)
{
if (lhs is float && rhs is float)
{
if ((float)(object)lhs < (float)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else if (lhs is int && rhs is int)
{
if ((int)(object)lhs < (int)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else
return ParameterComparisonResult.INVALID;
}
public static ParameterComparisonResult operator
if (lhs is float && rhs is float)
{
if ((float)(object)lhs > (float)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else if (lhs is int && rhs is int)
{
if ((int)(object)lhs > (int)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else
return ParameterComparisonResult.INVALID;
}
public enum ParameterComparisonResult
{
INVALID,
TRUE,
FALSE
}
private T m_Value;
private T m_MinValue;
private T m_MaxValue;
}
I'm trying to apply "less than" and "more than" operators on the
Generics (class template-like) type objects. Below is the code of my
class representing a parameter that can be any type, but only if it's
float or int the comparison results in true/false-like result, else
its INVALID. I get error:
Operator '<' cannot be applied to operands of type 'T' and 'T' at
line:
set { m_Value = value < m_MinValue ? m_MinValue : (value >
m_MaxValue ? m_MaxValue : value); }
Could anyone take a look and help me with that? I'd be grateful.
Best regards
teel
public class Parameter<T>
{
public Parameter(T value, T minValue, T maxValue)
{
m_Value = value;
m_MinValue = minValue;
m_MaxValue = maxValue;
}
public T Value
{
get { return m_Value; }
set { m_Value = value < m_MinValue ? m_MinValue : (value >
m_MaxValue ? m_MaxValue : value); }
}
public static ParameterComparisonResult operator
<(Parameter<T> lhs, Parameter<T> rhs)
{
if (lhs is float && rhs is float)
{
if ((float)(object)lhs < (float)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else if (lhs is int && rhs is int)
{
if ((int)(object)lhs < (int)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else
return ParameterComparisonResult.INVALID;
}
public static ParameterComparisonResult operator
{(Parameter<T> lhs, Parameter<T> rhs)
if (lhs is float && rhs is float)
{
if ((float)(object)lhs > (float)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else if (lhs is int && rhs is int)
{
if ((int)(object)lhs > (int)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else
return ParameterComparisonResult.INVALID;
}
public enum ParameterComparisonResult
{
INVALID,
TRUE,
FALSE
}
private T m_Value;
private T m_MinValue;
private T m_MaxValue;
}