How can I do this with generics please

  • Thread starter Thread starter Mike Davies
  • Start date Start date
M

Mike Davies

Hi,

I want to do this but can;t figure it out! :-( Can I even do it?

public static T Get4Bytes<T>( byte [] ptr , ref int Index)
{
uint ui = 0;

try
{
ui = ( (uint) ptr[ Index++ ] ) << 24;
ui += ( (uint) ptr[ Index++ ] ) << 16;
ui += ( (uint) ptr[ Index++ ] ) << 8;
ui += (uint) ptr[ Index++ ];

}
catch (Exception e)
{
Debug.WriteLine(e);
}

return (T)ui; <--------Cannot convert type 'uint' to 'T'

}

Thanks,

Mike
 
Why do you want the method to be generic?
Do you want to return an integer containing four bytes from the ptr argument
starting with position Index?
Do it. Without generics.

Just a quick note: BitConverter class might help you a little bit.
 
Hi,

What is what you want?

I see no use here for generic. Basically you are returning the numeric value
of a type represented by 4 bytes.

How many different types can be?

Not only that, but uint should be castable to T.
 
Back
Top