Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I like to know is it bossible to call Interface methods from collection. Let say I have collection of pages and I have implemented IMyPageMethods to each page of collection and now I like to call one of its implemented methods.

C#
public ObservableCollection<page> PageCollection = new ObservableCollection<page>();

// ..add pages with IMyPageMethods..

//call specific page method somewhere in code

PageCollection[SomeIndex].MyInterfaceMethod(); // <-- how to do this? intellisense does not offer my interface methods?
</page></page>


Hope you got idea :)

Cheers!

Edit: fixed typos
Posted
Updated 5-Aug-11 0:22am
v2
Comments
BobJanova 5-Aug-11 10:43am    
If Page implements IMyPageMethods then you should be able to call it on PageCollection[index], since that will be of type Page. So I don't understand the question.

You cannot directly.

What you can do is create your own ObservableCollection by implementing your local interface in this customized collection.
You can then use the code you have written above.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Aug-11 6:36am    
ObservableCollection is a good idea, my 5. I suggested my article DynamicMethodDispatcher as an alternative, please see my solution. Did you see this article before? -- I'm not sure, many people saw it.
--SA
Abhinav S 5-Aug-11 8:14am    
Thanks.

No I did not see that article before.
I can offer your a good robust alternative: my DynamicMethodDispatcher which dispatches a delegate call by key. This is quite powerful and effective approach. You will get full source code and usage samples. Please see my article Dynamic Method Dispatcher[^].

—SA
 
Share this answer
 
Comments
Abhinav S 5-Aug-11 8:13am    
Yup. Ny 5.
Sergey Alexandrovich Kryukov 5-Aug-11 8:19am    
Thank you, Abhinav. And thank you for looking at the article and your votes.
--SA
You can't 'call' Interface methods since they don't implement anything.
That's why it's called implementing an interface.

If a class Employee implements an Interface IPerson, then Employee will have to implement all the functions defined in IPerson, don't know it it makes sense though.

You should read a little about Interfaces:
http://msdn.microsoft.com/en-us/library/ms173156.aspx[^]

Hope it helps
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Aug-11 6:40am    
You are in principle right of course, but why do you think that OP really meant to "call" non-implemented interface methods? A collection item could store an interface reference which was obtained from the implementing class, so it could be called.

Anyway, I suggested my article DynamicMethodDispatcher as an alternative, please see my solution. Did you see this article before? I think this is really interesting.
--SA
_Zorro_ 5-Aug-11 8:30am    
I suppose I didn't got that question right then.
I didn't read your article but I'm printing it right now, it'll be a good lecture for tonight in the subway. Thanks.

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