generics

Using the `where T : SOMETHING` construct in C#

Just found a bit of code someone here had written to access some DB Entities... public static OurCustomObject GetOurCustomObject(int primaryKey) { return GetOurCustomObject<int>(primaryKey, "usp_GetOurCustomObjectByID"); } public static OurCustomObject GetOurCustomObject(Guid uniqueIdent) { return GetOurCustomObject<Guid>(uniqu...

Is it possible to write a generic function in .NET that only accepts numerical types?

Suppose I want to write a function like the following (as usual, a trivial example for illustrative purposes): Public Function calcSqSum(Of T)(ByVal list As IEnumerable(Of T)) As T Dim sumSq As T For Each item As T In list sumSq += (item * item) Next Return sumSq End Function As you can probably guess, this f...

Dynamic Sort Criteria for Generic List

The purpose of this is to avoid writing a ton of if() statements. Here is my current code: public override List<oAccountSearchResults> SearchForAccounts(oAccountSearchCriteria searchOptions) { List<oAccountSearchResults> results = Service.SearchForAccounts(searchOptions); results.Sort((a1, a2) => a2.AccountNumber.CompareTo(a1.A...

How do I tell whether a Type implements IList<>?

I want to write a method that uses Reflection to tell whether a given Type implements IList<T>. For example: IsGenericList(typeof(int)) // should return false IsGenericList(typeof(ArrayList)) // should return false IsGenericList(typeof(IList<int>)) // should return true IsGenericList(...

Generics: why won't this compile?

Given the following code: class A<T> { internal void Add(T obj) { } } class C { } class B<T> where T : C { public B() { A<T> a = new A<T>(); a.Add(new C()); } } The call to Add does not compile. It does when I cast it to T first: a.Add((T)new C()); It might be the sleep deprivation, but what am I missing here? ...

Can you have a generic List(of T) in your settings file???

Hi, In my settings file, I would like to have one setting that is of the type List(of Myclass).... I can't find anything on the net that does this. Is it possible? ...

Can AS3 vectors be declared with a type reference?

Instead of this: var v:Vector.<String> = new Vector.<String>(); is there any way to do something like this? var myType:Class = String; var v:Vector.<myType> = new Vector.<myType>(); Obviously that doesn't work as written, but hopefully you get the idea. ...

Creating a generic object based on a Type variable

I need to create a generic object based on a type that is stored in a database. How can I acheive this? The code below (which won't compile) explains what I mean: string typeString = GetTypeFromDatabase(key); Type objectType = Type.GetType(typeString); //This won't work, but you get the idea! MyObject<objectType> myobject = new MyObjec...

Casting Problem within generic types but the same types

The interface: interface IPlay<T> { } Classes: class Mp3Player : IPlay<int> { } class OggPlayer : IPlay<double> { } class DummyPlayer : IPlay<object> { } Trying to use : 1. IPlay<object> player = new Mp3Player (); 2. IPlay<int> player2 = new OggPlayer (); A Big why 1 and 2. usages can not cast? int to object, or, int to double. It's...

Java Generics, extended Generics and abstract classes

I've got the following classes set up: public abstract class Process<T,S> { ... } public abstract class Resource<T, S extends Process<T, S>> { protected S processer; ... } public class ProcessImpl<EventType1, EventType2> { ... } public class ResourceImpl extends Resource<EventType1, ProcessImpl> { processer = new ...

Want to create a custom class of type Dictionary<int, T>

Hi, I want to create a custom class that basically wraps a dictionary. I want to add a property to it called Name. I tried: public class MyDictionary<int, T> : Dictionary<int, T> { public string Name { get; set;} } Doesn't seem to be working, any ideas? Update THe error I'm getting is: Type parameter declaration must be...

How to pass a generic type not having a Interface to a Of T function

I have a following code which works fine MsgBox(AddSomething(Of String)("Hello", "World")) Public Function AddSomething(Of T)(ByVal FirstValue As T, ByVal SecondValue As T) As String Return FirstValue.ToString + SecondValue.ToString End Function Now we are redesigning the application to work with parameters of different types whi...

C# generic overload - Compiler can't determine correct call

I don't understand why the compiler can't resolve the correct overload to use here. (code below) There is only one version of Add() that is appropriate- BigFoo is an IFoo, and does not implement IEnumerable where T is an IFoo. But it insists on reporting an ambiguity. Any ideas? I tried adding a second generic type parameter- Add where T...

C# generics syntax for multiple type parameter contraints

I need a generic function that has two type constraints, each inheriting from a different base class. I know how to do this with one type: void foo<T>() where T : BaseClass However, I don't know how to do this with two types: void foo<TOne, TTwo>() where TOne : BaseOne // and TTwo : BaseTwo ??? How do you do this? (using .NET 2) ...

How Do I filter Generic List<> with Object of same Class

I have scoured the internet for this answer and asked several developers and have come up short. I have a Class called StaffingPositionsDataContract that I am loading mock data (for now) into a List<> and returning to a page. This is working great, but now I need to filter the list based on another list of values that are inputted on t...

Generic SortedList, How to find the index of the first element that is greater than the search key?

Without using extensions methods (LINQ). I am restricted to .NET 2.0 unfortunately. (Yeah, it sucks) Looking for something close to O(log(n)). Thanks for your help. ...

How to perform a binary search on IList<T>?

Simple question - given an IList<T> how do you perform a binary search without writing the method yourself and without copying the data to a type with build-in binary search support. My current status is the following. List<T>.BinarySearch() is not a member of IList<T> There is no equivalent of the ArrayList.Adapter() method for List<T...

C# Generics

I'm trying to create an instance of a generic class, without knowing what type to cast it to it until runtime. I've written the following code Type pType = propertyInfo.GetType(); ObjectComparer<pType> oc = new ObjectComparer<pType>(); Hopefully that gives you an idea what I'm trying to do, however it wont compile it ...

Generics in c#: "two levels of indirection"

Hi, There is a piece of code that I would implement like this in java: public void doIt( T extends MyEventArgs<? extends MyBaseClass> obj ) { ... } How would I do that in c#? I first thought this would be it: public void oIt( T obj ) where T : MyEventArgs<P> where P : MyBaseClass { ... } But apparantly my syntax is wrong. ...

Functors when should I use them whats their intended use.

I Just can't seem to wrap my head around them. As I understand it's dynamicly adding logic to a class. Are classes within the framework prepaired for this? Why should I just extend the class and add the funtionality to it in the extention. I would be globally accessable and afaik much easier to maintain. I've Read there are 4 functor ...