Re: Why can't switch be used for objects



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.

.



Relevant Pages

  • Re: Why cant switch be used for objects
    ... My one and only problem is that I do *not* want to declare an extra enum ... because my class already has enum semantics and I want to use it in a way I ... > hint that your class isn't beefy enough. ... because your client code contains business logic ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Interface and enums
    ... I would implement the interface and fill the enum with Strawberry and ... Your client code could choose one of your jams. ... If you declare the empty interface then the only thing ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why cant switch be used for objects
    ... The Color class IS my Enum. ... > enum ColorEnum ... > hint that your class isn't beefy enough. ... because your client code contains business logic ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: placement new and alignment
    ... After some reasoning, and before reading your hint, I came out with the ... struct TypeGCD ... value1 = offsetof(this_t, second), ...
    (microsoft.public.vc.language)