generics

Problem with Java Generics, type parameter, and returned list

Hello! This code is simplified as much as I can from a more complex class structure. In the real code, there were sub-types of the Integer and Double types I use here. I'm trying to use Java Generics with a type parameter. If the user requests the type of Number.class, we want to combine the List<Integer> list and the List<Double> list...

foreach struct weird compile error in C#

namespace MyNamespace { public struct MyStruct { public string MyString; public int MyInt; public bool MyBool; } public class MyClass { private List<MyStruct> MyPrivateVariable; public List<MyStruct> MyVariable { get { if (MyPriv...

How do I setup multiple ORed type bounds in Scala

Is it possible to do something like this in Scala: class MyTest { def foo[A <: String _or_ A <: Int](p:List[A]) = {} } That is, the type A could be a String or Int. Is this possible? (Similar question here) ...

How to get generic method on closed generic type, having open MethodInfo from open generic type?

Imagine type like this (C#): public interface IAmGeneric<T> { void SoAmI<T1>(T one, T1 two); } Given I have open generic MethodInfo from open generic version of the type (IAmGeneric<>.SoAmI<>()) and the following array new[] { typeof(int), typeof(string) }' I'm looking for well performing and reliable way of getting closed versi...

runtime casting in C#?

I'm reading data from a custom data format that conceptually stores data in a table. Each column can have a distinct type. The types are specific to the file format and map to C# types. I have a Column type that encapsulates the idea of a column, with generic parameter T indicating the C# type that is in the column. The Column.FormatTyp...

Injecting a generic factory in Guice

The following code is an example of a factory that produces a Bar<T> given a Foo<T>. The factory doesn't care what T is: for any type T, it can make a Bar<T> from a Foo<T>. import com.google.inject.*; import com.google.inject.assistedinject.*; class Foo<T> { public void flip(T x) { System.out.println("flip: " + x); } } interface Ba...

How to cast anonymous type to specified cast to get all its properties

This code is returning me a list that I am trying to pass to another function. _ucItem.lblItemName.Text = itemRow.Field<string>("ITEM_NAME").ToString(); _ucItem.pbxItemImg.Image = itemRow.Field<string>("IMAGE").ToString(); _ucItem.lblItemName.Text = itemRow.Field<string>("FK_ITEM_ID").ToString(); Here I want to replace this set of lin...

C# Generic Constraints on function

I want to write a generic function that has a constraint on the type. Specifically I want something like this: bool IsInList<T>(T value, params T[] args) { bool found = false; foreach(var arg in args) { if(arg == value) { found = true; break; } } return found; } The ...

Help needed with container for a generic item in C++

I wonder if it is possible to define a generic C++ container that stores items as follows: template <typename T> class Item{ typename T value; } I am aware that the declaration needs the definition of the item type such as: std::vector<Item <int> > items; Is there any pattern design or wrapper that may solve this issue? ...

Java: Casting from List<B> to List<A> when B implements A?

I have the following class & interface defined: public interface A { } public class B implements A { } I have a List of B objects that I need to cast to a List of A objects: List<B> listB = new List<B>(); listB.add(new B()); // dummy data listB.add(new B()); // dummy data listB.add(new B()); // dummy data List<A> listA = (List<A>...

Multi-value dictionary

I need to create a dictionary that has 2 values per key, it must return one of the 2 values with the same probability. Example: myDicry { key = "A", value1=15, value2=56; } int firstCall = myDicry["A"]; // = 15 int secondCall = myDicry["A"]; // = 56 ...

Simple Java generics question

I want to have a method which calculates the mean of a LinkedList of type Integer, Double and Float. The problem is the sum += i; statement, since java says that the + operator isn't defined for type Object. I could do a cast, but if the LinkedList was of type Float, for example, and the cast was to Integer, I would be not computing ...

LinkedList implementation in Java with generics and enhanced for

Hi. I need you to review my implementation of a Singly Linked List (SLL) please. The implementation should use generics and be able to use the enhanced for. The problem is that, when I do for (Number n : list) being list a MyLinkedList<Integer> or MyLinkedList<Double>, I get the error: "Type mismatch: cannot convert from element type O...

Is there a way to apply c# generic constraints severally rather than jointly?

Is there a way to apply several different csharp generic constraints to the same type where the test is OR rather than AND? I have an extension method I want to apply to a subset of an interface, but there is no common interface or base class that only captures the classes I wish to target. In the example below, I could write multiple ...

Is my class a subclass of other generic class?

I have an abstract generic class. public abstract class FieldHandlerWithData<DataType extends Parcelable> extends FieldHandler Now I have an object c Class<? extends FieldHandler> c = getHandlerClass(type); and now I want to test if c inherits FieldHandlerWithData (directly or indirectly). How to determine whether c inherits F...

How to get rid of generics warnings in code generated from xmlbeans-maven-plugin?

I have some code generated from xsd files by xmlbeans-maven-plugin. Unfortunately generated code uses raw collection types, like: java.util.List targetList = new java.util.ArrayList(); get_store().find_all_element_users(CURRENCY$0, targetList); Currency[] result = new Currency[targetList.size()]; targetList.toArray(result); which caus...

Hibernate and generics

In Java I have a class tha has a payload of type T public class GenericStatus<T> { private MyDateRange myDateRange; private T payload; At runtime T can be either a simple primitive Integer or a class called Price where Price is a class with 2 integers public class Price implements Serializable { private int adult; priva...

Using generics in Android Java code

Hi folks, I'm a newbie in Java so I'm not sure if this is possible. Basically I need to de-serialise a file into an object of a given type. Basically the method will do this: FileInputStream fis = new FileInputStream(filename); ObjectInputStream in = new ObjectInputStream(fis); MyClass newObject = (MyClass)in.readObject(); ...

Converting Null to Nullable Enum (Generic)

Hi Everyone, I'm writing some Enum functionality, and have the following: public static T ConvertStringToEnumValue<T>(string valueToConvert, bool isCaseSensitive) { if (String.IsNullOrWhiteSpace(valueToConvert)) return (T)typeof(T).TypeInitializer.Invoke(null); valueToConvert = valueToConvert.Replace(" ", ""); ...

WPF UserControl with generic code-behind

I have this code behind: CustomUserControl.xaml.cs namespace MyProject { public partial class CustomUserControl<T> : UserControl { ... } } and this xaml: CustomUserControl.xaml <UserControl x:Class="MyProject.CustomUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x...