short[] to int[] -- Generic? Overload?
-
Hi, I've inherited a batch of functions whose signatures differ only in the fact that one group takes a short[] as the first parameter and one takes an int[] as the first parameter. I'd like to merge the bulk of the code into one function so that changes don't have to be replicated accross versions (there already is some code difference, but no functional difference!). My two ideas were: to use a generic method, or to have one of the overloaded versions call the other version. The problem with the first (generics) is that I only want it to be able to accept int[] or short[], not some other type, and there are some explicit assignments such as "shortArray[i] = -12345;" that the compiler doesn't appreciate when I try to make it generic. The problem with the second, is that I cannot figure out how to convert from a short[] to an int[] without copying to another array and converting it in the loop (.CopyTo), the arrays are massive, so every time I have to copy is a performance hit (it isn't exactly performance critical at the moment, but I'd like to avoid needless operations right now to save time later during optimization). Any suggestions? Thanks, Phil
-
Hi, I've inherited a batch of functions whose signatures differ only in the fact that one group takes a short[] as the first parameter and one takes an int[] as the first parameter. I'd like to merge the bulk of the code into one function so that changes don't have to be replicated accross versions (there already is some code difference, but no functional difference!). My two ideas were: to use a generic method, or to have one of the overloaded versions call the other version. The problem with the first (generics) is that I only want it to be able to accept int[] or short[], not some other type, and there are some explicit assignments such as "shortArray[i] = -12345;" that the compiler doesn't appreciate when I try to make it generic. The problem with the second, is that I cannot figure out how to convert from a short[] to an int[] without copying to another array and converting it in the loop (.CopyTo), the arrays are massive, so every time I have to copy is a performance hit (it isn't exactly performance critical at the moment, but I'd like to avoid needless operations right now to save time later during optimization). Any suggestions? Thanks, Phil
Use a generic function and then check the Type to make sure that it is a valid type - i.e. throw an exception if it's not a short[] or int[]. Hint - use the is operator.
Deja View - the feeling that you've seen this post before.
-
Use a generic function and then check the Type to make sure that it is a valid type - i.e. throw an exception if it's not a short[] or int[]. Hint - use the is operator.
Deja View - the feeling that you've seen this post before.
Hi Pete, I'm sure this idea would work (with a bit of effort), but I don't know if it will work for me. Correct me if I'm wrong, but I'd still have to cast it to the appropriate type (short[] or int[]) and then carry around whichever version is the correct one along with flags signifying which to use (or copy it over to another short[] if it is an int[], which brings me to the 2nd problem presented in the original post). Adding an additional type - long, for instance, would require even more logic to be added all over the function to use the correct version. Furthermore, I'd like it to throw a compile time error and not a run-time exception.
-
Hi Pete, I'm sure this idea would work (with a bit of effort), but I don't know if it will work for me. Correct me if I'm wrong, but I'd still have to cast it to the appropriate type (short[] or int[]) and then carry around whichever version is the correct one along with flags signifying which to use (or copy it over to another short[] if it is an int[], which brings me to the 2nd problem presented in the original post). Adding an additional type - long, for instance, would require even more logic to be added all over the function to use the correct version. Furthermore, I'd like it to throw a compile time error and not a run-time exception.
All you need to do to support part of your problem is do something like:
public void Test<T>(IList<T> list) { if (list.Count > 0) { Console.WriteLine("Hello"); } }
Then, you can pass in short[] and int[] to your hearts content. It doesn't solve the constraining to short[] and int[] issues, but it does solve the other side (and you don't have to copy the arrays over).
Deja View - the feeling that you've seen this post before.
-
Hi, I've inherited a batch of functions whose signatures differ only in the fact that one group takes a short[] as the first parameter and one takes an int[] as the first parameter. I'd like to merge the bulk of the code into one function so that changes don't have to be replicated accross versions (there already is some code difference, but no functional difference!). My two ideas were: to use a generic method, or to have one of the overloaded versions call the other version. The problem with the first (generics) is that I only want it to be able to accept int[] or short[], not some other type, and there are some explicit assignments such as "shortArray[i] = -12345;" that the compiler doesn't appreciate when I try to make it generic. The problem with the second, is that I cannot figure out how to convert from a short[] to an int[] without copying to another array and converting it in the loop (.CopyTo), the arrays are massive, so every time I have to copy is a performance hit (it isn't exactly performance critical at the moment, but I'd like to avoid needless operations right now to save time later during optimization). Any suggestions? Thanks, Phil
Hi Phil, how about this: - build one method DoIt() that accepts IArray as its main operand - define an interface IArray similar to an array, supporting whatever Doit() needs - define a class that implements IArray when given an int[] - build a method that accepts an int[], instantiates said class and calls DoIt() - define a class that implements IArray when given a short[] - build a method that accepts a short[], instantiates said class and calls DoIt() That's it, solved without generics. Would even run on 1.x All this at the expense of a glue layer (the two classes described). :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hi Phil, how about this: - build one method DoIt() that accepts IArray as its main operand - define an interface IArray similar to an array, supporting whatever Doit() needs - define a class that implements IArray when given an int[] - build a method that accepts an int[], instantiates said class and calls DoIt() - define a class that implements IArray when given a short[] - build a method that accepts a short[], instantiates said class and calls DoIt() That's it, solved without generics. Would even run on 1.x All this at the expense of a glue layer (the two classes described). :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
Yes, perfect! I only need an indexer, so the Interface would be nice and light. Thanks again Luc!