generics

How to make a type safe wrapper around Variant values

I'm working with a OPC Server control that stores data tags as variant types, described by System.Runtime.InteropServices.VarEnum. These types include the following, VT_BSTR (string), VT_I2 (short) and VT_I4 (long). All these values are stored by the server as objects and then I have to cast to the correct value when I fetch them. I kn...

isAssignableFrom with reference and primitive types

I'd like to get a better understanding of the isAssignableFrom behaviour in Java between primitive and reference types. Eg: System.out.println(boolean.class.isAssignableFrom(Boolean.class)); // false System.out.println(Boolean.class.isAssignableFrom(boolean.class)); // false boolean primitive; Boolean referenceType = true; primitive =...

Java: cast collection type to subtype

Suppose class B extends class A. I have a List<A> that I happen to know only contains instances of B. Is there a way I can cast the List<A> to a List<B>? It seems my only option is to iterate over the collection, casting one element at time, creating a new collection. This seems like an utter waste of resources given type erasure makes ...

Is it possible to override a generisized function?

Hello Is it possible to override a generisized function as illustrated in the code snippet below? interface A { } interface B extends A { } abstract class C { protected abstract <T extends A> void abc(T xyz); } class D extends C { @Override protected void abc(B xyz) { // doesn't compile // The method abc(B) of type ...

Using Mockito to mock classes with generic parameters

Is there a clean method of mocking a class with generic parameters? Say I have to mock a class Foo<T> which I need to pass into a method that expects a Foo<Bar>. I can do the following easily enough: Foo mockFoo = mock(Foo.class); when(mockFoo.getValue).thenReturn(new Bar()); Assuming getValue() returns the generic type T. But that...

How do I compare the fields/properties between POCOs?

Let's say I have a POCO: public class Person { public string Name { get; set; } public DateTime DateOfBirth { get; set; } public IList<Person> Relatives { get; set; } } I want to compare two instances of Person to see if they're equal to each other. Naturally, I would compare Name, DateOfBirth, and the Relatives collection...

generic algoritm to convert the return values of the System.Data.DataRow Indexer

i am trying to write a function that will make DataRow[column] return nullable typed data. for example: if i have a property int? iValue { get; set; } and the DataRow["ID"] will have the int ID in the Database i want to make a wrapper function that will check if the value of the DataRow["ID"] is DBnull and set the iValue to null. an...

Why is my Java generics code not syntactically correct?

Could you please explain why this code is not syntactically correct? private void addEditor(final Class<? extends FieldEditor> fieldEditorClass, final Composite parent, final PropertyKey propertyKey, final String displayName){ final Composite composite = new Composite(parent, SWT.NULL); composite.setLayout(new GridLayout()); ...

Generic TryParse Extension method

Code taken from here I would like to hear some expert opinions on this extension method. I do plan to use it, but would like to hear about any known problems i may face. Am i better of using on primative types TryParse methods? public static T? TryParse<T>(this object obj) where T : struct { if (obj == null) retur...

Unknown Problem with Java Generics

Hey Im trying to use generic types for the first time in Java , as I only want my constructor to accept classes that implement the "Anealable" interface. theres a problem with my code by the only error I get is "Illegal start of Type" which is not getting very far with trying to make it work here is the code for my class package simul...

.net remoting - Problems with List<T>

I am developing a client-server application using .Net Remoting. From my server I want to return a List in response to a certain method call, however I get an exception saying that basically SoapFormatter cannot deal with generics. I need a workaround so that I am able to work with generics, or direction on how to use XmlSerializer or Da...

Java: Parameterized Runnable

Standard Runnable interface has only non-parametrized run() method. There is also Callable<V> interface with call() method returning result of generic type. I need to pass generic parameter, something like this: interface MyRunnable<E> { public abstract void run(E reference); } Is there any standard interface for this purpose or I must...

Delphi: how to use TObjectList<T>?

I need to understand how to use the generic Delphi 2009 TObjectList. My non-TObjectList attempt looked like TSomeClass = class(TObject) private FList1: Array of TList1; FList2: Array of TList2; public procedure FillArray(var List: Array of TList1; Source: TSource); Overload; procedure FillArray(var List: Array of TList2; Source:...

Java Generics - vanishing type information?

OK - I know that Java generics can be a minefield for the unwary, but I just came across a non-intuitive (to me anyway) behavior that I was wondering if anyone can explain: First of all, here's a class that compiles: public class Dummy { public List<? extends Number> getList() { return new ArrayList<Number>(); } public ...

Need some help sorting out a major abstract pattern headache within my DAL

I've caused myself a bit of an issue with my Data Access Layer. In this particular instance, I have a table that contains potentially 5 types of 'entity'. These are basically Company, Customer, Site, etc. The type is dictated by a PositionTypeId within the table. They're all in the same table as they all havethe same data structure; Posi...

Dynamic method return types in java

The following code does exactly what I want it to, but I am curious if there is a better way of going about it. This would be so much easier if Interfaces allowed static methods, or if Java methods could be generalized/parameterized to the extent they can in C#. I would much rather substitute the parameter "Class<TParsedClass> c" for "C...

Using instanceof with Java Enums

I have a situation where I'm receiving an enum from an external system, and for which I need to return an enum of our own. The two enums have the exact same literal values in them: // externalEnum is guaranteed not to be null public static MyEnum enumToEnum(final Enum<? extends Enum<?>> externalEnum) { if( externalEnum instanceof M...

what is the use of <T> in public static <T> T addAndReturn(T element, Collection<T> collection){ ....

Hi All, I am getting confused as and when i come across a generic method of this sort? public static <T> T addAndReturn(T element, Collection<T> collection){ collection.add(element); return element; } The reason being is that i could not understand why is required in this method? Also, I would be greatful to you if you can ...

web services, generics, typed DS, on Compact framework 2.0, current (2009) best practices?

Struggling here, many documents I find are CF 1.0 centric and talk of the 2.0 changes to allow generics and typed DS'es on CF 2.0. Well, CF 2.0 has come and gone, and MS still shows these without update. I am not ready to jump into WCF, and my clients have a lot of older compact framework 2.0 machines. I am fortunate in that these We...

Generate a generic list of some given type

My friend is trying to create a utility function that is given some Type and in that function it creates a generic List of that type. We're having trouble creating that list: public static List<T> GetQueryResult(string xpathQuery, Type itemType) { // this line does not work: List<itemType> lst = new List<itemType>(); return lst;...