generics

WSDL not interpreting generic properly

Hello All, I am designing a framework for the use of error codes in an already very developed application. There are several web services that handle database interaction and then there is the UI. If an error occurs in the web service the error code will need to be passed to the UI and processed (for showing user friendly error messag...

Java generics Pair<String, String> stored in HashMap not retrieving key->value properly

Here's Pair.java import java.lang.*; import java.util.*; public class Pair<TYPEA, TYPEB> implements Comparable< Pair<TYPEA, TYPEB> > { protected final TYPEA Key_; protected final TYPEB Value_; public Pair(TYPEA key, TYPEB value) { Key_ = key; Value_ = value; } public TYPEA getKey() { return Key_; } public...

C# Generics question

I am a bit rusty on generics, trying to do the following, but the compiler complains: protected List<T> PopulateCollection(DataTable dt) where T: BusinessBase { List<T> lst = new List<T>(); foreach (DataRow dr in dt.Rows) { T t = new T(dr); lst.Add(t); } return lst; } So as you can see, i am trying ...

How to convert a type to a generic version given its type?

I'm having a spot of trouble with generics in C#. I have to store a number of generic objects together but their type parameter differs so I have made a non-generic interface which they implement. What I'm looking for is a way to convert back to the generic version, given a type object. I know I can do it with reflection but I was wonder...

Convert List<T> to object[]

I am looking for a one liner that transforms List<T> into object[]. It's one liner, so I am not interesting in solutions such as foreach, or for... Any takers? Hint: No, both List<T>.ToArray() and List<T>.ToArray<object>() don't work. Edit: Why List<T>.ToArray<object>() doesn't work? Because it can't compile. ...

Confused by Java generics requiring a cast

I'm confused by the following code: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class GenericsTest<T extends List> { public void foo() { T var = (T) new LinkedList(); } public static void main(String[] args) { GenericsTest<ArrayList> gt1 = new GenericsTest<ArrayList>(); ...

How can i use generic in an implicit operator?

I have a c++ class that is very simple struct Pt_t { T x, y; template <class T2> operator Pt_t<T2>() { Pt_t<T2> pt = {x, y}; return pt; } }; That allows me to create a pt that has T as any type i want. I can also do Pt_t<s8> = Pt_t<u64>; without a problem. How do i do the same in C#? i tried the below and got an error cla...

How do I overload operator for a generic class?

It seems to me that it is not only my problem. Please do not close my question as a duplicate because I looked these questions through and I did not find the solution. class Matrix<T> { private Int32 rows; private Int32 cols; private T[,] matrix; public Matrix() { rows = cols = 0; ...

How to search StackOverflow for string containing < and > characters?

Is it possible to search StackOverflow for a string containing < and > characters? I'm trying to find questions relating to Where<T> but get told there are no results for WhereT. This makes it difficult to find information on questions relating to C# generics. ...

Generic way to find or create an object in Linq to Sql?

Quite often in my Linq to Sql code, I need to "find or create" an entity as such: var invoiceDb = ctx.Invoices.FirstOrDefault(a => a.InvoicerId == InvoicerId && a.Number == invoiceNumber); if (invoiceDb == null) { invoiceDb = new Invoice(); invoiceDb.Number = invoiceNumber; ct...

C#: Using new in a Func<T>

I was wondering, after having read this question... he has this code: public static T FindOrCreate<T>(this Table<T> table, Func<T, bool> find) where T : new() { T val = table.FirstOrDefault(find); if (val == null) { val = new T(); table.InsertOnSubmit(val); } return val; } Would it be possible ...

C# - Reflection using Generics: Problem with Nested collections of ILists

Hi Everyone, I would like to be able to print object properties, and I've hit a snag when I hit nested collections of the iLists. foreach (PropertyInformation p in properties) { //Ensure IList type, then perform recursive call if (p.PropertyType.IsGenericType) { ...

Can you pass an int array to a generic method in java?

I'm playing around with some code katas and trying to get a better understanding of java generics at the same time. I've got this little method that prints arrays like I like to see them and I have a couple of helper methods which accept an array of 'things' and an index and returns the array of the 'things' above or below the index (it'...

Where do you specify Generics type?

I have a generic Print method that iterates over a list and simply prints each item's file name private static void Print<T>( Func<IEnumerable<T>> getFiles, Func<T, string> getFileName) where T : class { foreach (T file in getFiles()) { var fileName = getFileName(file); ...

Using reflection to address a Linqed property

I'm trying to writing a generic method that will load a record of a specific type, with a specific ID. Here's one way that works: public abstract class LinqedTable<T> where T : LinqableTable { public static T Get(long ID) { DataContext context = LinqUtils.GetDataContext<T>(); var q = from obj in context.GetTable<T>() whe...

How to find duplicates in a List<T> quickly, and update the original collection

Let me start by saying I've read these questions: 1 & 2, and I understand that I can write the code to find duplicates in my List, but my problem is I want to update the original list not just query and print the duplicates. I know I can't update the collection the query returns as it's not a view, it's an anonymous type IEnumerable<T>....

Create List<int> with values at compile time.

It is possible to create an array at compile time like; int[] myValues = new int[] { 1, 2, 3 } ; But I would like to do something like this; List<int> myValues = new List<int>() { 1, 2, 3 }; The compiler says No. Is there a way to do this (C# 2.0) without using LINQ (C# 3.0)? ...

Pointer to generic type

In the process of transforming a given efficient pointer-based hash map implementation into a generic hash map implementation, I stumbled across the following problem: I have a class representing a hash node (the hash map implementation uses a binary tree) THashNode <KEY_TYPE, VALUE_TYPE> = class public Key : KEY_TYPE; Value ...

Derive from specialized generic types

Is it possible to derive a class from a specialized generic type: TGenericBase <T> = class // ... end; TSpecializedDerived = class (TGenericBase <String>) // ... end; Just wondering if this is possible at all... EDIT Code works fine when I put it in a new project. Must be due to some other mistake; sorry about that ...

Parameter type using Generics constraint VS Explicit type declaration

What would be the difference between these two seemingly similar declarations? When would you choose one syntax over another? Is there any specific reason to choose one over the other? Any performance penalties incurred for either case? public void Import<T>( Func<IEnumerable<T>> getFiles, Action<T> import) where T : IFileI...