How to use of a template class object?

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;
}
 
J

John B

teel said:
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>'"

I believe you want to define an implicit conversion from T (float) to
Parameter<T> (Parameter<float>)

public static implicit operator T(Parameter<T> value)
{
return value == null ? default(T) : value.Value;
}

public static implicit operator Parameter<T>(T value)
{
return new Parameter(value, DEFAULTMINVALUE, DEFAULTMAXVALUE);
}

This will enable implicit conversions between T and Parameter<T>

The actual conversion you use will depend on the behaviour you desire.

<...>

HTH

JB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top