Hi ,
I have a generic list of string:
List listOfString = new List();
I then add 4 strings to this list:
listOfString .Add("test1");
listOfString .Add("test2");
listOfString .Add("test3");
listOfString .Add("test4");
I want to check check a string variable if it contains any element within my string a...
In C++, I understand that it is possible to create a Singleton base class using templates. It is described in the book, Modern C++ Design, by Andrei Alexandrescu.
Can something like this be achieved in Java?
...
When you have a method like:
public static T DoSomething<T> ( params T [ ] input )
C# lets you to call it without specifying the T, like:
DoClass.DoSomething ( "1", "2", "3" );
Does the compiler figure out T by what's passed to it?
Is this a good convention (to leave out T in this case)?
...
When I want to constraint the type T to be comparable, should I use:
where T : IComparable
or
where T : IComparable<T>
I can't get my head around if #2 makes sense. Anyone can explain what the difference would be?
...
Introducing some of the goodness of collection operations to our codebase without adding a new external library dependency, we are adding these methods to our utility package.
static public List<T> filter(List<T> source, Predicate<T> filter);
static <Y,T> public List<Y> transform(List<T> source, Mutator<Y,T> filter);
static public boole...
I have a Java question about generics. I declared a generic list:
List<? extends MyType> listOfMyType;
Then in some method I try instantiate and add items to that list:
listOfMyType = new ArrayList<MyType>();
listOfMyType.add(myTypeInstance);
Where myTypeInstance is just an object of type "MyType"; it won't compile. It says:
...
Hi,
I have a method where I would like to return an object instance of parameterized type T ie. Foo<T>.
The type T is instantiated within the method using GetType(), from a string element in an XML file. Since neither the class or method knows about it before it is created, I cant parameterize either.
Is there a way I can return an o...
Basically what I want, istwo public methods with slightly different return values to call the same method to do whatever work is needed. They both return the return value of the private method, however the private method will know how to return the correct value depending on the public method that called it.
Example methods:
public Map...
I'm building a generic Repository<T> class that supports Linq to SQL, and I'd like to use a factory pattern for the DataContext, because currently I have to pass the correct context to the constructor.
Does anybody know how to determine the correct DataContext type for T, where T is a Linq to Sql Table?
...
That would allow the invoke method to have the right return type. For instance:
class Method<T> {
T invoke(Object obj, Object... args);
}
...
I ran across a compilation issue today that baffled me. Consider these two container classes.
public class BaseContainer<T> : IEnumerable<T>
{
public void DoStuff(T item) { throw new NotImplementedException(); }
public IEnumerator<T> GetEnumerator() { }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnume...
The only C# generics explanations I can ever seem to locate go into the "List<T>" discussion and end there. I am looking for something a little more in-depth, specifically when dealing with <T> in method signatures -- I see some pretty wild syntax at times and am having trouble understanding why and when to use it. I feel like I could ...
As I understand them, generics are a compile time feature of Java, and parametrized type information does not exist in the compiled byte code. I have now discovered the Field#getGenericType and Method#getGenericReturnType methods, thusly shattering my world view. Please help me to piece it together.
...
I'm trying to cast a returned base object to it's specific generic type. The code below should work I think but generates an internal compiler error, is there another way to do this?
type
TPersistGeneric<T> = class
private
type
TPointer = ^T;
public
class function Init : T;
end;
class function TPersistGeneric<T>.In...
I want to write a generic class that should be casted to itself with a different generic argument.
class Base {}
class Inherited : Base {}
class MyGeneric<T> {}
// WCF Service interface
void Foo(MyGeneric<Base> b);
// somewhere else
MyGeneric<Inherited> inherited;
Foo(inherited)
I know that this could be done in C# 4.0, but this do...
How can I overload a method that takes a Generic List with different types as a parameter?
For example:
I have a two methods like so:
private static List<allocations> GetAllocationList(List<PAllocation> allocations)
{
...
}
private static List<allocations> GetAllocationList(List<NPAllocation> allocations)
{
...
}
Is there a...
In the following example i can create an object dynamically via a string; however, i have no way to get at the public methods of BASE class. i can't cast obj to a BASE because i don't know what generic will be used at design time. any suggestings on doing so at runtime would be nice.
Project A contains Class A{T,J> : BASE{T,J>
Proje...
How to encapsulate below codes (i need refoctoring)
public class mycollection
{
private DateTime tarih;
private int sira;
private int visitingcount;
public DateTime TARIH { get { return tarih; } set { tarih = value; } }
public int SIRA { get { return sira; } set { sira = value; } }
pu...
So I have a map:
Map<String, Class> format = new HashMap<String, Class>();
And I would add elements to it like this:
format.put("Vendor Number", Integer.class);
format.put("Vendor Dispatch", Date.class);
....
I have a generic method as follows:
public static <T> T verifyType(String name, Class<T> type) {
if (type == Integer.cla...
class Test1<T> {
Test1(Class<T> type) {}
}
class Test2 extends Test1<Class> {
Test2() {
super(Class.class);
}
}
This works, but, I am warned about use of a raw generic type in "Test1<Class>". I understand that, but, changing it to "Class<?>" (which should be equivalent?) gives a compiler error in the call to super() -- Class...