generics

Java Generics Hell - Passing in this but it wants type T

I'm currently developing a lightweight, general-purpose simulation framework. The goal is to allow people to subclass the Simulation and Scenario objects for their domain-specific needs. Generics seemed like an appropriate way to achieve this, but I'm afraid I might be dipping into generics hell. The Sim object provides access to the si...

Overriding inherited generic methods

I have this code in base class protected virtual bool HasAnyStuff<TObject>(TObject obj) where TObject:class { return false; } In child class I am overriding protected override bool HasAnyStuff<Customer>(Customer obj) { //some stuff if Customer.sth etc return false; } I am getting this error '''T...

How to declare a generic constraint that is a generic type

I have a two generic abstract types: Entity and Association. Let's say Entity looks like this: public class Entity<TId> { //... } and Association looks like this: public class Association<TEntity, TEntity2> { //... } How do I constrain Association so they can be of any Entity? I can accomplish it by the following: public class...

Open generic interface types of open implementation don't equal interface type?

Here's a test that should, in my opinion be passing but is not. [TestMethod] public void can_get_open_generic_interface_off_of_implementor() { typeof(OpenGenericWithOpenService<>).GetInterfaces().First() .ShouldEqual(typeof(IGenericService<>)); } public interface IGenericService<T> { } public class OpenGenericWithOpenService...

Casting Generic Types

Public Function CastToT(Of T)(ByVal GenericType(Of Object) data) As GenericType(Of T) Return DirectCast(data, GenericType(Of T)) End Function The above clearly does not work. Is there any way to perform this cast if I know that all objects inside data are in fact of Type T? ...

WPF View/ViewModels using Generics- how?

New to WPF, so please bear with me... Suppose I have 2 tables in SQL Thing OtherThing Both have the exact same fields: ID (int) Name (string) Description (string) IsActive (bit/bool) DateModified (DateTime) So, I want to create one Model (not two) and do something like this: BaseModel<T>() { public int ID {get;set;} ... } et...

Refactoring exercise with generics

I have a variation on a Quantity (Fowler) class that is designed to facilitate conversion between units. The type is declared as: public class QuantityConvertibleUnits<TFactory> where TFactory : ConvertableUnitFactory, new() { ... } In order to do math operations between dissimilar units, I convert the right hand side of the oper...

C# generics when T could be an array

I am writing a C# wrapper for a 3rd party library that reads both single values and arrays from a hardware device, but always returns an object[] array even for one value. This requires repeated calls to object[0] when I'd like the end user to be able to use generics to receive either an array or single value. I want to use generics so ...

Uses for static generic classes?

What are the key uses of a Static Generic Class in C#? When should they be used? What examples best illustrate their usage? e.g. public static class Example<T> { public static ... } Since you can't define extension methods in them they appear to be somewhat limited in their utility. Web references on the topic are scarce so cl...

how can I implement Comparable more than once?

I'm upgrading some code to Java 5 and am clearly not understanding something with Generics. I have other classes which implement Comparable once, which I've been able to implement. But now I've got a class which, due to inheritance, ends up trying to implement Comparable for 2 types. Here's my situation: I've got the following classes/i...

Scala: "Parameter type in structural refinement may not refer to an abstract type defined outside that refinement"

Hi, I'm having a problem with scala generics. While the first function I defined here seems to be perfectly ok, the compiler complains about the second definition with: error: Parameter type in structural refinement may not refer to an abstract type defined outside that refinement def >>[B](a: C[B])(implicit m: Monad[C]): C[B] = { ...

Using generics in dotnet for functions with any number of arguments?

I would like to have a function that can "wrap" any other function call. In this particular case, it would allow me to write some more generic transaction handling around some specific operations. I can write this for any particular number of arguments, e.g. for one argument: Public Shared Sub WrapFunc(Of T)(ByVal f As Action(Of T), B...

Why can I derived from a templated/generic class based on that type in C# / C++

Title probably doesn't make a lot of sense, so I'll start with some code: class Foo : public std::vector<Foo> { }; ... Foo f; f.push_back( Foo() ); Why is this allowed by the compiler? My brain is melting at this stage, so can anyone explain whether there are any reasons you would want to do this? Unfortunately I've just seen a sim...

Using Generics to return a literal string or from Dictionary<string, object>

I think I outsmarted myself this time. Feel free to edit the title also I could not think of a good one. I am reading from a file and then in that file will be a string because its like an xml file. But in the file will be a literal value or a "command" to get the value from the workContainer so <Email>[email protected]</Email> or <...

Generic Subclasses linked to Generic Superclasses

I am creating a class that can inherit properties from all its super-classes. That's all nice and standard but I have a unique problem of the lowest subclass needing to inherit from different superclasses that all inherit from the same superclass while only being able to inherit from one class depending on the current setup of the system...

c# How to implement a collection of generics

I have a worker class that does stuff with a collection of objects. I need each of those objects to have two properties, one has an unknown type and one has to be a number. I wanted to use an interface so that I could have multiple item classes that allowed for other properties but were forced to have the PropA and PropB that the worke...

Using Generics to typecast object type to generic type

I am very new to generics and trying to implement it. How can i use it here. private T returnValueFromGrid(int RowNo, int ColNo) { return (T) dgvCurrencyMaster.Rows[RowNo].Cells[ColNo].Value; } I am trying to convert below value to generic type and then return it. dgvCurrencyMaster.Rows[RowNo].Ce...

C# delegate or Func for 'all methods'?

Hi, i've read something about Func's and delegates and that they can help you to pass a method as a parameter. Now i have a cachingservice, and it has this declaration: public static void AddToCache<T>(T model, double millisecs, string cacheId) where T : class public static T GetFromCache<T>(string cacheId) where T : class So in a pl...

C# generics with MVVM, pulling the T out of <T>

My Model is a generic class that contains a (for example) Value property which can be int, float, string, bool, etc. So naturally this class is represented something like Model<T>. For the sake of collections Model<T> implements the interface IModel, although IModel is itself empty of any content. My ViewModel contains and instance of M...

restrict T to a int value?

How can i solve this error msg? static public class Blah { public static T val<T>(this bool b, T v) { return b == true? v:0; } } error Type of conditional expression cannot be determined because there is no implicit conversion between 'T' and 'int ...