generics

Extend from Generic Supertype?

In Java, am I able to extend from a generic supertype? According to this article, it looks like I should be able: http://www.ibm.com/developerworks/java/library/j-djc05133.html. However, when I do something similar in my application, I get the following error: "Cannot refer to the type parameter T as a supertype." Does anyone know if I...

WPF custom control objects databinding to List(T) based on query

In a window of my WPF application I have several hundreds of objects, they based on a custom control. They differ from each other only by name: ... <MyNamespace:MyCustControl x:Name="x4y3" /> <MyNamespace:MyCustControl x:Name="x4y4" /> <MyNamespace:MyCustControl x:Name="x4y5" /> <MyNamespace:MyCustControl x:Name="x4y6" /> <MyNamespace:M...

“Could not find type” error loading a form in the Designer

This is the exact same question as this one: “Could not find type” error loading a form in the Designer Before anyone goes closing my question please read that one. You will realize that it did not get a real answer. I hope to get a full answer (rather than a workaround) from this question. When I create a class that descends from Co...

How to convert List<Company> to List<ICompany>

Hi I would like to convert List<Company> to List<ICompany> ICompany is an interface which Company implements. public List<ICompany> FindAll() { List<Company> companies = new List<Company>(); var query = from c in _scope.Extent<Company>() select c; companies = query.ToList(); return companies.ToList<I...

Checking type parameter of a generic method in C#

Is it possible to do something like this in C#: public void DoSomething<T>(T t) { if (T is MyClass) { MyClass mc = (MyClass)t ... } else if (T is List<MyClass>) { List<MyClass> lmc = (List<MyClass>)t ... } } ...

Inherit from a generic base class, apply a constraint, and implement an interface in C#.

This is a syntax question. I have a generic class which is inheriting from a generic base class and is applying a constraint to one of the type parameters. I also want the derived class to implement an interface. For the life of me, I cannot seem to figure out the correct syntax. This is what I have: DerivedFoo<T1,T2> : ParentFoo<T1, T...

Is this a covariance problem? Not sure if brick wall.

I wrote ASP.NET pages which will manage forms. They're based on the following base class. public abstract class FormPageBase<TInterface, TModel> : Page, IKeywordProvider where TModel:ActiveRecordBase<MasterForm>, TInterface, new() where TInterface:IMasterForm { public TInterface FormData { get; set; } ...

generics in Groovy

Hi, The following Groovy code prints "it works" def printIt(Class<? extends Exception> clazz) { println "it works" } printIt(String.class) even though the parameter doesn't satisfy the constraint Class<? extends Exception> My understanding is that this is because: Type erasure in Java generics means there's no run-time generic...

How to specify a generic type should implement another generic type?

Imagine the following method public void SomeMethod<T>(T param) where T: List<T2> { } It doesn't work: Error 16 The type or namespace name 'T2' could not be found (are you missing a using directive or an assembly reference?) How do I achieve the what I clearly intended to do? ...

Exposing class library DLL to COM with generics

For the sake of this question, here is my generic class. [ComVisible(true)] public class HtmlTable<T> where T : class { List<T> listToConvert; public HtmlTable(List<T> listToConvert) { this.listToConvert = listToConvert; } } Essentially, this class is responsibly for converting a List of class T to an HTML tab...

How do I use paramertized generic types in an inner class?

Hello, I am trying to implement an inner class that has generic parameterized type. Here is my code (short version): public class AVLTree<T extends Comparable<? super T>> implements Iterable<T> { ... private class BinaryNode<T extends Comparable<? super T>> { ... } private class TreePreOrderIterator<E extends Comparable<? super...

implicitly casting a generic<T> back to T

If I write a generic class like class MyGeneric<T> is it possible to write an implicit cast to type T, so I can do stuff like: public class MyGeneric<T> { ... } public class GenericProperties { public MyGeneric<string> MyGenericString {get;set;} public void UseMyGeneric() { string sTest = MyGenericString; MyGene...

Need derived class for java generics declaration

I've run into a sticky problem that I can't seem to solve with java generics. This is a bit complicated, but I couldn't think of a simpler scenario to illustrate the problem... Here goes: I have a Processor class that requires a Context. There are different types of Context; most processors just need any abstract Context, but others ...

How to create expressions of type Class<List<?>>

Take the following: public Class<List<String>> getObjectType() { // what can I return here? } What class literal expression can I return from this method which will satisfy the generics and compile? List.class won't compile, and neither will List.<String>class. If you're wondering "why", I'm writing an implementation of Spring's ...

C++ class template of specific baseclass

Let's say I have the classes: class Base{}; class A: public Base{ int i; }; class B:public Base{ bool b; }; And now I want to define a templated class: template < typename T1, typename T2 > class BasePair{ T1 first; T2 second; }; But I want to define it such that only decendants of class Base can be used as templa...

syntax difference between generics in java and c#

I'm coming from a c# background, how do generics look in java compared to c#? (basic usage) ...

Problems with abstract types and interafaces as generic type params with the new() constraint

I need to have the generic type parameter as an interface, however I would like to instantiate the type in the generic class (SomeGenericType) as follows: class Program { static void Main(string[] args) { var val = new SomeGenericType<ISomeInterface>(); Console.ReadKey(); } } internal class SomeGenericType<...

Unbounded wildcards in Java

Is there ever a difference between an unbounded wildcard e.g. <?> and a bounded wildcard whose bound is Object, e.g. <? extends Object>? I recall reading somewhere that there was a difference in the early drafts of generics, but cannot find that source anymore. ...

Foreach loop through ObservableCollection

In WPF app I use LINQ query to get values from ObservableCollection and update the state of some objects: var shava = from clc in _ShAvaQuCollection where clc.xyID == "x02y02" select clc; switch (shava.First().Status) { case 1: x02y02.Status = MyCustControl.Status.First; break...

Instantiating Generic class using field type at runtime

Hi all, First question here. I am trying to instantiate a generic class using the type of a field. public class ValueRange<type1> { type1 min; type1 max; } void foo() { int k; ValueRange<k.GetType()> range; } This doesn't work. Any suggestions? Thanks in advance ...