Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a method definition for an interface

I have an interface that I'm designing. Let's call it, "IMyInterface". Each class that implements IMyInterface is going to have N steps. One of the methods in my in IMyInterface is going to be GetData(int StepNumber). GetData needs to return an ObservableCollection which will be a collection of some class that changes depending on the step we're on. For example, if class ModelA implements iMyInterface and ModelA has 2 steps, then calling ModelAInstance.GetData(1) might return an ObservableCollection<ClassX>. Calling ModelAInstance.GetData(2) might return ObservableCollection<ClassY>. Etc.

For some reason, I'm having trouble coming up with the method declaration for GetData. I could use:

ObservableCollection<object> GetData(int StepID);

but that doesn't seem right to me. I think there's a better way to do this with generics, right?

like image 955
DeadZone Avatar asked Nov 27 '25 17:11

DeadZone


1 Answers

Based on the existing comments, if the return type is enough to specify to the user what to expect back from a call, then you should just be able to include that type in the call itself.

ObservableCollection<T> GetData<T>(int StepID);

In which case it could simply be called as such:

ModelAInstance.GetData<ClassX>(1);
ModelAInstance.GetData<ClassY>(2);

On that note, I would wonder if the step number is necessary any longer (unless of course it is used for other logic within the method).

Edit: Clumsy me. I didn't notice the linked extra comments. Should this solution work, I defer to Mike who provided the same answer in comments before me.

like image 188
OhBeWise Avatar answered Nov 29 '25 08:11

OhBeWise



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!