Rendering list items differently based on object

  • Follow


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.
0
Reply Spondishy 4/5/2010 8:41:02 AM

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
0
Reply Peter 4/5/2010 9:33:43 AM


On Apr 5, 10:33=A0am, Peter Duniho <no.peted.s...@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.
0
Reply Spondishy 4/6/2010 9:48:24 AM

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
0
Reply Peter 4/6/2010 3:29:22 PM

3 Replies
205 Views

(page loaded in 0.039 seconds)

Similiar Articles:
















7/29/2012 9:38:44 AM


Reply: