Prev: Best TreeListView control for showing hierarchical structures (like XML)
Next: Get registry keys over wmi on a remote host?
From: Spondishy on 5 Apr 2010 04:41 This is a bit of a mix between asp.net and c#, but I thought I'd try here first. I have a number of objects (imagine products) in an IList that I want to render in the UI. They all inherit from the same base class. The individual classes have extra attributes, for example one might have weight, another color. I want a generic way to render these in the UI, without the need of a huge switch statement. Should each object know how to render itself for instance? Any implemented examples around the net would be good. Thanks.
From: Peter Duniho on 5 Apr 2010 05:33 Spondishy wrote: > This is a bit of a mix between asp.net and c#, but I thought I'd try > here first. > > I have a number of objects (imagine products) in an IList that I want > to render in the UI. They all inherit from the same base class. The > individual classes have extra attributes, for example one might have > weight, another color. > > I want a generic way to render these in the UI, without the need of a > huge switch statement. Should each object know how to render itself > for instance? > > Any implemented examples around the net would be good. Thanks. The question is a classic example of where you should use some form of polymorphism. You can either define an interface that each object implements, or the base class can include a virtual method for rendering that each object overrides. A Google search can turn up a wealth of information on both, as far as examples go. Pete
From: Spondishy on 6 Apr 2010 05:48 On Apr 5, 10:33 am, Peter Duniho <no.peted.s...(a)no.nwlink.spam.com> wrote: > Spondishy wrote: > > This is a bit of a mix between asp.net and c#, but I thought I'd try > > here first. > > > I have a number of objects (imagine products) in an IList that I want > > to render in the UI. They all inherit from the same base class. The > > individual classes have extra attributes, for example one might have > > weight, another color. > > > I want a generic way to render these in the UI, without the need of a > > huge switch statement. Should each object know how to render itself > > for instance? > > > Any implemented examples around the net would be good. Thanks. > > The question is a classic example of where you should use some form of > polymorphism. > > You can either define an interface that each object implements, or the > base class can include a virtual method for rendering that each object > overrides. > > A Google search can turn up a wealth of information on both, as far as > examples go. > > Pete Thanks Pete... I'm fine with polymorphism. Just to add some context. I have a list of objects that I want to display on an asp.net page and depending on the object, display differently. I've read some articles on having a factory create the user controls, but it look messy.
From: Peter Duniho on 6 Apr 2010 11:29
Spondishy wrote: > Thanks Pete... I'm fine with polymorphism. Just to add some context. I > have a list of objects that I want to display on an asp.net page and > depending on the object, display differently. I've read some articles > on having a factory create the user controls, but it look messy. I'm afraid there's not anything in your post that suggests a specific question to answer. If you're having trouble with some ASP.NET-specific aspect of the problem, I would agree that the ASP.NET newsgroup is a better place to ask the question. If you're having trouble with some C#-specific aspect of the problem, perhaps you could elaborate on what, specifically, is the question you have? Pete |