generics

Strange behaviour with parameterized method on abstract class

Hi. Can someone tell my why this gives a compile error? I don't see why the cast to A in the second for-loop causes strings() to return a general List of Objects. import java.util.ArrayList; import java.util.List; public class E { public static void main(String[] args) { for (String s : new D().strings()) { Sys...

ASP.NET MVC View / Partial with generics

I have written a List`1 editor template for use with the EditorFor extension methods (MVC2), however I am running into issues when using generics + null objects. Given a model class MyModel { public Foo SomeFoo { get;set; } public List<Foo> SomeFooList { get;set; } } and a template in /Views/Shared/EditorTemplates/List`1.ascx ...

how to create records for writing a csv file with FileHelpers

I am using Filehelpers to import my database from files. I am now working on the reverse process and am having trouble seeing how to create the records from my data objects. All the examples I can find show going from file -> table -> file I am using interfaces with generics to convert. I use this one on the inbound conversion: publi...

Javac flags and Eclipse settings to process Class.<GenericType>method() invocations.

It happens to see Collections.<Object>asList(...) method invocations in the code, but Eclipse seems do not understand this(with my configuration) and shows this as compilation error. Is my Eclipse configuration wrong? Or this doesn't work with Sun compiler (jdk 1.6.013)? What should I check / enable for thing like this? ...

Instantiate Generic Type in C# class

Pretty basic question in C#, class Data<T> { T obj; public Data() { // Allocate to obj from T here // Some Activator.CreateInstance() method ? obj = ??? } } Not sure how to do this ? Thank you for any assistance. ...

Demystify Wildcard for me..

Why do I get a compile time error on this piece of code? public Interface Location { ....... } Class code... Map<Type, List<? extends Location>> locationsTypeMap = new HashMap<Type, List<? extends Location>>(); /** Code to add elements to the hashMap. */ newLocation = getNewLocation() while(mapHasElements){ Location....

c++ getting dynamic generic type of pointer?

Hi! the title probably is misleading, but i didn't really know how to name it. let's say I have the following structs template <typename T> struct SillyBase{ void doFunnyStuff(vector<T> vec){ dummyField = T(); for(int i=0; i<10; i++) vec.push_back(dummyField++); } T dummyField; }; struct A : pu...

Cast from Generics<T> to Specific SubClass

I have a class as such public class MyClass<T> where T: OneType { T MyObj {get;set;} public MyCLass(T obj) { } } public class SubClass: MyClass<TwoType> { } // snip for other similar class definition where, TwoType is derived from OneType. Now, I have this utility method public static MyClass<T> Factory<T>(T vd) w...

Observer pattern C# using generics

I am trying to implement the observer pattern with a slight twist, the Subject and Observer are the same class. For example, class myclass { public delegate void UpdateHandler(object sender); public event UpdateHandler UpdateEvent; public Attach(myclass obj){ // use delegate to attach update function of obj} public Update(ob...

Cast generic property of type T up to a known inheriting type

I'm getting the following compiler error Cannot convert type 'T' to 'ProjectReportingHandler' for the line var projectReport = (ProjectReportingHandler)result.Report; when trying to compile: public abstract class ReportingHandler { // Report stuff } public abstract class ProjectReportingHandler: ReportingHandler { //...

Why can I write a generic catch statement in C# that does nothing?

Possible Duplicate: Why cant I catch a generic exception in C#? I have been reviewing and writing Circuit Breaker code recently. The following method compiles, but the catch block is never entered. I have plenty of work-arounds, and this isn't the only way to get the right behavior (filtering exceptions), but I'm curious why thi...

C# (+WPF) Problems casting generic class to non-generic.

I must honestly say that I do not really understand much about casting, but I thought this would work. I have a Generic Class Test: public class Test<T,U> { T variable1; U variable2; //etc. } I need to use this class in a WPF view, and since you can't create generic views in WPF (how lovely) I thought: lets just use a...

Cast value type to generic

I have a generic class that I need to constrain to only value types (int, float, etc.). I have a method that calls the Parse method based on a test of the type. For example: class MyClass<T> { ... private static T ParseEntry(string entry) { if(typeof(T) == typeof(float)) { return (T) float.Pars...

"Ambiguous Constructor Reference" XmlSerializer

I am trying to serialize a List<MyObject>. When I create my XmlSerializer as such: XmlSerializer xmlSerializer = new XmlSerializer(List<MyObject>); I get the following error: Ambiguous Constructor Reference How can I fix this so I can serialize and deserialize my list? ...

Help with refactoring to an Action<T> delegate

In a test method (for a Fluent NHibernate mapping, although that's not really relevant) I have the following code wrapped in a bunch of usings and try/catch blocks: new PersistenceSpecification<Entry>(session) .CheckProperty(e => e.Id, "1") .VerifyTheMappings(); I would like to refactor this so that I can pass it to a helper m...

How to make an Abstract Base class IComparable that doesn't compare two separate inherited classes?

(C#, VS2008) In a program I'm working on, I've got lots of objects that all have an ID and implement IComparable so that List<>-s of the various objects are easily searchable by ID. Since I hate copy/pasting code, I thought I'd abstract that bit of functionality down to a base class, like so: using System; namespace MyProg.Logic { ...

Problems casting objects of type Func<T,T> in C#

Hi there, Why doesn't this compile? I suppose there are ways to work around this; just curious. Thanks! static void Main(string[] args) { Func<int, int> f_int = n => n * n; Func<int, double> f_double = (Func<int, double>)f_int; } ...

How to write a generic method in Java

How to write a generic method in Java. In C# I would do this public static T Resolve<T>() { return (T) new object(); } Whats the equivalent in Java? ...

Calling method from another window (Class) issue

In code behind file of the main window of WPF application I have a method quering a database with LINQ to SQL and writing results to an ObservableCollection: public void GetStateByDate(string shcode) { MydbDataContext contextSts = new MydbDataContext(); _ShAvaQuCollection.Clear(); var sts = from p in co...

Select random from generic lists with different <T>

I'm looking for the best way to make it possible to get a random element from a List where the T's will be objects of different types that is not related via a base class. I've been loooking at creating an extension method to List, or a helper method that recieves a List, but I haven't been able to get it together. Each time I've run in...