Re: explicit cast between generic types
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 10 Dec 2007 12:35:22 -0500
Jon,
Why do you have two types to begin with? How is it that the sum of
types Ti equals a ^NEW^ type Ts? Why wouldn't you just need one type, Ti?
As for the Clear method, you can fix this by using default:
public void Clear()
{
// This should probably be Ti, the same could be applied.
Ts_sum = default(Ti);
} // Clear
If Ti is a blittable type, then an instance of that type will be created
with the data set to 0 for all bits in the type. In the case of int,
double, etc, etc, that means values of 0.
Changing to Ti will fix the Dequeue issue as well.
As for adding, you aren't going to be able to do much with that, since
the constraint system doesn't tell the compiler anything about operations on
a type. Because of this, you will need another class that handles this.
First, you have to define an interface like this:
interface ISimpleMath<T>
{
T Add(T value1, T value2);
T Subtract(T value1, T value2);
}
Then, you implement it for each of the types you want to use, like so:
class IntSimpleMath: ISimpleMath<int>
{
public int Add(int value1, int value2)
{
// Return the sum.
return value1 + value2;
}
public int Subtract(int value1, int value2)
{
// Return the difference.
return value1 - value2;
}
}
Finally, you would add a second type to your generic type which would
indicate the type used to perform the adding:
public class C_Filter_MA<Ti, TSimpleMath> where TSimpleMath:
ISimpleMath<Ti>, new()
{
// Assuming that the operations on the implementation of ISimpleMath are
thread-safe (and
// they should be, since you are passing all operands into the method
and not storing
// shared state), you can store one instance and use that across all
instances of the type.
private static readonly SimpleMathImplementation = new TSimpleMath();
// ............................................................
protected Queue<Ti> Ti_queue; // Queue (FIFO) that stores the items.
// Changed to be of type Ti.
protected Ti Ts_sum; // Sum of the values stored in the queue.
public void Clear()
{
Ts_sum = default(Ti)
} // Clear
public Ti InOutData(Ti Ti_in)
{
// Get the item.
Ti item = Ti_queue.Dequeue();
// Subtract from the sum.
Ts_sum = SimpleMathImplementation.Subtract(Ts_sum, item);
// Add the item passed in.
Ts_sum = SimpleMathImplementation.Add(Ts_sum, Ti_in);
} // InOutData
} // C_Filter_MA
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"Jon" <jmp@xxxxxxxxxxx> wrote in message
news:mpsql3l6d65hsi36j968to1gdjrbl8urhr@xxxxxxxxxx
Hi,
When I try to compile the following generic class, the compiler gives
me many errors "Cannot conver type '...' to '...'" on the lines
indicated. Besides, the C# compiler gives me errors even if I don't
declare and instantiate C_Filter_MA<Ti,Ts> in my program.
Ti and Ts should be one of {Byte, UInt16, UInt32, UInt64, SByte,
Int16, Int32, Int64, double}.
Any clue on how to be able to compile this class?
Thank you very much.
// ==============================================================
public class C_Filter_MA<Ti,Ts>
{
// ............................................................
protected Queue<Ti> Ti_queue; // Queue (FIFO) that stores the
items.
protected Ts Ts_sum; // Sum of the values stored in the
queue.
...
// ............................................................
...
// ............................................................
public void Clear()
{
...
Ts_sum =(Ts)0; // <-- Compile error.
...
} // Clear
// ............................................................
public Ti InOutData(Ti Ti_in)
{
...
Ts_sum -=(Ts)Ti_queue.Dequeue(); // <-- Compile error.
...
Ts_sum +=(Ts)Ti_in; // <-- Compile error.
...
} // InOutData
// ............................................................
} // C_Filter_MA
// ==============================================================
.
- Follow-Ups:
- Re: explicit cast between generic types
- From: Jon
- Re: explicit cast between generic types
- From: Jon
- Re: explicit cast between generic types
- From: Marc Gravell
- Re: explicit cast between generic types
- References:
- explicit cast between generic types
- From: Jon
- explicit cast between generic types
- Prev by Date: Re: Visual Studio 2008 Express RTM bug??
- Next by Date: Re: colordialog.color to int
- Previous by thread: explicit cast between generic types
- Next by thread: Re: explicit cast between generic types
- Index(es):
Relevant Pages
|