Re: explicit cast between generic types

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
// ==============================================================



.



Relevant Pages

  • Re: java package - money
    ... public money(int value1, int value 2) ... public void addMoney(int value1, int value2) ... You don't really need to repeat the word "Money" in the method names, since the methods are specific to the "Money" clss anyway. ...
    (comp.lang.java.help)
  • Re: Vergleichsoperatoren in einer Datenbank (Tabelle) ablegen
    ... Select Case Opr ... Return Value 1 = Value2 ... Return Value1 < Value2 ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: problem in java question
    ... It was value1 and value2. ... This seems to contradict the earlier instructions, ... And do you know the lifecycle of an applet? ...
    (comp.lang.java.programmer)
  • Re: Best practice for set-and-test idiom?
    ... On Aug 15, 2008, at 8:12 AM, Gavin Sinclair wrote: ... it's retrieving worthwhile data, it's worth a line of its own. ...
    (comp.lang.ruby)
  • Re: Performance Concern
    ... Look at below script which changing NVARCHAR ... alter table alter column [value2] varcharNULL ... > the some of the data from the char data type that is imported as, ... > int, money, and a few other data types. ...
    (microsoft.public.sqlserver.programming)