generics

Java Generics Question

How can I implement generics in this program so I do not have to cast to String in this line: String d = (String) h.get ("Dave"); import java.util.*; public class TestHashTable { public static void main (String[] argv) { Hashtable h = new Hashtable (); // Insert a string and a key. h.put ("Ali", "Anorexic Ali")...

Newbie polymorphism question using generics

I have the following method that takes in a details object, validates it, converts it to a request and enqueues it. Everything is fine apart from the validate request which I am having trouble with. Basically, there is different validation logic for each different details object. I know from the generic constraint that the details object...

Explain how generics work in this method

I have been working on Google AdWords and came across this code adwords-api-6.4.0, com.google.api.adwords.lib.AdWordsUser public <T extends java.rmi.Remote> T getService(AdWordsService service) throws ServiceException { try { return (T) AdWordsServiceFactory.generateSerivceStub(service, this, service.getEndpointSe...

How can I see if GenericTypeDefinition implements IEnumerable<>

I have a method that checks if a type is generic and then checks if the GenericTypeDefinition is of IEnumerable<>. static Type GetEnumerableType(Type type) { if(type.IsGenericType) { var genericTypeDefinition = type.GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(IEnumerable<>)) { ...

Why are not all type information erased in Java at runtime?

My obviously wrong understanding of Java Generics was up to now, that Type Erasure removes all type information such that there is nothing left at all at runtime. Recently i stumbled upon a code fragment where i had to ask myself: How the hack does this work? Simplified it presents as import java.lang.reflect.ParameterizedType; import...

Entity Framework - Is it possible to create a table containing strings of alternative names to generic types?

I have recently been trying to learn more about the Entity Framework technologies from Microsoft. I have found a situation which I am not sure if it is possible (or correct) to solve using EF. I have lots of entities that can be referred to by lots of different names. For example, a persons occupation maybe described as "Coder", "Progra...

Code reuse through generics vs polymorphism

What is the better way to reuse implementation: inheritance or generics? The model is following: Script has Steps, Steps have Elements. Tree structure is double linked, i.e. Steps know their Script and Elements now their Step. Now, there are 2 types of Scripts: Templates and Runs, where a Run is created at first as a copy of the Templ...

C# Generic Types Cause Ambiguity

I am creating a custom generic class: class Widget< T1, T2> { ... public bool Bar( T1 type1 ) { ... } public bool Bar( T2 type2 ) { ... } ... } The following lines, of course, creates an ambiguous call compile error: Widget<int, int> Foo = new Widget<int, int>(); ... Foo.Bar(5); ... I...

Remove duplicates from a List(Of T) in VB.NET

I fail to remove duplicates from my List. What am I doing wrong? Dim Contacts As New List(Of Person) ... ' remove duplicates ' Contacts = Contacts.Distinct(New PersonEqualityComparer).ToList my equality comparer: Public Class PersonEqualityComparer Implements IEqualityComparer(Of Person) Public Function Equals1(ByVal x As ...

How can multiply-nested generic classes refer to each other?

Examine the code below public abstract class ClassA<ClassBType extends ClassB<ClassCType>,ClassCType extends ClassC> { public void method(ClassBType type) { type.myClassA = this; //Error. Type mismatch: cannot convert from TestGameMain.ClassA<ClassBType,ClassCType> to TestGameMain.ClassA<TestGameMain.ClassB<ClassCType>,Cla...

Identifying whether an object is a type of List and iterating over it

I am trying to determine if an object x is a list. It may be a list of any type and with any generic parameter. If it is, then I want to iterate over it if it is. This is the best I could come up with, but it fails due to a compilation error if(x is List){ foreach(Object o in (List)x){ ; } } How can I do this? ...

How to handle a generic dictionary whose types are unknown and don't matter?

If 'value' is an incoming generic dictionary whose types are unknown/don't matter, how do I take its entries and put them into a target dictionary of type IDictionary<object, object> ? if(type == typeof(IDictionary<,>)) { // this doesn't compile // value is passed into the method as object and must be cast IDictionar...

Alternative to being able to define static extension methods

I understand that I can't extend static classes in C#, I don't really understand the reason why, but I do understand that it can't be done. So, with that in mind, here is what I wanted to achieve: public static class GenericXmlSerialisationExtender { public static void WriteToXML<T>(this T targetObject, string fileName) { ...

what's the purpose of wraping a generic interface?

I saw in a projekt many interfaces which just wrapped a generic class. An example: public interface IAssertionViewModelMapper : IMapper<Assertion, AssertionViewModel> {} public interface ISearchPageViewModelMapper : IMapper<IList<Tag>, SearchPageViewModel> {} Could someone explain why they did this ? I mean I can just straight impl...

How can I trim a List<string> so preceding and succeeding blank lines are removed?

What is the easiest way to do this? The results should be: 1: one 2: two 3: 4: 5: five Code: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestLines8833 { class Program { static void Main(string[] args) { List<string> lines = new List<string>(); ...

Java generics hysterics

Somebody help me with these generics! If you have an overloaded method with different types which can be an argument, can one write a generic method which at runtime call the right one? (Sorry if my java-nology is poor, if so) Example: public interface CoolInterface { setOverloadedValue(String o); setOverloadedValue(Integer o);...

What is the best way to convert a string separated by return chars into a List<string>?

I need to often convert a "string block" (a string containing return characters, e.g. from a file or a TextBox) into List<string>. What is a more elegant way of doing it than the ConvertBlockToLines method below? using System; using System.Collections.Generic; using System.Linq; namespace TestConvert9922 { class Program { ...

How can I get an extension method to change the original object?

I want to be able to write extension methods so that I can say: lines.ForceSpaceGroupsToBeTabs(); instead of: lines = lines.ForceSpaceGroupsToBeTabs(); However, the following code currently outputs: ....one ........two instead of: Tone TTtwo What do I have to change in the following code to make it output: Tone TTtwo (not...

Access a static property of a generic class?!

I use a Height for all the Foos members. Like this public class Foo<T> { public static int FoosHeight; } public partial class Form1 : Form { public Form1() { InitializeComponent(); Foo<???>.FoosHeight = 50; // DO I Set "object" here? } } The same situation is in VB.NET. ...

How do I access and use generic type parameters as a regular type in C#?

I have a generic business object collection class which contains some business objects: public abstract class BusinessObjectCollection<T> : ICollection<T> where T : BusinessObject I want to write a method on my Collection class that returns the type T and a method that returns a newly-instantiated object of type T. In C++, thi...