generics

Is there a way to generically return all of the records from a table with the Entity Framework

Basically I'd be looking to implement a method like this. IQueryAble GetQuery<T>(Entities db) or extension method Entities.GetQuery<T>() This way you could do things like this public IQueryable<T> GetAll() { return yourEntityClasses.GetQuery<T>(); } which would return a SELECT * FROM query expression and obviously from there you c...

Web Service: Specifying XML Serialization element names for generic types

I've created a web service that uses a generic type Response<TCode, TData> and so I'm ending up with elements like ResponseOfResponseCodeUserData ResponseOfResponseCodeArrayOfRightData etc. Functionally works just fine but I'm wondering if there's a way to name these particular elements? EDIT: Here's an example. [return: XmlEleme...

Nested Generics

I have asked this before but I didn't get the question right so the answer was missed. If I have a method that returns an ObservableColletion<T> how would I then use this in another generic method. Would method2<ObservableCollection<T>>(){} be the way to go. I am trying to make a generic resultEventArgs that will pass the results ...

Unable to call generic method with wildcard

I have a class defined like so: public class Test { static <T> List<Class<T>> filter(List<Class<T>> input) { // code here } public static void main(String[] args) { List<Class<? extends Throwable>> list = new ArrayList<Class<? extends Throwable>>(); filter(list); } } The filter meth...

Is it possible in C# to overload a generic cast operator in the following way?

Hi, Just wondering if there is anyway to represent the following code in C# 3.5: public struct Foo<T> { public Foo(T item) { this.Item = item; } public T Item { get; set; } public static explicit operator Foo<U> ( Foo<T> a ) where U : T { return new Foo<U>((U)a.Item) } } Thanks ...

Initialize Java Generic Array of Type Generic

So I have this general purpose HashTable class I'm developing, and I want to use it generically for any number of incoming types, and I want to also initialize the internal storage array to be an array of LinkedList's (for collision purposes), where each LinkedList is specified ahead of time (for type safety) to be of the type of the gen...

What is better to use: Dictionary<>.ForEach(pair =>) or Dictionary<>.Keys.ForEach(key =>) ?

If I have access only keys from a Dictionary<TKey, TValue> what is better to use: Dictionary<TKey, TValue>.ForEach(pair => action(pair.Key)) or Dictionary<TKey, TValue>.Keys.ForEach(key => action(key)) Which method is more 'best-practice' ? Speed in both cases I think seems to be very similar. ...

How to handle subscription to generic type's events?

I am working on a WinForms app. There are many instances where I need to display a new screen in the small viewing area, so I am using Panels. Basically, I inherit from panel, expose any properties for the information I need from the panel, anything that needs to happen to display information in the panel happens in it's own code behind....

Using CompareTo() to sort based on multiple columns

Currently I have an object implementing the IComparable interface (ASP.NET 3.5, VB). When I place several instantiated objects into a Generics list, I sort them by doing a simple someList.Sort. My CompareTo() function is this: Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo 'default is...

C# boxing enum error with generics

I don't understand what is going on here... I've got the following error: The type 'TestApp.TestVal' cannot be used as type parameter 'T' in the generic type or method 'TestApp.SomeClass<T>'. There is no boxing conversion from 'TestApp.TestVal' to 'System.IComparable<TestApp.TestVal>'. This error happens for the following code: publi...

I know the typeof(T) but the compiler doesn't. How to fix?

Hey, I'm trying to write a method like this: public static T Test<T>() { if (typeof(T)==typeof(string)) return "1241"; // do something else } but I can't seem to figure out how to pull it off. I want to return values depending on the type of T that the method was invoked with. I need to return strings, int's, custom classe...

What is the generic signature of a method that returns an instance of a given subclass?

Basically I want to create a method that has a signature like the following: public <T> T getShellTab(Class<T extends ShellTab> shellTabClass) but this isn't valid Java. I want to be able to pass a class that is a subclass of ShellTab and have an instance of that class returned. public <T> T getShellTab(Class<T> shellTabClass) wor...

How can I get a datagridview to display a list<int>/list(of integer)?

seems simple enough, I want to take a generics list of integers and display them on a datagridview. google comes back with plenty of results on displaying custom classes in a datagridview, but not a list of int. when I just submit the list as the datasource, nothing shows. I tried using dim _CheckIns as new list(of integer) _checkins....

Class object type parameterization in Java

Suppose the following object structure: class Super {} class SubA extends Super {} class SubB extends Super {} I want to be able to have a variable that will hold the class object for either of my subclasses. I feel like this should do it: Class<Super> classObj; Then, I want to be able to something like this: classObj = SubA.cla...

Java syntax question: <O> O accept(ObjectVisitorEx<O> visitor)

Naive question about Java syntax. What does <T> T accept(ObjectVisitorEx<T> visitor); mean? What would be the C# equivalent? ...

.NET XmlReader with 'FOR XML' data

Recently, in company we got some MSSQL database from some old project which we have to integrate in current solution. Database has about 100-150 stored procedures that use FOR XML AUTO clause, so that queries return complete object-graph as XML instead of rows. Quickest solution (for us in company) was to create serializable classes (w...

Inheritance - Arrays of parent class and arrays of child class - casting

I'm having trouble with inheritance. I can do what I need with single instances of the parent class and child class - as expected the methods of the parent class work fine with instances of the child class. But, I cannot see how to extend a class that uses an array of the parent class, in a way that allow me to store an array of the ...

Why is code that returns a genericized Map generating a compiler warning when assigned to a generic Map?

I have a method with this signature: protected final Map<String, Object> buildOutputMappings( AbstractDataObject ado, MDBase md) And called with this method (in a subclass): Map<String, Object> params = buildOutputMappings(ra, md); I get this compiler warning: Warning:Warning:line (136)...

Why do some languages need Boxing and Unboxing ?

Hi, This is not a question of what is boxing and unboxing, it is rather why do languages like Java and C# need that ? I am greatly familiar wtih C++, STL and Boost. In C++ I could write something like this very easily, std::vector<double> dummy; I have some experience with Java, but I was really surprised because I had to write som...

Why are generics completely disabled when you ignore a parameter type?

As a followup to this question, first the background Given a class with this declaration: public class SomeClass<T> And a subclass that does not use the generic parameter: public class SomeSubClass extends SomeClass A method on SomeClass declared as follows: protected Map<String, Object> getMap(Object param) { } If the subclass...