Defining a Method Definition for an Interface
I have an interface that I am developing. Let's call it "IMyInterface". Each class that implements IMyInterface will have N steps. One of the methods in my IMyInterface will be GetData(int StepNumber)
. GetData should return an ObservableCollection, which will be a collection of some class that changes depending on where we are. For example, if the ModelA class implements iMyInterface and ModelA has 2 steps, then the call ModelAInstance.GetData(1)
might return ObservableCollection<ClassX>
. The call ModelAInstance.GetData(2)
can return ObservableCollection<ClassY>
. Etc.
For some reason, I'm having trouble declaring a method for GetData. I could use:
ObservableCollection<object> GetData(int StepID);
but that doesn't seem right to me. I think there is a better way to do this with generics, right?
source to share
Based on existing comments, if the return type is sufficient to tell the user what to expect from the call, then you should simply include that type in the call itself.
ObservableCollection<T> GetData<T>(int StepID);
In this case, it can simply be called as such:
ModelAInstance.GetData<ClassX>(1);
ModelAInstance.GetData<ClassY>(2);
In this post, I would like to know if the step number is needed more (unless, of course, it is used for other logic in the method).
Edit: clumsy. I didn't notice any additional comments associated. If this solution works, I'll refer to Mike, who provided the same answer in my comments.
source to share