generics

VB.NET generic function to return a control

Hi All, I have created a couple of simple functions in VB.NET that simply return a control of a certain type that I already know, such as HtmlInputHidden, Label, etc. That means that each of the functions is created only for that special purpose. What I'd like to do is combine all those functions into one function using generics. The...

Retrieving the Class object of the generic argument

I am not sure if this is a duplicate question to this one mainly because I'm a bit new to generics, so please be forgiving. I have a generic class: public class MyGeneric<T extends Collection> { private Class<T> genericType; public MyGeneric() { // Missing code here } } My question is this: how do I initializ...

What is the difference between these two Unit Test Assertions?

Hi Guys, Came across the following MS Unit Test: [TestMethod] public void PersonRepository_AddressCountForSinglePerson_IsNotEqualToZero() { // Arrange. Person person; // Act. person = personRepository.FindSingle(1); // Assert. Assert.AreNotEqual<int>(person.Addresses.Count, 0); } I have never seen the use of gene...

Are there drawbacks to creating a class that encapsulates Generic Collection?

A part of my (C# 3.0 .NET 3.5) application requires several lists of strings to be maintained. I declare them, unsurprisingly, as List<string> and everything works, which is nice. The strings in these Lists are actually (and always) Fund IDs. I'm wondering if it might be more intention-revealing to be more explicit, e.g.: public class ...

How to get the generic type at runtime?

This is my code: The ExecutorImp extends AbstractExecutor which extract the same execute logics of its implementers(ExecutorImp is one case),when calling the execute() method of ExecutorImp, it will call the method in its supertype,but the supertype (the AbstractExcutor) should know another class binding to the implementer(in the example...

How to implement generics using JPA/hibernate EntityManager and EJB3.0 ?

I have an slsb holding my business logic, how do I use generics to change the following three methods into one generic method ? The first two are the same db, the third is a different database. Also do the methods require further annotation in relation to transaction ? @PersistenceContext(unitName = "db") private EntityManager myEntity...

Java: generics inheritence confusion

Hey, Imagine we have following classes: public interface MyInterface<T> { List<T> getList(T t); } abstract class BaseClass<T extends Number> implements MyInterface<T> { @Override public List<T> getList(Number t) { return null; } } class ChildClass extends BaseClass<Integer> { @Override public List<Inte...

Is there a technique to differentiate class behavior on generic types?

I'd like to do something like the following, but because T is essentially just a System.Object this won't work. I know T can be constrained by an interface, but that isn't an option. public class Vborr<T> where T : struct { public Vborr() { public T Next() { if ( typeof( T ) == typeof( Double ) ) { ...

Java Reference assignment with generic lists

I feel stupid asking this but I am. The line List<HasId> ids = list is giving a compile error in the following code: public class MyGarbageClass { public void myMethod(List<MyCompany> list){ List<HasId> ids = list; } interface HasId { int getId(); } class MyCompany implements HasId{ private...

Generic visitor pattern in java

Is the following java implementation of the visitor pattern using generics, general enough to be useful? (I suppose it is). Could it be improved in some way? It's important to be easily call-able using anonymous classes. Thanks. (Example of use): Vector<Number> numbers = new Vector<Number>(); numbers.add(new Double(1.2)); ...

Why is implementing a generic interface so counter intuitive?

interface Foo<T extends Number>{ } class Bar<T extends Number> implements Foo<T>{ } Why does the class have to be written that way instead of: class Bar<T extends Number> implements Foo<T extends Number>{ } Surely the second way is clearer. ...

How to use different methods with generics

public class MyGenerics<T extends Number>{ List<T> = new ArrayList<T>(); public void addToList(){ for (ListIterator<T> it; it.hasNext(); ){ it.next(); it.add(number()); } } public T number(){ return (T) 5*Math.pi; } } As far as I can tell, there is no way to tell the compiler "I promise...

generic problem

HI: What is the generic type in java? Just for reduce the class cast exception? ALso, the K means "keys", V means "valuse", what about the E and T? For a generic class,for example: List, I understand it as that the list can only put object of String type,but I am not quite clear about the generic method: static <T> void fromArrayToCol...

Cannot use polymorphism because of generic base class

I create base generic class with no fields with just one method public class Base<T> where T:class { public static T Create() { // create T somehow } } public class Derived1 : Base<Derived1> { } public class Derived2 : Base<Derived2> { } public class Program { bool SomeFunction() { // Here I need reference to...

Java generic usage question

Say I want to do something like public class Container<C extends Member> { public void foo() { C newC = new C(); } } I realize that this will not work, but what is the correct Java idiom for this? ...

Issue with interfaces in Java

I'm working through an exercise sheet regarding interfaces, generics and abstract classes in Java. No matter what way I seem to code it, the class Exercise1 won't work. The question asked are commented in the code. Any help would be appreciated, I'm not sure if the error is in the Exercise one code or the implementation of the interface ...

Java generics versus C++ templates

I think I can sum up the use of generics in Java in one word: type-safety. Can you conclude the use of templates in C++ in one word, please? ...

Setting EntitySet<t> properties to default using reflection

I am trying to write generic code for detaching a linq class. What I have currently is: public void Detach() { this.PropertyChanged = null; this.PropertyChanging = null; this.Categories = default(EntitySet<Categories>); this.Jobs = default(EntitySet<Jobs>); this.Tasks= default(EntitySet<Tasks>); } This is all fine...

Code Contracts: ContractClassFor when dealing with a generic abstract class?

So, I have a little problem here. Suppose I have: public class Repository<TEntity> where TEntity : class { public abstract void Add(TEntity entity); // ...and so on... } And now I want to define a contract class, like so: public class RepositoryContracts<TEntity> : Repository<TEntity> where TEntity : class { pub...

Why overloading does not occur?

I have the following class: class CrmToRealTypeConverter : IConverter { #region IConverter Members public object Convert<T>(T obj) { return Convert(obj); } #endregion private DateTime? Convert(CrmDateTime obj) { return obj.IsNull == false ? (DateTime?)obj.UserTime : null; } priva...