473,473 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why can't switch be used for objects

Why can't switch used for objects, at least for static readonly fields it
wold be nice.
The Jit could certainly optimize this case because the addresses of static
readonly variables are known at jit time.

The thing is that is often used my own enum classes because I usually want
to assign a userfriendly (and localizable) name to an enum member which is
automatically displayed if I add a enum member to a combobox for example.
Additionally I have helper methods for returning me a list of all or a
specific subsets of the member of the enums:

public class Color
{
public static reaonly Color Red = new Color(255,0,0);
public static reaonly Color Green = new Color(0,255,0);
// and so on

private Color(){}

public Color[] GetColors() ..
public Color[] GetGreenLikeColors() ..
public Color[] GetDarkColors() ..
public Color[] GetSystemColors() ..

Color GetByName(string name){}
Color GetByID(string name){}
}

The getXX methods work mainly using hashtables and the great thing is that
if using generics I could derive all enums from some base enum class without
having to add new getXX methods which return Color[] instead of object[].

The stupid thing is if I change some enum into my enum class I always have
to rewrite all switches into huge ugly if's.

I see no technical reason why switch shouldn' be allowed for objects.
Certainly is won't be as fast as with constant int values but anyway they
allowed strings in switch statements.

In a modern language like C# the purpose of a switch statement shouldn't be
to make a program faster but more readable.

Am I just plain stupid or is there something huge I overlooked?
Nov 17 '05 #1
9 1382
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.

Nov 17 '05 #2

"Bruce Wood" <br*******@canada.com> schrieb im Newsbeitrag
news:11**********************@g43g2000cwa.googlegr oups.com...
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:
The Color class IS my Enum. Is it not a enum like a c# enum but it works
like an enum though there are differences.
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.


And how do you think to convert a ColorEnum to a Color object?
Nov 17 '05 #3
> The Color class IS my Enum. Is it not a enum like a c# enum but it works like an enum though there are differences.

OK... then I need some code to illustrate those "big ugly 'ifs'"
because I don't understand your original problem.
And how do you think to convert a ColorEnum to a Color object?


Well, if you were doing things that way, a static method in Color would
do the trick:

public static Color ColorForEnum(ColorEnum en)
{
}

inside there you could use a switch or set up collections by asking
each Color what enum it belonged to.

Anyway, you're not doing things that way. So, post me some code so my
poor brain can get a handle on your problem. :-)

Nov 17 '05 #4
On Wed, 22 Jun 2005 01:56:34 +0200, "cody" <de********@gmx.de> wrote:

<snip>

Am I just plain stupid or is there something huge I overlooked?


The answer is yes to both questions.

Oz
--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 17 '05 #5
Oz,

You're a regular in the Linux forums and you lost your way, didn't you?
:-)

Nov 17 '05 #6
On 22 Jun 2005 16:40:12 -0700, "Bruce Wood" <br*******@canada.com>
wrote:
Oz,

You're a regular in the Linux forums and you lost your way, didn't you?
:-)


The answer is no to both questions.

Oz
--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 17 '05 #7
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
normally would do with real enums.

The original question of me was: Is there a real reason why c# does not
support switch with objects?
"Bruce Wood" <br*******@canada.com> schrieb im Newsbeitrag
news:11**********************@g43g2000cwa.googlegr oups.com...
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.

Nov 17 '05 #8
1. Because it would be grossly slow.
2. Because the semantics of comparison are unclear (do you use equality
by reference, or use the Equals method, or do you allow IComparers,
or... ?)
3. Because if you need this then there is probably some other (cleaner)
way to do what you want to do... which is why I'm wondering exactly
where you need this switch statement and for what, in order to propose
an alternate design. :-)

Nov 17 '05 #9
Bruce Wood <br*******@canada.com> wrote:
1. Because it would be grossly slow.
2. Because the semantics of comparison are unclear (do you use equality
by reference, or use the Equals method, or do you allow IComparers,
or... ?)
3. Because if you need this then there is probably some other (cleaner)
way to do what you want to do... which is why I'm wondering exactly
where you need this switch statement and for what, in order to propose
an alternate design. :-)


Actually, it's not entirely unreasonable, nor does it need to be slow -
with some extra support.

I've recently been playing around with Java 1.5's enums, which allow
switching and all kinds of fun. They're much more powerful than the
..NET enums - although there are certain bits of behaviour from .NET's
"simple" enums which Java makes slightly harder.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Dave | last post by:
Hello all, Consider the case of an if with many else if clauses vs. a switch with many cases. In thinking about how such control constructs would likely be implemented, it would seem that...
10
by: Chris Martin | last post by:
Background: Our main menu is built using a simple unordered list containing other ULs. There is a table on a page that holds the subnav. Because of certain circumstances, I need to build the subnav...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
3
by: Poewood | last post by:
Okay here are four classes for a pocket pc program: Input, fpositional, ComboBoxArray and TextBoxArray. The "input" class is the form. I use the fpositional class to handle most of the functions...
10
by: Evie | last post by:
I understand that when a switch statement is used without breaks, the code continues executing even after a matching case is found. Why, though, are subsequent cases not evaluated? I wrote a...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
7
by: cytec123187 | last post by:
Hello, I am working on an Adobe Acrobat file that uses javascript for calculations. I am trying to create a field that uses two other fields to determine a number value. I think this requires...
15
by: Zhang Weiwu | last post by:
http://www.w3.org/MarkUp/2004/xhtml-faq provided a trick to serve xhtml webpage to IE as application/xml I used that trick and now every one of my xhtml webpage have following first 4 starting...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.