Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I have Candidate, Certificates and ExamSession as class.

And i'm getting these in a list, IEnumerable<candidate> for list of candidates,
IEnumerable<ceritificates> for list of certificates and IEnumerable<examsessions> for list of ExamSession.

What I have tried:

Now, my question is:

I wrote a method, in which, i want to pass IEnumerable<> as a Parameter. Here I don't which object I will call(Candidate/Certificate/ExamSession).

Can any one please help me, how to pass IEnumerable as a parameter in a given method...


Thanks
Posted
Updated 4-Mar-17 22:48pm
Comments
Graeme_Grant 5-Mar-17 4:30am    
Post your code so that we can see...
Graeme_Grant 5-Mar-17 4:34am    
By post your code, I mean click on Improve question

Unclear what you are asking for but here are 2 examples...

1. Standard
C#
void FuntionName(IEnummerable<candidate> Data)
{
    // do stuff with data...
}
2. Generic
C#
void FuntionName<T>(IEnummerable<T> Data)
{
    // do stuff with data...
}
Pretty simple - programming 101...
 
Share this answer
 
Comments
abdul subhan mohammed 5-Mar-17 4:54am    
thanks
Unless the three classes share a base class, you can't pass a collection of them to a method, except as a collection of object instances, any more than you could write a method that would take a single instance of any of the three classes, unless there is a direct conversion from A to B and vice versa.

So you could pass a collections of objects:
C#
List<MyClass> obs = new List<MyClass>();
...
MyMethod(obs);
...
public void MyMethod(IEnumerable<object> collection)
   {
   ...
   }
But that's a very poor design.
And it's unlikely that there will be a good reason for the three classes you mention to share a common base, so it's not likely that there would be a good design reason to wrote a single method to take a collection of all three classes, as pretty much the first thing it would have to do is find out what it had been passed so it could cast the objects appropriately to use them.
A better - but not much - approach would be to create four overloads:
public void MyMethod(IEnumerable<Candidate> c) { MyMethod(c, null, null); }
public void MyMethod(IEnumerable<Certificates> c) { MyMethod(null, c, null); }
public void MyMethod(IEnumerable<ExamSession> c) { MyMethod(null, null, c); }
public void MyMethod(IEnumerable<Candidate> cand, IEnumerable<Certificates> cert, IEnumerable<ExamSession> es)
    {
    ...
    }
As at least that way you can check more easily, and retain some strong typing.

But to be honest you would be better off rethinking this approach.
 
Share this answer
 
v2
Comments
Graeme_Grant 5-Mar-17 5:31am    
Generics work much better if the functionality is common.
OriginalGriff 5-Mar-17 5:43am    
That's why I didn't suggest a generics solution - unless there is a common base class and thus a constraint, it's as bad as passing an object collection!
Graeme_Grant 5-Mar-17 6:06am    
I don't disagree at all. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900