Re: Why can't switch be used for objects
- From: "Bruce Wood" <brucewood@xxxxxxxxxx>
- Date: 21 Jun 2005 22:36:20 -0700
I'm a little confused by your post, since I don't see any enums
declared anywhere in it. However, if I understand the nature of your
problem in general, then I have a question:
Why is some outside agent (some method outside the class), sifting
through your objects in an attempt to decide which enum to return? Why
doesn't the class itself supply you with its appropriate enum. For
example:
enum ColorEnum
{
Unspecified,
Red,
Green,
Blue
}
Color r = new Color(255,0,0,ColorEnum.Red);
Color g = new Color(0,255,0,ColorEnum.Green);
Color b = new Color(0,0,255,ColorEnum.Blue);
Then you just say:
ColorEnum en = g.EquivalentEnum;
No switch or ifs needed. As a general rule, classes should be designed
so that the object knows a lot about itself. If you find yourself
writing complicated tests of any kind in client code then it's a big
hint that your class isn't beefy enough. It's also a hint at a future
maintenance headache, because your client code contains business logic
that "knows too much" about your objects; such code belongs within the
class itself.
.
- Follow-Ups:
- Re: Why can't switch be used for objects
- From: cody
- Re: Why can't switch be used for objects
- From: cody
- Re: Why can't switch be used for objects
- References:
- Why can't switch be used for objects
- From: cody
- Why can't switch be used for objects
- Prev by Date: Re: Strange Bug with the order of objects in a collection
- Next by Date: Re: Inheritance question
- Previous by thread: Why can't switch be used for objects
- Next by thread: Re: Why can't switch be used for objects
- Index(es):
Relevant Pages
|