generics

how to make this a generic method in java?

public void setSRC(int var) { if (src== null) { src = new int[1]; src[0] = var; } else { int i = 0; int[] temp = null; temp = new int[src.length]; temp = src; i = temp.length; src = new int[i+1]; for (int j =0; j < temp.length ; j++) ...

Reconstructing generic types at runtime with Guice via Types and TypeLiterals

I have a few types that are like this // a value that is aware of its key type (K) Bar<K> // something that deals with such values and keys Foo<V extends Bar<K>, K> how would one recreate Foo such that you could consume it in guice? the bit I'm stuck on is how to cross reference the K from Bar to the 2nd parameterised type of Foo. s...

Serializing a generic collection with XMLSerializer in VB.NET

Why won't XMLSerializer process my generic list? Sub Main() Serializing() End Sub <System.Serializable()> _ Public Class User Public Sub New() End Sub Public Sub New(ByVal Username As String, ByVal UserId As Integer) Name = Username ID = UserId End Sub Public Name As String Public ID As Int...

Can Delegate.DynamicInvoke be avoided in this generic code?

This question is partly about delegates, and partly about generics. Given the simplified code: internal sealed class TypeDispatchProcessor { private readonly Dictionary<Type, Delegate> _actionByType = new Dictionary<Type, Delegate>(); public void RegisterProcedure<T>(Action<T> action) { _actionByType[typeo...

Why IList<T> does not have Insert methods which take IEnumerable<T>?

I'm in a situation where I just want to append values in string array (type String[]) to an object with IList<String>. A quick look-up on MSDN revealed that IList<T>'s Insert method only has a version which takes an index and an object T, and does not have a version which takes IEnumerable<T> instead of T. Does this mean that I have to...

Is there anyway to store different types into a typed generic array?

Hi, Is there anyway to have a typed array where each element is a different object? ...

C# InvalidCastException Though Same Base Class

The following code throws an InvalidCastException. public static MachineProductCollection MachineProductsForMachine( MachineProductCollection MachineProductList, int MachineID) { return (MachineProductCollection) MachineProductList.FindAll(c => c.MachineID == MachineID); } This surprises me since MachineProductCollecti...

How this Generic Inheritance is done (internally) ?

Hi, How do you think the following code? Is this good? If so, why it is? If not, why it is not? And how CLR see this code? public abstract class EntityBase<TEntity> : IEquatable<TEntity> { public bool Equals(TEntity other) { // check equalitiy } // yes, below is object's Equals and GetHashCode method ...

What is the easiest way to foreach through a List<T> removing unwanted objects?

In my application, _collection is a List from which I need to remove all User objects which do not match the criteria. However, the following code gets an invalid operation error in its second iteration since the _collection itself has been changed: foreach (User user in _collection) { if (!user.IsApproved()) { _collect...

Creating an array that contains only objects of a given class

Ok, so I have the code below (Objective-C FYI) and I was wondering if I want to create an NSMutableArray of c_data objects, how would I go about doing that? It's sort of like declaring a List<c_data> cData in C#. @interface c_data : NSObject { double value; int label; int ID; } @property double value; @property int label...

A class implementing an interface that takes an enum

So, say I have a simple enum and a class that uses it: enum ThingType { POTATO, BICYCLE }; class Thing { public void setValueType(ThingType value) { ... } public ThingType getValueType() { ... } } But, in reality, I have lots of different classes that implement setValueType, each with a different kind of enum. I want to make ...

When, if at all, should IEnumerable be used over IEnumerable<T>?

In the example below, if client code using GetPeople wanted to print the name and age of each person to the console, it would have to use reflection (I suppose) to determine that query contained an IEnumerable(Of Person) and then cast it as such to get at its properties. Public Function GetPeople() As IEnumerable Dim query = From p ...

Casting to a recursively bounded type

I defined a Java interface to represent the ability of an object to copy itself (no, I don't want to use Cloneable, but thanks for the suggestion ;-). It's generic: public interface Copiable<T> { T copy(); } An object will only make copies of its own type: public class Foo implements Copiable<Foo> { Foo copy() { return new Fo...

Do special processing for a type in a generic class

Do special processing for a type in a generic class Pinnew member dan neely 19mins ago I'm trying to roll up some old (originally .net 1.1) abstract classes into generics. The classes in question all provide similar functionality for a data object of a specific type. For the most part things are going well, but I've ran into a few pla...

C# - How to reflect the generic parameter that was used for inheritance

Let's say I have the following class hierarchy: TaskViewer inherits from ListViewer<Task> which in turn inherits from ViewerBase. If I debug into a method that is declared in ViewerBase and look at this.GetType(), it correctly returns TaskViewer. However, I cannot find a property or method which will return me the generic parameter tha...

What is SuppressWarnings ("unchecked") in Java?

Sometime when looking through code, I see many methods specify an annotation: @SuppressWarnings("unchecked") What does this mean? ...

How to avoid unchecked-conversion-warning in Java, if you use legacy libraries?

I like the generics-feature in java and use it often. But I have a problem, if I use libraries that aren't yet aware of generics. An example are servlets. If you use ServletRequest.getParameterMap() the result will be a raw map, but it includes only String as keys and String[] as values. So I want to assign it to a Map. But for this assi...

Type parameter constraints for the liskov principle in C#.NET

I try to create a generic interface that inherits the System.ICloneable interface but where the returntype of the Clone()-method is T. Of course the T-type needs constraints to be sure it's an inheritance of the System.Object-class but the following code is not working. public interface ICloneable<T> : System.ICloneable where T : object...

Detecting Interfaces in Generic Types

Hi, I have a method: public void StoreUsingKey<T>(T value) where T : class, new() { var idModel = value as IIDModel; if (idModel != null) Store<T>(idModel); AddToCacheUsingKey(value); } that I would like to optionally call the following method, based on the value parameter's implementation of IIDModel. public void Store...

simple generic list in java

Why are java generics so tricky? I thought I finally understood, but eclipse gives me an error at the line in somOtherMethod below using either of the getOuterList methods below. protected List<?> getOuterList() { // blah blah } protected List<? extends Object> getOuterList() { // blah blah } protected void someOtherMethod() { ...