generics

How to use the type parameter of a generic parent inside of local classes?

Why can't I reference the type parameter of a generic parent class inside of contained local classes? public class IsGeneric<T> { public void doSomething(T arg) { class A { T x; } A foo = new A(); foo.x = arg; T bar = foo.x; // error: found java.lang.Object, required T } } ...

How to instantiate an instance of type represented by type parameter in Scala

example: import scala.actors._ import Actor._ class BalanceActor[T <: Actor] extends Actor { val workers: Int = 10 private lazy val actors = new Array[T](workers) override def start() = { for (i <- 0 to (workers - 1)) { // error below: classtype required but T found actors(i) = new T acto...

generic function with a "has property X" constraint?

I have a third-party, closed source application that exports a COM interface, which I am using in my C#.NET application through Interop. This COM interface exports many objects that all show up as System.Object until I cast them to the appropriate interface type. I want to assign an property of all of these objects. Thus: foreach (objec...

Comparision problem in csharp.

I have created a BinaryTreeNode<T> class and then creating Add(T data) method for BinaryTree<T> class. When I try to compare Values of objects compiler says : operator '<' cannot be applied to operands of type 'T' and 'T'. Example: public void AddNode(T data) { BinaryTreeNode<T> node = new BinaryTreeNode<T>(data); ...

Retrieving static field in generic method by lambda expression

Let's say i got this: public class Foo{ public string Bar; } Then i want to create a 'static reflection' to retrieve value of Bar like this: public void Buzz<T>(T instance, Func<T, string> getProperty){ var property = getProperty(instance); } That should work. But what if Foo looks like this? public class Foo{ ...

Enum with Generics

UPDATE: Disregard this question. I had an inadvertent import in my abstract class that was the same name as COLUMNS... Someone please delete. I'm encountering a weird issue with Generics and Enum. Here are the relevant code: public abstract class AbstractModel<E extends Enum<? extends E>> { protected abstract Object getValue(in...

What is the most performant way to check for existence with a collection of integers?

I have a large list of integers that are sent to my webservice. Our business rules state that these values must be unique. What is the most performant way to figure out if there are any duplicates? I dont need to know the values, I only need to know if 2 of the values are equal. At first I was thinking about using a Generic List of i...

How to cast Generic Lists dynamically in C#?

I'm trying to cast List<object> to List<string> dynamically. I've tried several ways, but I can't find a solution. This is a small sample that shows the problem: List<object> listObject = new List<object>(); listObject.Add("ITEM 1"); listObject.Add("ITEM 2"); listObject.Add("ITEM 3"); List<string> listString = ¿¿listObject??; Thanks ...

C# -Generics Help

Just i started learning Collection and Generics in C# .I visited MSDN resource page.It directed me to lot of sub pages ,i was going page after page,finally got confused. Can anybody help me where to start and what are the essential things to learn bot about collection (non-generics) and generics?. MSDN articles are dragging me by putti...

Code re-use with Linq-to-Sql - Creating 'generic' look-up tables

Hey all, I'm working on an application at the moment in ASP.NET MVC which has a number of look-up tables, all of the form LookUp { Id Text } As you can see, this just maps the Id to a textual value. These are used for things such as Colours. I now have a number of these, currently 6 and probably soon to be more. I'm trying to pu...

Class, Interface, Generics .... need simplification

Hello, At this time, I have this piece of code for my "employee" class. But I have almost the same for "customer" and all the others. Is there a way to create an equivalent of my class "EmployeeRepository" but more something like this MyRepo<Employee> but implement IEmployeeRepository in this case, ICustomerRepository if I do this MyRe...

Getting the class of a Java generic, and interface implementation of generics

Hi guys, I'd like to make a class that looks basically like this: public class MyClass<T implements Serializable) { void function() { Class c = T.class; } } Two errors: - I cannot call T.class, even though I can do that with any other object type - I cannot enforce that T implements Serializable in this way How do I solv...

Overloads vs generic arguments

I have a question. In the framework, that was largely written before the generics came, you often see a function with lots of overloads to do something with different types. a) Parse(int data) Parse(long data) Parse(string data) ..etc That seems to be to be ok as it helps keeping code small for each method and so. On the other hand, ...

using Enumerable.Aggregate on System.Collection.Generic.Dictionary

Say that i have a generic dictionary with data like this (I hope the notation is clear here): { "param1" => "value1", "param2" => "value2", "param3" => "value3" } I'm trying to use the Enumerable.Aggregate function to fold over each entry in the dictionary and output something like this: "/param1= value1; /param2=value2; /p...

C#: Generic Interface for Numbers

I am trying to perform some generic number manipulation independent of the number type. However, I know of no way to use generics to do this. The first idea was to filter the incoming types with a where statement, but all of the number types are closed and are, therefore, not valid for a generic filter. Also, the generics don't allow sta...

In Java, why does untyped call of constructor of parameterised type provoke compiler warning?

In Java, why does an untyped invocation of a constructor of parameterised type provoke a compiler warning? Why is it okay to do similar thing with a static method? For example: class Test<T> { Test() {} static <T> Test<T> create() { return new Test<T>(); } @SuppressWarnings("unused") public static void m...

Curiously Recurring Template Pattern and generics constraints (C#)

I would like to create a method in a base generic class to return a specialized collection of derived objects and perform some operations on them, like in the following example: using System; using System.Collections.Generic; namespace test { class Base<T> { public static List<T> DoSomething() { List<T> object...

C# generics usercontrol

Helo, I would like a stuff like that : public partial class ObjectSelectorControl<T> : UserControl where T : class The problem is that the designer can't resolve this. Someone as a workaround with this ? ...

Generic Interface takes self as Parameter. Recursive Generic?

Disclaimer: I don't have a whole ton of experience with Java Generics, but my colleagues and I just spent a solid hour trying to decipher an interface that was structured like this: Interface HasAttributes<a extends HasAttributes<a,b>, b extends HasAttributesType<b>> extends Identification<a> What exactly does it mean when an int...

How can you return a Collection<T> as a Collection<Interface>?

I have a concrete class that contains a collection of another concrete class. I would like to expose both classes via interfaces, but I am having trouble figuring out how I can expose the Collection<ConcreteType> member as a Collection<Interface> member. I am currently using .NET 2.0 The code below results in a compiler error: Canno...