generics

Aggregating contributions from multiple donors

As I try to modernize my C++ skills, I keep encountering this situation where "the STL way" isn't obvious to me. I have an object that wants to gather contributions from multiple sources into a container (typically a std::vector). Each source is an object, and each of those objects provides a method get_contributions() that returns any...

C# Generic new() constructor problem

I'm trying to create a new object of type T via its constructor when adding to the list. I'm getting a compile error: The correct error message is: 'T': cannot provide arguments when creating an instance of a variable But it does! Any ideas? public static string GetAllItems<T>(...) where T : new() { ... List<T> tabListItems = ne...

How to sort list type generic if more than one property?

I have a list-generic that has a property (class type). I need a sort method for Z parameters (TrainingSet): public override List<TrainingSet> CalculatedDistancesArray (List<TrainigSet> ts, double x, double y, int k) { for (int i =0; i < ts.Count; i++) { ts[i].Z = (Math.Sqrt(Math.Pow((ts[i].X - x), 2) ...

ArrayList question

Just a minor problem with Arraylist. I want to sort a ArrayList<Client> by name. Class Client{ String name; int phonenumber ..} This code does the work, but i'm having a compiler warning: "uses unchecked or unsafe operations". Whats the problem? public void sortByName(){ Collections.sort(ListofClients, new NameComparator()); }...

Convert StringCollection to List<String>

Normally, I'd choose List<String> [or, in VB, List(Of String)] over StringCollection whenever possible: see also Best string container. However, as it seems, generics — and hence, List<String> — are apparently not supported in VS 2008's settings designer. Therefore, if I want to use a list of strings in my user settings, I have to resor...

Nested generic collections: How to implement reference from item to container?

Hi! While implementing a design using nested generic collections, I stumbled across those limitations apparently caused by C#'s invariant Generics: Cannot convert from 'Collection<subtype of T> to 'Collection<T>' That means, the following will not work, apparently due to the invariance of Generics: class Outer<TInner, TInnerItem> wh...

How can I write a generic container class that implements a given interface in C#?

Context: .NET 3.5, VS2008. I'm not sure about the title of this question, so feel free to comment about the title, too :-) Here's the scenario: I have several classes, say Foo and Bar, all of them implement the following interface: public interface IStartable { void Start(); void Stop(); } And now I'd like to have a contai...

Are there strongly-typed collections in Objective-C?

I'm new to Mac/iPhone programming and Objective-C. In C# and Java we have "generics", collection classes whose members can only be of the type declared. For example, in C# Dictionary<int, MyCustomObject> can only contain keys that are integers and values that are of type MyCustomObject. Does a similar mechanism exist in Objective-C...

In asp.net mvc is it possible to make a generic controller?

I'm attempting to create a generic controller, ie: public class MyController<T> : Controller where T : SomeType { ... } However, when I try to use it, I'm running into this error everywhere... Controller name must end in 'Controller' So, my question, Is it possible to make a generic controller in asp.net mvc? Thanks! ...

Generic return

Hi, there is an immutable class: Scope<Cmp extends Comparable<Cmp>> public Scope<Cmp> crop(Scope<Cmp> scope) { ... return new Scope<Cmp>(starts, ends); } it has many similar methods is extended by: Timerange extends Scope<Date> and many others (also immutable). Id like them to return object of its type. For example: time...

C# Generic List Union Question

I'm trying to merge 2 lists using "Union" so I get rid of duplicates. Following is the sample code: public class SomeDetail { public string SomeValue1 { get; set; } public string SomeValue2 { get; set; } public string SomeDate { get; set; } } public class SomeDetailComparer : IEqualityComparer<SomeDetail> { bool IEqual...

IList(Of T).Sort help

Dim classCodeDetails As List(Of ClassCodeDetail) = db.ClassCodeHeaders.Single(Function(cch) cch.CLCH_ID = classCodeHeaderId ).ClassCodeDetails.ToList() classCodeDetails.Sort(Function(c1, c2) c1.Make.MAKE_English.CompareTo(c2.Make.MAKE_English) ) My question is how can I sort on multiple attributes? I want to...

Generic collection loading from the generic type

I have the following code, where i am trying to create a generic collection for the objects in my DAL (just an exercise, not actually production code). My problem is that i want to use the type passed in's Read method (which is part of an interface that the classes implement). I cannot create a new T so i dont have an instance of the...

Java generics

Is it possible to make a method that will take anything, including objects, Integers etc? I have a method checking the value if it is null and I was thinking perhaps it could be done with generics instead of overloading. Unfortunately, trying nullChecking(Class<? extends Object> value){ ... } won't allow Integers as they extend Number...

C#: Printing all properties of an object

Is there a method built in to .NET that can write all the properties and such of an object to the console? Could make one using reflection of course, but I'm curious to if this already exists... especially since you can do it in Visual Studio in the Immediate Window. There you can an object name (while in debug mode), press enter, and it...

What types to use for boxing in generics

I've written a simple abstract generic class in C# (.NET 2.0) and I preferably want to limit it to only reference types, so I can indicate no value by a null. However, I also want to use types such as long and decimal, why don't allow null (being structs after all). I considered making the class public abstract Field<Nullable<T>> { ...

ASP.NET MVC - Why doesn't my view inherits properly from System.Web.Mvc.ViewPage(of T)

EDIT: Page language is VB UPDATE I'm going completely crazy. Here are some screenshots, I hope somebody sees what's going on (TestClass is a simple class with ID and Name property): View (list, model = IEnumerable) View (index, no model) Controller Resulting page at runtime (list: error) Resulting page at runtime (index: works) So...

How to constrain generic type to must have a construtor that takes certain parameters?(C#)

I have a wrapper generic class that intended to be used with a set of types. Those types are generated by a utility and are all derived from a base class ClientBase. While ClientBase has only a default constructor, all generated types have default constructor as well as a constructor takes a string as parameter. In the constructor of the...

Why does casting List<T> into IList<T> result in reduced performance?

I was doing some performance metrics and I ran into something that seems quite odd to me. I time the following two functions: private static void DoOne() { List<int> A = new List<int>(); for (int i = 0; i < 200; i++) A.Add(i); int s=0; for (int j = 0; j < 100000; j++) { f...

Factory method pattern in java using generics, how to?

I have code that looks like follows: public interface BaseDAO{ // marker interface } public interface CustomerDAO extends BaseDAO{ public void createCustomer(); public void deleteCustomer(); public Customer getCustomer(int id); // etc } public abstract class DAOFactory { public BaseDAO getCustomerDAO(); public static DAOFactory getIn...