generics

Why does java.util.Properties implement Map<Object,Object> and not Map<String,String>

The java.util.Properties class is meant to represent a map where the keys and values are both Strings. This is because Properties objects are used to read .properties files, which are text files. So, why in Java 5 did they retrofit this class to implement Map<Object,Object> and not Map<String,String>? The javadoc states: Because P...

Generic partial view: how to set a generic class as model?

I'm trying to build a generic grid view in an ASP.NET MVC application. Let me explain with some code: public interface ITrustGrid<T> { IPagedList<T> Elements { get; set; } IList<IColumn<T>> Columns { get; set; } IList<string> Headers { get; } } This is an interface of a class that allows me to set columns and expressions ...

Higher-kinded generics in Java

Suppose I have the following class: public class FixExpr { Expr<FixExpr> in; } Now I want to introduce a generic argument, abstracting over the use of Expr: public class Fix<F> { F<Fix<F>> in; } But Eclipse doesn't like this: The type F is not generic; it cannot be parametrized with arguments <Fix<F>> Is this possible at ...

Java Generics and numbers

In an attempt to see if I can clean up some of my math code, mostly matrix stuff, I am trying to use some Java Generics. I have the following method: private <T> T[][] zeroMatrix(int row, int col) { T[][] retVal = (T[][])new Object[row][col]; for(int i = row; i < row; i++) { for(int j = col; j < col; j++) { r...

What are Reified Generics, how do they solve the Type Erasure problem and why can't they be added without major changes?

I've read Neal Gafter's blog on the subject and am still unclear on a number of points. Why is it not possible to create a implementations of the collections api that preserve type information given the current state of Java, the JVM and existing collections API? Couldn't these be them replace the existing implementations in a future ve...

Generics confusion

Below is a question from Kathy & Bert Bates SCJP 5 preparation CD. I have posted this elsewhere too, but have not gotten a satisfactory explanation until now.... Please help me understand this: public class BackLister { //Insert code here { List<T> output=new LinkedList<T>(); for(T t:input) output.add(0,t); ...

C# Linq-SQL: An UpdateByID method for the Repository Pattern

I have implemented a sort of Repository class and it has has GetByID, DeleteByID methods and so on, but I'm having trouble implementing the UpdateByID method. I did something like this: public virtual void UpdateByID(int id, T entity) { var dbcontext = DB; var item = GetByID(dbcontext, id); item = entity; ...

How to convert int[] to Integer[] in Java?

I'm new to Java and very confused. I have a large dataset of length 4 int[] and I want to count the number of times that each particular combination of 4 integers occurs. This is very similar to counting word frequencies in a document. I want to create a Map<int[], double> that maps each int[] to a running count as the list is iterate...

How to use generics containing private types with Visual Studio Unit Tests

I have an issue with a class that I'm trying to test. I've declared a private enum and I'm using that in a generic dictionary in the code. This enum has no meaning outside of this class but it's used in a private method. When I generated the code the accessor is written into the generic dictionary type, but throws an invalid cast exce...

ASP.NET MVC: Partial class: Cast Model to Generic class?

I'm building a generic grid to use in ASP.NET MVC applications. I'm having troubles with making a partial view that gets a generic class passed to it. I have prepared a small sample project which demonstates what I want to do. Download it here. In the HomeController I have 2 controller actions that use my generic grid class to prepare ...

Generics c#.net

In the below code "where T : WsgTypes.RouteRestriction", can I add multiple classes so that T can be of only those few classes types which I am interested of public static T GetDetails<T>(string code) where T : WsgTypes.RouteRestriction { T details; if (typeof(T) == typeof(WsgTypes.TicketType)) { ...

Use the Subsonic.Select() ExecuteTypedList Method with String

This is more a question regarding generics than subsonic: Imagine if have the following code: List<int> result = DB.Select(Product.Columns.Id) .From<Product>() .ExecuteTypedList<int>(); That works great and returns a generic list with the ids from my Product table. But if I want to get a list of the Produc...

How to up-cast to a generic object?

I have an array of Castle windsor registration components of type IRegistration[] In this case ComponentRegistration<T> : IRegistration For each element in my array, if it can be upcast to ComponentRegistration<> I would like to upcast it back to ComponentRegistration<T> and process it. How exactly would I do this? I got as far as ...

How can I easily tell if two collections of .NET EntityObjects contain any of the same objects?

Pages have Roles. Users have Roles. A user may only view a page if he and it share one or more roles. This works: Dim Allow As Boolean = False CurrentPage.Roles.Load() For Each r As Role In CurrentPage.Roles r.Users.Load() For Each u As User In r.Users If u.Id = CurrentUser.Id Then Allow = True ...

Perform .Select() on IQueryable with List of Func<T, string> delegates.

First of all, let me apologize in case the title doesn't make sense. I'm having a hard time understanding what I'm doing, let alone being able to use the right words for describing what I'm doing. I'm building a generic grid class in which I define the columns by a header / linq expression combination: public class Column<T> { publ...

Convert Func<T, string>[] into Func<T, string[]> ?

Consider this class: public class Column<T> { public string Header { get; set; } public Func<T, string> ValueExpression { get; set; } } used like this: var columns = new List<Column<Employee>> { new Column<Employee> {Header = "Employee Id", ValueExpression = e => e.EmployeeID.ToString()}, ...

Java object to int: a better way?

I have a TreeSet, which will be full of integers. To make a long story short, I'm trying to loop starting after the last (greatest) value stored in the list. What I'm doing now to get the starting variable is: Object lastObj = primes.last(); Integer last = new Integer(lastObj.toString()); int start = 1 + last.intValue(); // ...

Action(Of T) in Visual Basic in List(Of T).ForEach

I have searched high and low for documentation on how to use this feature. While the loop I could write would be simple and take no time, I really would like to learn how to use this. Basically I have a class, say, Widget, with a Save() sub that returns nothing. So: Dim w as New Widget() w.Save() basically saves the widget. Now let'...

Type safety: Unchecked cast between Class-Objects

I am quite comfortable with generics and such, but in this special case I have a question concerning the "Type safety: Unchecked cast from .. to .." warning. Basically I have a List of Class objects and now I want to get a subset of these that implement a special interface but the resulting List should also have that special Type: ... ...

Generic ComboBox with automatically named properties

I'm writing a wrapper for the WinForms ComboBox control that will let me populate the dropdown with a List<T>, and has a Selected property that returns an item of type T (or null if nothing selected). Rather than having a Selected property, I'd like it to be named based on the generic type automatically. For example: MyDropDownList<Us...