473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How can I do this with generics please

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
Jun 9 '06 #1
2 1190
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.

"Mike Davies" <mike@(cutmeout)scrappy.freeserve.co.uk> wrote in message
news:64********************************@4ax.com...
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

Jun 9 '06 #2
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.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Mike Davies" <mike@(cutmeout)scrappy.freeserve.co.uk> wrote in message
news:64********************************@4ax.com...
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

Jun 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
13
by: Anders Borum | last post by:
Hello! Now that generics are introduces with the next version of C#, I was wondering what kind of performance gains we're going to see, when switching from e.g. the general hashtable to a...
3
by: Pierre Y. | last post by:
Hi, I've just read that article : http://msdn.microsoft.com/library/en-us/dnvs05/html/csharp_generics.asp I'm asking something. Why are generics constraints not been implemented using...
4
by: KC | last post by:
Could some one explain to me the casting rules for sending generic lists, ex. List<Person>, to a function that accepts List<object>? I cannot get the following easy-cheesy app to work. I get the...
5
by: PeterLawrance | last post by:
Hi, I would expect that inheritance through the parameters of a generic class would be fine however I get errors as below in the following code. Public Class Form1 Sub test() Dim xxx As...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
18
by: riftimes | last post by:
Hello, would you help me to find totorials with examples about generics and Dictionary thank you.
7
by: JCauble | last post by:
I have a question about using Generics with Interfaces and some of there inheritance issues / problems. If this is not possible what I describe below I will have to go a different route and would...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.