Questions tagged [generic-method]

180 questions
1
vote
2 answers

Generic method inheritance

The question is about the following block of code: public class SomeClass { public static class A { public void f(int x) { System.out.println("1"); } public void f(Object x) { …
Mickey
  • 1,405
  • 2
  • 13
  • 33
1
vote
2 answers

Generic Method That takes unique parameter and returns unique parameters Java

I have a requirement where in the function takes different parameters and returns unique objects. All these functions perform the same operation. ie. public returnObject1 myfunction( paramObject1 a, int a) { returnObject1 = new…
ajroot
  • 93
  • 1
  • 1
  • 7
1
vote
3 answers

Pass Generic ObservableCollection to a method and iterate over it

I have 2 ObservableCollections, say of type class1 and class2. private ObservableCollection cOne; // collection of objects of type Class1 private ObservableCollection cTwo; // collection of objects of type Class2 Now in the below…
Jadav Bheda
  • 5,031
  • 1
  • 30
  • 28
1
vote
1 answer

Generic methods: returning double or double-like class

I'm currently implementing a simple version of algorithmic differentiation with operator overloading in C#. I'm trying to figure out how to design generic math functions that works for with ordinary doubles and my own class "ADouble" that works like…
amri
  • 77
  • 8
1
vote
2 answers

How do I AssertWasCalled a generic method with three different types using RhinoMocks?

I'm trying to learn Rhino Mocks AAA syntax, and I'm having trouble asserting a certain method (with any argument value) was called. I'm using Machine.Specifications as my testing framework. This particular method is generic and I want to make sure…
1
vote
0 answers

How to define a generic class which extends Number

In the method operator += is not defined, how to solve it? The class contains methods to find min sum, min positive sum, and max product. class Sub{ public T minSubSum(T[] a){ T minsum ; T thissum = a[0]; for(int i…
Lairai
  • 31
  • 3
1
vote
2 answers

Generic method: Contraint with Interface, how to access properties

I want to access a property of a new created object within a generic method, which is constraint by an Interface: public interface MyInterface { int ID { get; set; } string Name { get; set; } } Since the Compiler knows that "T" is of the…
Dave
  • 263
  • 3
  • 12
1
vote
3 answers

'U extends Something' VS just 'Something' in Bounded Type Parameters

Suppose I have a bounded type parameter in a generic method (an example from the The Java™ Tutorials, http://docs.oracle.com/javase/tutorial/java/generics/bounded.html): public static void inspect(U u) { } Then, I can call using…
Sung Kim
  • 8,417
  • 9
  • 34
  • 42
1
vote
1 answer

Get type of property in generic method

I have method: static T RandomObject (...) { var tmp = Activator.CreateInstance(); ... foreach (PropertyInfo info in tmp.GetType().GetProperties()){ ... } } And if class T have object properties I want to generate them random too, so…
Kazaar
  • 33
  • 6
1
vote
1 answer

generic types and inheritance confusion

I have a class hierarchy like the following: public class Country : MainObj public class MainObj : BaseEntity and I have business logic classes like the following: public class CountryBLL : CountryDLL,IBaseBLL where…
brtb
  • 2,201
  • 6
  • 34
  • 53
1
vote
1 answer

Generic Method in C++

I would like to create a method that will do the same actions for some types of argument. For example: I have a file and would like the method WriteToFile() to write string or a int or a double to the document. WriteToFile( type…
user3165438
  • 2,631
  • 7
  • 34
  • 54
1
vote
2 answers

Wildcard parameter in method

I have defined the following class: public class priorityQueue> implements Iterable It contains the following methods: public boolean Push(T Node) public T Pop() public Iterator iterator() I need to write a method…
1
vote
3 answers

Generic method returning the position of max object in a list using a iterator

Im making a generic method that can take any type of list and find the position of the max value in that list, this is part of a bigger program I have made and I have made a iterator that I use to run through the list. Im nearly finished but Im…
comodeque
  • 95
  • 1
  • 2
  • 7
1
vote
1 answer

How to get the type of a generic parameter by reflection

I need to get the generic type of an generic parameter by reflection. But the real type, and not the type { Name="T" ; FullName=null } public void Sample(T i, object o) { MethodBase basee = MethodBase.GetCurrentMethod(); Type[] types =…
1
vote
1 answer

Pass MakeGenericMethod a Dynamic Type

I'm trying to call a generic method and need to pass it a Type dynamically. But get a compile error, 'CS0246: The type or namespace name `t' could not be found. Are you missing a using directive or an assembly reference'. Please tell me what I'm…
user1229895
  • 2,259
  • 8
  • 24
  • 26