generics

I'm having some trouble with generics and casting in C#...

So I have a simple validation rule pattern that I'm using to do validation on entity objects. Here is my ValidationRule class: public class ValidationRule { public Func<object, bool> Rule { get; set; } public string ErrorMessage { get; set; } public ValidationRule(string errorMessage, Func<object, bool> rule) { R...

ArrayList versus an array of objects versus Collection of T

I have a class named Customer (with typical customer properties) and I need to pass around, and databind, a "chunk" of Customer instances. Currently I'm using an array of Customer, but I've also used Collection of T (and List of T before I knew about Collection of T). I'd like the thinnest way to pass this chunk around using C# and .NET ...

Coding tip - intersection types and java enums

Intersection types allow you to (kinda sorta) do enums that have an inheritance hierarchy. You can't inherit implementation, but you can delegate it to a helper class. enum Foo1 implements Bar {} enum Foo2 implements Bar {} class HelperClass { static <T extends Enum<T> & Bar> void fooBar(T the enum) {} } This is useful when you ha...

Java generics

Why is the following seen as better than the old way of casting? MyObj obj = someService.find(MyObj.class, "someId"); vs. MyObj obj = (MyObj) someService.find("someId"); ...

Can I get specific metadata from a Func<T, object>?

Consider the following code: string propertyName; var dateList = new List<DateTime>() { DateTime.Now }; propertyName = dateList.GetPropertyName(dateTimeObject => dateTimeObject.Hour); // I want the propertyName variable to now contain the string "Hour" Here is the extension method: public static string GetPropertyName<T>(this IList<...

Delphi Generic constraints problem

I am trying to create a generic list class for use with tiOPF (an Object Persistence Framework for delphi @ www.tiopf.com). Specifically I am trying to take an existing generic class (TtiObjectList) and make a generic version that uses TtiObject descenants. I have limited scope for altering the base classes as they need to compile und...

How to I override List<T>'s Add method in C#?

I am currently looking to make my own collection, which would be just like a regular list, except that it would only hold 10 items. If an item was added when there were already 10 items in the list, then the first item would be removed before the new item was appended. What I want to do is create a class that extends System.Collections....

StructureMap non-generic configuration

Hi, I am struggling with setting up StructureMap without the use of the generic fluent interface, I can't use the generic methods, because I don't know the types at design time. Ie: To choose a default constructor the only method I could find is using 'SelectConstructor<T>()', but I only know the type at runtime... This is related: ...

What is the generic alternative of Array?

In C# some collections such as ArrayList and HashTable have generic alternatives which are List<T> and Dictionary<TKey, TValue>. Does Array also have a generic alternative? ...

C# .NET passing a collection of InterfaceImplementingClass objects to a routine that takes a collection of Interface objects

I have a routine public void SomeRoutine(List<IFormattable> list) { ... } I then try to call this routine List<Guid>list = new List<Guid>(); list.Add(Guid.NewGuid()); SomeRoutine(list); And it fails with a compile-time error. System.Guid implements IFormattable, but the error I get is cannot convert from 'System.Collections....

Generics and Collections in GWT, Null Pointer

When calling a method that adds an object to a collection in GWT I get a null pointer error. I have no idea why as everything I have done creates a very simple object (only contains a string). Here is the code that calls the function and the function: public class PlantMenu extends VerticalPanel { private Collection<PlantData> pl...

Creating and intialising different sub-types without lots of casting

Boy, I am sure am messing around with a lot of casting and such in this code below. It seems like there should be a smoother way. I'm basically trying to use a builder method (CreateNewPattern) to handle creating new objects of the passed sub-class type (by the CreateNewCircularPattern and CreateNewLinePattern methods). I presently only ...

How can i return List from Class

class Program { static void Main(string[] args) { mylist myitems1 = new mylist("Yusuf","Karatoprak"); SelectedItemsList slist = new SelectedItemsList(); slist.Items.Add(myitems1); foreach( object o in slist.Items) Console.Write(o.ToString()+"\n"); ...

Generic delegate or generic cast problem

How can i make the following code compile? Action<MyClass<object, object>> func = x => Console.WriteLine(x.ToString()); public void Apply<T1, T2>(MyClass<T1, T2> target) { func.Invoke(target); } I know it doesnt work because a MyClass<T1, T2> isnt a MyClass<object, object>, but what can i do? Can i make the f...

return unknown Generic List<T>

Hello, and thanks for any assistance. How would I return from a method an unknown Generic.List type. public void Main() { List<A> a= GetData("A"); } public List<T> GetData(string listType) { if(listType == "A") { List<A> a= new List<A>() ... return a; } else { List<B> b = new List<B>() ...

Generic methods and multiple constraints

I have a generic method which has two generic parameters. I tried to compile the code below but it doesn't work. Is it a .NET limitation? Is it possible to have multiple constraints for different parameter? public TResponse Call<TResponse, TRequest>(TRequest request) where TRequest : MyClass, TResponse : MyOtherClass Thanks! ...

Generic method in Java without generic argument

In C# I can do actually this: //This is C# static T SomeMethod<T>() where T:new() { Console.WriteLine("Typeof T: "+typeof(T)); return new T(); } //And call the method here SomeMethod<SomeClassName>(); But for some reason I can't get it to work in Java. The thing I want to do is, to create a static method on a superclass, so the...

C#: Declaring and using a list of generic classes with different types, how?

Hi, Having the following generic class that would contain either string, int, float, long as the type: public class MyData<T> { private T _data; public MyData (T value) { _data = value; } public T Data { get { return _data; } } } I am trying to get a list of MyData<T> where each item would be of differen...

Generics..? Super T

A) List<? super Shape> shapeSuper = new ArrayList<Shape>(); shapeSuper.add(new Square()); //extends from SHAP shapeSuper.add(new DoubleSquare()); //extends from SQ shapeSuper.add(new TripleSquare()); //extends from DS shapeSuper.add(new Rectangle()); //extends from SHAP shapeSuper.add(new Circle()); //extends f...

How to copy between generic declared types with value type constraints

I have a generic method that copies values between value types. The following approaches give a design time error, even with the struct constraint. Any idea how I can copy or cast between the values? private Ttgt MyMethod<Tsrc,Ttgt>(Tsrc SourceObject) where Tsrc:struct where Ttgt:struct { //Error:cannot ...