Questions tagged [generic-method]
180 questions
0
votes
2 answers
How to implement one generic method for two classes in java
I have an interface that has one ordinary method and one generic method. I have implemented ordinary method for two different classes, but do not now how to do that with generic method. Here is my code:
Sphere.java:
public class Sphere implements…

user_01
- 447
- 3
- 9
- 16
0
votes
2 answers
Implementing generic method from interface in java
I have two interfaces and one class implementing them. In the interface I have shown one usual method and one generic method. When implementing them in main method usual method shows correct result, but the generic one does not. If possible could…

John Doe
- 77
- 1
- 9
0
votes
1 answer
CLR generic method branching based on the type of the generic argument
In C#, is it generally a bad practice to use a generic argument to modify the behaviour of a generic method?
In example:
class Foo { }
class Bar { }
void GenericMethod()
{
if (typeof(T) == typeof(Foo))
{
…

Filip Minx
- 2,438
- 1
- 17
- 32
0
votes
1 answer
Generic methods in conjunction with generic containers
I currently have a class that contain a specific Collection implementation, much like the following example:
public class Bag {
Set> items;
public Set getItems() {
return (Set) items;
}
}
I now need to generalise…

Paolo Simonetto
- 21
- 4
0
votes
2 answers
abstract function, parameter type extends class
I am building a library management application in Java.
I have an abstract class called Material. It has an abstract method called equals.
There is a subclass called Newspaper and it of course implements equals, with the exact same signature as…

DavidVV
- 225
- 1
- 4
- 12
0
votes
3 answers
C# incompatibility in two T types in generic method which return T type
I am trying to return max value of a T[] array by using following method. I don't understand why compiler complains here T max = _genericList[0]; that throws error cannot implicitly convert ...\GenericList(9) to ...\GenericList(123).
Why these two…

Ivan Slaev
- 23
- 1
- 7
0
votes
3 answers
Java - type inference for array paramater of a generic method
Type inference doesn't seem to work for arrays with generic methods? I receive the error 'The method contains(T[], T) is not applicable for the arguments (int[], int)'. How should I be doing this?
method(new int[1], 0); //Error
...
public static…

Dimpl
- 935
- 1
- 10
- 24
0
votes
1 answer
C# Generic .Contains() method implementing SqlFunctions.StringConvert in Entity Framework
I have a generic method which dynamically creates a query in Entity Framework.
I use this as a search function on data table headers.
The function works perfectly if the Entity property type/SQL data type is a string. This is because of the…

Sean Thorburn
- 1,728
- 17
- 31
0
votes
1 answer
How to refactor method into generic base class implementation?
I've already implemented my Repository classs, but I'm wondering how I can refactor it's methods into a base class that can be extended by different repository types.
I started by creating the base Repository class below, but not sure how abstract…

Brian Var
- 6,029
- 25
- 114
- 212
0
votes
1 answer
Why my Java code cannot compile? [Java: Generic Methods and Bounded Type Parameters]
The following is my Java code and it just cannot compile. I cannot figure the reason for fail:
interface Comparable
{
public int compareTo(T o);
}
class MyClass {
public static > int method1(T t1, T t2)
{
…

Zander Hsu
- 19
- 1
- 4
0
votes
1 answer
What if main method is made parameterized, then how one can specify its real type?
public class TestA {
@SuppressWarnings("unchecked")
public static void main(String...args) throws InterruptedException {
Thread.currentThread().setName("--Parent--");
final InheritableThreadLocal itl = new…

Manoj
- 76
- 6
0
votes
1 answer
Java passing className + object to generic method
In Android Java I want a MyDownloadHelper to download and return JSON data. This is working in two separate files with different class/object names. However, I can't get this to work dynamically.
With the current setup, I can call…
user443346
0
votes
0 answers
c# Call generic method dynamically with reflection, which has a same method sign,non-generic version in the method table
Say I have a class like this:
public class MyTestClass
{
public void DoSomething(object o)
{
Logger.Debug("Non-generic version called.");
}
public void DoSomething(T o)
{
Logger.Debug("Generic version…

lingtianlan
- 304
- 2
- 7
0
votes
1 answer
Invalid argument when calling method via reflection
I am trying to call the Prism EventAggregator using reflection, because the event payload is determined at runtime.
Here is the normal way to use the EventAggregator:
[TestMethod]
public void…

user2145393
- 499
- 2
- 6
- 11
0
votes
1 answer
How do I return a generic type while having another generic type as parameter, both required to implement an interace?
Okay yes, the title is kind of confusing. But this is what I want to accomplish:
I want to return a list containing elements of a type C. And I want the method to receive a variable of type R. And C have to be a class implementing an interface, i.e.…

Jonatan Stenbacka
- 1,824
- 2
- 23
- 48