Generics Problem Question
-
Good afternoon gentlemen, I was hoping somebody could explain what is wrong with the following implementation of a simple Generic function
protected void SetValue<>(T value, int length) { byte[] data = BitConverter.GetBytes(value); if (SrbData.Length < (length + data.Length)) throw new InvalidOperationException(); Array.Copy(data, 0, SrbData, length, data.Length); }
ps, the <> is because I could not work out how to get a single greater than to display and SrbData is a byte array The Visual Studio error is Error 1 The best overloaded method match for 'System.BitConverter.GetBytes(bool)' has some invalid arguments C:\Documents and Settings\Paul\My Documents\Visual Studio 2005\Projects\Comet\SrbLibrary\SrbHeader.cs 17 27 SrbLibrary I do not have much experience with Generics so this is most likely a silly problem, but thanks for the help anyway Paul -
Good afternoon gentlemen, I was hoping somebody could explain what is wrong with the following implementation of a simple Generic function
protected void SetValue<>(T value, int length) { byte[] data = BitConverter.GetBytes(value); if (SrbData.Length < (length + data.Length)) throw new InvalidOperationException(); Array.Copy(data, 0, SrbData, length, data.Length); }
ps, the <> is because I could not work out how to get a single greater than to display and SrbData is a byte array The Visual Studio error is Error 1 The best overloaded method match for 'System.BitConverter.GetBytes(bool)' has some invalid arguments C:\Documents and Settings\Paul\My Documents\Visual Studio 2005\Projects\Comet\SrbLibrary\SrbHeader.cs 17 27 SrbLibrary I do not have much experience with Generics so this is most likely a silly problem, but thanks for the help anyway PaulBitConverter.GetBytes() accepts only several types. It seams that the type that you are substituting T with cannot be expected by BitConverter.GetBytes(). Msdn has more information about this function
#region signature my articles #endregion
-
BitConverter.GetBytes() accepts only several types. It seams that the type that you are substituting T with cannot be expected by BitConverter.GetBytes(). Msdn has more information about this function
#region signature my articles #endregion
Thanks for the response However this I understand, but I was hoping to overcome the need to duplicate a function for each type that I need to add to the bit array. Hence the attempt with Generics. My understanding is that the type T will become what ever T i pass in to it! Is the problem because the base function will only accept 9 types and generics potentially means that far more types could be passed in??
Ta Paul Help, Urgent, Need answers Urgent, Quick Help arggggghhhhhhhhhh
-
Thanks for the response However this I understand, but I was hoping to overcome the need to duplicate a function for each type that I need to add to the bit array. Hence the attempt with Generics. My understanding is that the type T will become what ever T i pass in to it! Is the problem because the base function will only accept 9 types and generics potentially means that far more types could be passed in??
Ta Paul Help, Urgent, Need answers Urgent, Quick Help arggggghhhhhhhhhh
Yes, T will become the type that you pass but GetBytes can accept only 9 types. So either the type of T should be one of them or you should write a new class like that acts like bitconverter and has a function called GetBytes that accepts many types but that means quite a lot of work
#region signature my articles #endregion
-
Yes, T will become the type that you pass but GetBytes can accept only 9 types. So either the type of T should be one of them or you should write a new class like that acts like bitconverter and has a function called GetBytes that accepts many types but that means quite a lot of work
#region signature my articles #endregion
-
Is it possible to limit the types that T can become??
Ta Paul Help, Urgent, Need answers Urgent, Quick Help arggggghhhhhhhhhh
Yes you can by adding the where clause and specifying the restriction you want to apply but I'm not sure if you can limit T to only those nine types. For the constraint that you can apply have a look at this: Types of generic constraints[^]
#region signature my articles #endregion