generics

How to solve this Java type safety warning? (Struts2)

Map session = ActionContext.getContext().getSession(); session.put("user", user); This code generates a warning: Type safety: The method put(Object, Object) belongs to the raw type Map. References to generic type Map should be parameterized. Map<String, Serializable> session = (Map<String, Serializable>)ActionContext.getContext().getS...

What's the reason I can't create generic array types in Java?

What's the reason Java doesn't allow us to do private T[] elements = new T[initialCapacity]; ? I could understand .NET didn't allow us to do that, as in .NET you have value types that at run-time can have different sizes, but in Java all kinds of T will be object references, thus having the same size(correct me if I'm wrong). What i...

A c# Generics question involving Controllers and Repositories

I have a base repository class which contains all the common repository methods (as generic): public abstract class BaseRepository<T, IdType> : IBaseRepository<T, IdType> My repositories from this base e.g.: public class UserRepository : BaseRepository<User, int>, IUserRepository I also have a base controller class containing comm...

Need help make these classes use Visitor Pattern and generics

Hi. I need help to generify and implement the visitor pattern. We are using tons of instanceof and it is a pain. I am sure it can be modified, but I am not sure how to do it. Basically we have an interface ProcessData public interface ProcessData { public setDelegate(Object delegate); public Object getDelegate(); //I am sure the...

How to find the class object of Java generic type?

Assume I have a generic type P which is an Enum, that is <P extends Enum<P>>, and I want to get the Enum value from a string, for example: String foo = "foo"; P fooEnum = Enum.valueOf(P.class, foo); This will get a compile error because P.class is invalid. So what can I do in order to make the above code work? ...

Why Is the sender type null when dealing with events

From C# Via CLR: Note A lot of people wonder why the event pattern requires the sender parameter to always be of type Object After all, since the MailManager will be the only type raising an event with a NewMail EventArgs object, it makes more sense for the callback method to be prototyped like this: void MethodNa...

Getting class Type information for Elements on a collection

I would like to get gain access to the type of Object held in a Collection. Below is a simplified example of when and why I might want to do this. Is this even possible? List<Address> addressList = new LinkedList<Address>(); Main.addElement(addressList); Class Main{ public void addElement(Object inArgument){ List<Object> ar...

C# Generic Static Constructor

Will a static constructor on a generic class be run for every type you pass into the generic parameter such as this: class SomeGenericClass<T> { static List<T> _someList; static SomeGenericClass() { _someList = new List<T>(); } } Are there any draw backs to using this approach? ...

Generics syntax: classes versus primitive data types

Why does this one does not work: ArrayList<LinkedList<int>> where this one does: ArrayList<LinkedList<Integer>> ??? ...

Can i use a generic implicit or explicit operator? C#

How do i change the following statement so it accepts any type instead of long? Now here is the catch, if there is no constructor i dont want it compiling. So if theres a constructor for string, long and double but no bool how do i have this one line work for all of these support types? ATM i just copied pasted it but i wouldnt like doi...

Compile time error: cannot convert from specific type to a generic type

I get a compile time error with the following relevant code snippet at the line that calls NotifyObservers in the if construct. public class ExternalSystem<TEmployee, TEventArgs> : ISubject<TEventArgs> where TEmployee : Employee where TEventArgs : EmployeeEventArgs { protected List<IObserver<TEventArgs>> _observers = null; ...

Generic Class VB.NET

hi guys, I am stuck with a problem about generic classes. I am confused how I call the constructor with parameters. My interface: Public Interface IDBObject Sub [Get](ByRef DataRow As DataRow) Property UIN() As Integer End Interface My Child Class: Public Class User Implements IDBObject Public Sub [Get](ByRef DataR...

Deserialize Stream to List<T> or any other type

Attempting to deserialize a stream to List<T> (or any other type) and am failing with the error: The type arguments for method Foo.Deserialize<T>(System.IO.Stream) cannot be inferred from the usage. Try specifying the type arguments explicitly. This fails: public static T Deserialize<T>(this Stream stream) { BinaryFormatter bi...

.NET generic class instance - passing a variable data type

As the title suggests, I'm tyring to pass a variable data type to a template class. Something like this: frmExample = New LookupForm(Of Models.MyClass) 'Works fine Dim SelectedType As Type = InstanceOfMyClass.GetType() 'Works fine frmExample = New LookupForm(Of SelectedType) 'Ba-bow! frmExample = New LookupForm(Of InstanceOfMyClass.Get...

How to create a complete generic TreeView like data structure

I want to create a completely generic treeview like structure. some thing like this: public class TreeView<T, K, L> { public T source; public K parent; public List<L> children; } as you can see in this class source, parent and also the children, all have a different generic data type. also i want my tree view to have unlim...

Need help in c# code

I have a function protected void bindCurrencies(DropDownList drp) { drp.DataSource = dtCurrencies; drp.DataTextField = "CurrencyName"; drp.DataValueField = "CurrencyID"; drp.DataBind(); drp.Items.Insert(0, new ListItem("Please Select")); } I am binding a dropdown list using this. But s...

Calling a generic function in VB.NET / C#

Question: I want to call a generic function, defined as: Public Shared Function DeserializeFromXML(Of T)(Optional ByRef strFileNameAndPath As String = Nothing) As T Now when I call it, I wanted to do it with any of the variants below: Dim x As New XMLserialization.cConfiguration x = XMLserialization.XMLserializeLDAPconfig.Dese...

Using 'or' in Java Generics declaration

I have a method that returns an instance of Map<String, List<Foo>> x(); and another method that returns an instance of Map<String, Collection<Foo>> y(); Now if I want to dynamically add one of this Maps in my field, how can I write the generics for it to work? ie: public class Bar { private Map<String, ? extends Collection<...

Where's IEnumerable<T>.OrderBy declared?

Sorry for being lazy but where's IEnumerable.OrderBy declared. I know it's an extension method but where, in which class is it declared? PS: Opening reflector... ...

Best way to translate from IDictionary to a generic IDictionary

I've got an IDictionary field that I would like to expose via a property of type IDictionary<string, dynamic> the conversion is surprisingly difficult since I have no idea what I can .Cast<>() the IDictionary to. Best I've got: IDictionary properties; protected virtual IDictionary<string, dynamic> Properties { get { return _...