T
teel
Hi there,
I finally wrote a template class for a parameter, which is listed
below. I'd like to use the parameter to store int, float or enum
object, and to use it with a property, thus I don't want to worry
about it's type. Let's say I've got a class Screen defined as follows:
public class Screen {
public Parameter<float> Hue {
get { return m_Hue; }
set { m_Hue = value; }
}
...
private Parameter<float> m_Hue = new Parameter<float>(-1, -1,
-1);
private Parameter<float> m_Saturation = new Parameter<float>(-1,
-1, -1);
private Parameter<float> m_Brightness = new Parameter<float>(-1,
-1, -1);
}
Ok, so we've got a parameter which is float. But we've got another
class which defines parameters to be integer, or enum. When I try
something like this:
Screen scr = new Screen();
scr.Hue = 5;
I get an error saying that "Cannot implicitly convert type 'int' to
'Parameter<float>'"
Ok, but the same will happen if I assign a float value scr.Hue = 5f,
this time I get "Cannot implicitly convert type 'float' to
'Parameter<float>'".
The question is: is it possible to simply assign any value to be
casted to Parameter<float>' internally by property?
Could you tell me why the Parameter<T> constructor works properly
whatever is passed and the above does not?
Below there is a Parameter<T> class. Thanks in advance!
Best regards
teel
public class Parameter<T> where T : IComparable
{
public Parameter(T value, T minValue, T maxValue)
{
Value = value;
m_MinValue = minValue;
m_MaxValue = maxValue;
}
public T Value
{
get { return m_Value; }
set
{
if (m_MinValue.CompareTo(value) > 0)
m_Value = m_MinValue;
else if (m_MaxValue.CompareTo(value) < 0)
m_Value = m_MaxValue;
else
m_Value = value;
}
}
private T m_Value;
private T m_MinValue;
private T m_MaxValue;
}
I finally wrote a template class for a parameter, which is listed
below. I'd like to use the parameter to store int, float or enum
object, and to use it with a property, thus I don't want to worry
about it's type. Let's say I've got a class Screen defined as follows:
public class Screen {
public Parameter<float> Hue {
get { return m_Hue; }
set { m_Hue = value; }
}
...
private Parameter<float> m_Hue = new Parameter<float>(-1, -1,
-1);
private Parameter<float> m_Saturation = new Parameter<float>(-1,
-1, -1);
private Parameter<float> m_Brightness = new Parameter<float>(-1,
-1, -1);
}
Ok, so we've got a parameter which is float. But we've got another
class which defines parameters to be integer, or enum. When I try
something like this:
Screen scr = new Screen();
scr.Hue = 5;
I get an error saying that "Cannot implicitly convert type 'int' to
'Parameter<float>'"
Ok, but the same will happen if I assign a float value scr.Hue = 5f,
this time I get "Cannot implicitly convert type 'float' to
'Parameter<float>'".
The question is: is it possible to simply assign any value to be
casted to Parameter<float>' internally by property?
Could you tell me why the Parameter<T> constructor works properly
whatever is passed and the above does not?
Below there is a Parameter<T> class. Thanks in advance!
Best regards
teel
public class Parameter<T> where T : IComparable
{
public Parameter(T value, T minValue, T maxValue)
{
Value = value;
m_MinValue = minValue;
m_MaxValue = maxValue;
}
public T Value
{
get { return m_Value; }
set
{
if (m_MinValue.CompareTo(value) > 0)
m_Value = m_MinValue;
else if (m_MaxValue.CompareTo(value) < 0)
m_Value = m_MaxValue;
else
m_Value = value;
}
}
private T m_Value;
private T m_MinValue;
private T m_MaxValue;
}