Questions tagged [generic-method]

180 questions
1
vote
1 answer

sending a generic class object to a generic method (java)

I have a class named InstanceClass as follows: package trials; public class InstanceClass { private T num; public void calculate() { System.out.printf("%s", num); } public T getNum() { return num; } …
1
vote
1 answer

Cast enum-value in generic method to int

I have created a generic method. The typeparameter could be any enum: private AdditionalFields GetAdditionalFields(params T[] fields) where T : struct, Enum { var fieldTextList = new List(); foreach ( var field in fields ) { …
BennoDual
  • 5,865
  • 15
  • 67
  • 153
1
vote
1 answer

How to use STATIC class value with LIST or LAMBDA EXPRESSION?

This is my sample code. public class Sample { public void Main() { Execute01(); Execute02(); Execute01(); Execute02(); Execute01(); …
ha2pa1
  • 25
  • 3
1
vote
1 answer

Creating a method that accepts both Vector2 and Vector3 arguments in C#

I'm starting with C# and having trouble writing a method that accepts both Vector2 and Vector3 arguments in C#. Generic methods looked like the way to go, but I can't make it work just yet. Here's what I tried: static void GetNoisePosition(ref T…
Robin
  • 21,667
  • 10
  • 62
  • 85
1
vote
2 answers

Dart: doesn't override generic method has generic parameter type similar to the return type

I have an issue to override a generic method from an abstract class. Here is my abstract class: abstract class A { String getData(Type key); } when I created a class (B) to implement class (A) as showing below: class B implements A { …
Ahmed Mehanna
  • 141
  • 3
  • 10
1
vote
3 answers

How to create a generic method that accepts only generic parameters and not raw type parameters?

I want to create a generic method that accepts only an arraylist of integers. But the method is also accepting a raw type. How can I restrict it to accept only an arraylist of integers? package generics; import java.util.ArrayList; import…
Subbu
  • 217
  • 2
  • 11
1
vote
1 answer

In generic method, why the generic overload is preferred to an implemented method with parent type?

Let's say I create a generic method, and implement some types, like shown in the code below. I also have two objects, a Foo and a Bar, where Bar inherits from Foo. I then implement the generic method for Foo. If I call the method with Bar, why…
1
vote
2 answers

Why i am getting error by setting the Integer type parameter to the Integer variable in generics?

I have the following program: class MyGenClass{ public void setAge(T ageParam){ Integer age = ageParam; } } class Program{ public static void main(String args[]){ MyGenClass gnClass = new MyGenClass(); …
1
vote
2 answers

Java - Generics - Casting generic object in generic specified object doesn't work

I'm trying to resolve this apparently simple generic casting problem : First, declaring this simple generic object : public interface GenericObject {} Second, declaring this working interface : public interface Generic { // I don't want to do…
1
vote
3 answers

Generic method return type

Does generic method return type resolve on the basis of value reference? e.g. public class TestGenericMethod { public static void main(String[] args) { TestGenericMethod dis = new TestGenericMethod(); String str =…
Arun
  • 2,360
  • 2
  • 17
  • 23
1
vote
3 answers

Java - Generics; Is it usefull to parameterize a method if you want to swap 2 Items of an object?

The following code alows to swap 2 Items of an Object : public class Box { F first; S second; //Method to swap public static void swapItems(Box box){ F temp = box.first; box.first = box.second; …
Smith
  • 25
  • 4
1
vote
0 answers

Java: Pass genric type from generic method to the new instance a generic class (inside method)

How can I instantiate a new Object of a generic class inside of a generic method, exactly with the same type that is passed (as type) to that method? (Something like passing a variable from method parameters to a class constructor.) Example: Both B…
Mir-Ismaili
  • 13,974
  • 8
  • 82
  • 100
1
vote
3 answers

Overriding a generic method on a non generic class

I know how to accomplish my goal when using a generic class. But to understand why the syntax of generic methods is allowed (to be declared virtual or abstract) in a non-generic class I can't seem to determine. The first chunk of code shows a simple…
aitee
  • 91
  • 1
  • 1
  • 9
1
vote
1 answer

Why is this generic method not giving compile-time error?

In this program I am creating a generic method in which second parameter extends first parameter but when I am passing String as first paameter and Integer array as second parameter then too the program is running fine. Why is it not giving compile…
1
vote
1 answer

generic methods: how to force using the most specialized method available

I defined a generic method Use in an interface IInterface. I tried to make an implementation of that interface where the concrete implementation of the Use method depends on the actual type T, and I want to always call the most specialized…
Kjara
  • 2,504
  • 15
  • 42