By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,411 Members | 1,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,411 IT Pros & Developers. It's quick & easy.

What scope is best for defining Enum type?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.