471,596 Members | 804 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,596 software developers and data experts.

What scope is best for defining Enum type?

Which of the following is better? Defining an enum type inside a class
as a nested type, or in the the namespace?
An example of nested type enumerated type would be:

public Class Product
Public Enum Status
psNormal
psCharged
End Enum

.....'other stuff in class
'Use the enumerated type within the class like this:
Dim enuPS as Status
End Class

So in the rest of the class I would have to declare a new instance of
the enumerated type as such:
Dim enuPS as Product.Status

The regular approach (defining in the namespace would) would be as such:

public class Product
.....
end class

Public Enum ProductStatus
psNormal
psCharged
End Enum

Notice that the name of the enumerator would change depending depending
whether it's nested to the object or not.

The framework engineers clearly seem to think that the namespace
definition of enumerators is better, but I still care about the
community opinion. I think there are pros and cons to both approaches.
The biggest advantage that i see of nesting the enum definition is that
it's tightly coupled with a class that it makes most sense with. On the
con side you have to use the . which makes it look like a member of a
class instead of a type definition, and the autocompletion in the IDE
won't work when declaring new instances of the type.

So what does everyone think about that? Which way is better?

Thanks.

Alex

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #1
2 1685
As a rule of thumb, I usually only declare Enums inside of classes when they
are only relevant to the class itself (and make them Private), otherwise I
place them in the appropriate Namespace.

"Alex Feldman" <al**********@hotmail.com> wrote in message
news:Op**************@TK2MSFTNGP12.phx.gbl...
Which of the following is better? Defining an enum type inside a class
as a nested type, or in the the namespace?
An example of nested type enumerated type would be:

public Class Product
Public Enum Status
psNormal
psCharged
End Enum

.....'other stuff in class
'Use the enumerated type within the class like this:
Dim enuPS as Status
End Class

So in the rest of the class I would have to declare a new instance of
the enumerated type as such:
Dim enuPS as Product.Status

The regular approach (defining in the namespace would) would be as such:

public class Product
.....
end class

Public Enum ProductStatus
psNormal
psCharged
End Enum

Notice that the name of the enumerator would change depending depending
whether it's nested to the object or not.

The framework engineers clearly seem to think that the namespace
definition of enumerators is better, but I still care about the
community opinion. I think there are pros and cons to both approaches.
The biggest advantage that i see of nesting the enum definition is that
it's tightly coupled with a class that it makes most sense with. On the
con side you have to use the . which makes it look like a member of a
class instead of a type definition, and the autocompletion in the IDE
won't work when declaring new instances of the type.

So what does everyone think about that? Which way is better?

Thanks.

Alex

*** Sent via Developersdex http://www.developersdex.com ***

Nov 21 '05 #2
Alex,
As Beth suggests I will nest an Enum (or other type) inside a Class when the
nested type (the Enum) is an implementation detail specific to that Class.
Also as Beth suggests this normally means making the nested type private.

FWIW:
| Public Enum ProductStatus
| psNormal
| psCharged
| End Enum

I would expect members Normal & Charged, as the Enum name (ProductStatus) is
required to use the enum, ProductStatus.psNormal feels awkward, specifically
the ps...

Hope this helps
Jay

"Alex Feldman" <al**********@hotmail.com> wrote in message
news:Op**************@TK2MSFTNGP12.phx.gbl...
| Which of the following is better? Defining an enum type inside a class
| as a nested type, or in the the namespace?
| An example of nested type enumerated type would be:
|
| public Class Product
| Public Enum Status
| psNormal
| psCharged
| End Enum
|
| .....'other stuff in class
| 'Use the enumerated type within the class like this:
| Dim enuPS as Status
| End Class
|
| So in the rest of the class I would have to declare a new instance of
| the enumerated type as such:
| Dim enuPS as Product.Status
|
| The regular approach (defining in the namespace would) would be as such:
|
| public class Product
| .....
| end class
|
| Public Enum ProductStatus
| psNormal
| psCharged
| End Enum
|
| Notice that the name of the enumerator would change depending depending
| whether it's nested to the object or not.
|
| The framework engineers clearly seem to think that the namespace
| definition of enumerators is better, but I still care about the
| community opinion. I think there are pros and cons to both approaches.
| The biggest advantage that i see of nesting the enum definition is that
| it's tightly coupled with a class that it makes most sense with. On the
| con side you have to use the . which makes it look like a member of a
| class instead of a type definition, and the autocompletion in the IDE
| won't work when declaring new instances of the type.
|
| So what does everyone think about that? Which way is better?
|
| Thanks.
|
| Alex
|
| *** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Morgan Cheng | last post: by
5 posts views Thread by Rouben Rostamian | last post: by
7 posts views Thread by moondaddy | last post: by
669 posts views Thread by Xah Lee | last post: by
1 post views Thread by wenmang | last post: by
1 post views Thread by cedric.louyot | last post: by
5 posts views Thread by =?GB2312?B?17/HvyBaaHVvLCBRaWFuZw==?= | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.