keerthi

keerthi

  • NA
  • 4
  • 0

Reusing a method that takes class as argument

Dec 9 2007 5:19 PM
Dear friends,

Please help me with the following:

In a C# program, how can I reuse a method that takes classes (or list of classes, List<classname> ) as arguments? The method is defined inside a class.
 
Presently, the method in my program works only if "the object of the function call" and "the object defined in the parameter list of the function" are the same. Thus practically I have to repeat the method code as many times  as the function is called, each with a different class as argument.


The following shows the gravity of my situation: Supposing there are 10 such methods in my c# program and each method (which by itself is quite long and detailed) has to be performed on 10 different classes as arguments.

What I want is, the method (written as a single copy, meaning, if-else option is ruled out) should accept any suitable class passed to it, any number of times, and produce the result. I am looking for a suitable way, using for example, inheritance. Use of gettype to resolve the classname doesnot work as it produces complie errors.

Suppose if the method takes Int or Double argument types, this problem does not arise; thus, is this a fundamental problem in OOP languages? Is there a classical method (I mean, not workaround) to overcome this problem?

Anyway I would be happy to learn an efficient workaround too!

Thanks for ur replies in advance!

Greetings,
Keerthi

Answers (1)