.NETGURU
Can enums be extened?
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcs' list.


Remas Wojciechowski
Greetings!

Can enums be extended? E.g., can I add a static method to a custom enum?

Thanks,
Remas
http://www.aspalliance.com/remas

Reply to this message...
 
    
Brian W. Spolarich

Enums are just fancy wrappers around an integer type. The System.Enum =
class is defined as abstract, so you can't instantiate it like an object =
or extend it (the compiler calls it a "special" class):

c:\inetpub\wwwroot\test\WebForm1.aspx.cs(59): 'test.myEnum' cannot =
inherit from special class 'System.Enum'

Since enum just provides some pretty syntactic dressing around integer =
types, why not create a class with integer properties? You can then =
extend it, define methods (static or otherwise), etc.

-bws

| -----Original Message-----
| From: Remas Wojciechowski [mailto:Click here to reveal e-mail address]
| Sent: Thursday, July 11, 2002 8:48 AM
| To: aspngcs
| Subject: [aspngcs] Can enums be extened?
|=20
|=20
| Greetings!
|=20
| Can enums be extended? E.g., can I add a static method to a=20
| custom enum?
|=20
| Thanks,
| Remas
| http://www.aspalliance.com/remas
|=20
|=20
| | [aspngcs] member Click here to reveal e-mail address =3D YOUR ID
| | http://www.asplists.com/asplists/aspngcs.asp =3D JOIN/QUIT
|=20

Reply to this message...
 
    
Remas Wojciechowski
Brain,

yes, a class with integer properties would be an option. I have a nagging
impression that enums had some advantages over classes but can't seem to
recall them. Except for: built-in enumerability, built-in string to int
(enum) and back conversions, probably slightly better performance. Oh, I've
just recalled one serious advantage--say you design a class with a property
that accepts only 4 different values. By creating an enum with 4 attributes
and defining the property to be of that type, you automatically ensure that
only valid values can be passed to it. With a class, however, the user of
this class could pass any integer value. In that respect, the enum is more
elegant.

Another option would be to create a class with a custom enum as property and
some methods "around" it but then - why use an enum at all :)

Anyways, thanks for the input!

Remas
http://www.aspalliance.com/remas

----- Original Message -----
From: "Brian W. Spolarich" <Click here to reveal e-mail address>
To: "aspngcs" <Click here to reveal e-mail address>
Sent: Thursday, July 11, 2002 4:00 PM
Subject: [aspngcs] RE: Can enums be extened?

Enums are just fancy wrappers around an integer type. The System.Enum
class is defined as abstract, so you can't instantiate it like an object or
extend it (the compiler calls it a "special" class):

c:\inetpub\wwwroot\test\WebForm1.aspx.cs(59): 'test.myEnum' cannot inherit
from special class 'System.Enum'

Since enum just provides some pretty syntactic dressing around integer
types, why not create a class with integer properties? You can then extend
it, define methods (static or otherwise), etc.

-bws

| -----Original Message-----
| From: Remas Wojciechowski [mailto:Click here to reveal e-mail address]
| Sent: Thursday, July 11, 2002 8:48 AM
| To: aspngcs
| Subject: [aspngcs] Can enums be extened?
|
|
| Greetings!
|
| Can enums be extended? E.g., can I add a static method to a
| custom enum?
|
| Thanks,
| Remas
| http://www.aspalliance.com/remas
|
|
| | [aspngcs] member Click here to reveal e-mail address = YOUR ID
| | http://www.asplists.com/asplists/aspngcs.asp = JOIN/QUIT
|

| [aspngcs] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcs.asp = JOIN/QUIT

Reply to this message...
 
    
Ryan Trudelle-Schwarz
Are you certain about that? Seems like you can set an enum value to any
integer as long as you cast the int to the enum type...

-> -----Original Message-----
-> From: Remas Wojciechowski [mailto:Click here to reveal e-mail address]
->
-> Brain,
->
-> I've
-> just recalled one serious advantage--say you design a class with a
-> property
-> that accepts only 4 different values. By creating an enum with 4
-> attributes
-> and defining the property to be of that type, you automatically
ensure
-> that
-> only valid values can be passed to it. With a class, however, the
user of
-> this class could pass any integer value. In that respect, the enum is
-> more
-> elegant.
->
->
-> Remas

---
[This E-mail scanned for viruses by Declude Virus]

Reply to this message...
 
    
Sills, Adam
You are correct. Here's an example:

enum MyEnum
{
    Value1 = 1,
    Value2 = 2,
    Value3 = 3,
    Value22 = 22
}
class Class1
{
    [STAThread]
    static void Main(string[] args)
    {
        WriteEnum((MyEnum) 1);
        WriteEnum((MyEnum) 22);
        WriteEnum((MyEnum) 9);
        Console.ReadLine();
    }
    static void WriteEnum(MyEnum e)
    {
        Console.WriteLine(e.ToString());
    }
}

This prints out:
Value1
Value22
9

However, if you change WriteEnum to this:

static void WriteEnum(MyEnum e)
{
    if(Enum.IsDefined(typeof(MyEnum), e))
    {
        Console.WriteLine(e.ToString());
    }
    else
    {
        Console.WriteLine("Error. Enum value is not defined");
    }
}

This prints out:
Value1
Value22
Error. Enum value is not defined

So with the enum you can easily tell if the value passed in was valid,
however with the class with public consts you'd have you write your own code
to determine if the value was valid or not.

Adam..

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]
Sent: Thursday, July 11, 2002 1:19 PM
To: aspngcs
Subject: [aspngcs] RE: Can enums be extened?

Are you certain about that? Seems like you can set an enum value to any
integer as long as you cast the int to the enum type...

Reply to this message...
 
    
Paul D. Murphy
Define a structure with the enumeration as a property. Put the static
method on the structure.=20

Paul

    Paul D. Murphy
    Click here to reveal e-mail address
    "Teamwork is a lot of people doing what I say."

-----Original Message-----
From: Remas Wojciechowski [mailto:Click here to reveal e-mail address]=20
Sent: Thursday, July 11, 2002 8:48 AM
To: aspngcs
Subject: [aspngcs] Can enums be extened?

Greetings!

Can enums be extended? E.g., can I add a static method to a custom enum?

Thanks,
Remas
http://www.aspalliance.com/remas

| [aspngcs] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/aspngcs.asp =3D JOIN/QUIT

Reply to this message...
 
 
System.Console
System.Enum




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification