5

I am using a generic class called ViewModelCollection<BaseViewModel> which handles a lists of ViewModels and delivers standard add() and delete() commands.

Now I'm wondering if I can "extend" this class using the partial construct for a certain ViewModel, whose name is, say, CarViewModel.

Is something like this possible?

partial class ViewModelCollection<BaseViewModel>
{
    ... some command and list stuff ...
}

partial class ViewModelCollection<CarViewModel>
{
    ... special commands for car view model
}
peterh
  • 11,875
  • 18
  • 85
  • 108
Michael Hilus
  • 1,647
  • 2
  • 21
  • 42

3 Answers3

12

No, you can't, partial just splits the class definition over multiple files, the definition has to be the same. You need to derive from ViewModelCollection<T>:

public class ViewModelCollection<T> where T: BaseViewModel
{
   //methods
}

public class CarViewModelCollection : ViewModelCollection<CarVieModel>
{
  //specific methods
}
Femaref
  • 60,705
  • 7
  • 138
  • 176
  • Thanks for your answer. I hoped I wouldn't have to use derivation. The view model part in my application is generated so now I have to make some changes on the generators before going the derivation way. :-( – Michael Hilus Apr 17 '11 at 13:52
1

partial is used only to split a class across multiple source files. The class definition itself must be the same.

Charlie Salts
  • 13,109
  • 7
  • 49
  • 78
1

Take the partial methods added and create an interface, you can then constrain the generic to use that interface and work off of those methods defined.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122