473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1817
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Morgan Cheng | last post by:
Hi, In my program module, there are some Constants should be defined to be integer key value of std::map. In the module, methods of a few classes will return std::map containing value indexed by...
5
by: Rouben Rostamian | last post by:
I searched the C99 standard and clc's FAQ but was unable to find an answer to the following issue. I hope that someone here can illuminate me. Suppose I have: enum myenum { enuma, enumb,...
4
by: imme929 | last post by:
I got things working until I tried adding this enum to a structure... Public Enum Keyboard EnglishUS EnglishUK Spanish German Italian French
7
by: moondaddy | last post by:
I want to create a public enum that can be used throughout a project. I created an enum like this in a module: Public Enum ParentType Project = 0 Stage = 1 VIP = 2 Func = 3 Equipment = 4...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
9
by: Jacek Dziedzic | last post by:
Hi! I often find that my programs need to store information on "current mode of something" with two or at most several mutually exclusive "modes" to choose from, e.g. - datafile: is it in a)...
1
by: wenmang | last post by:
Hi, I encountered some legacy codes with multiple definitions for some symbols in term of enum(global naemspace pollution). How can I enforce the scope of an enum? e.g., enum MyEnum { OK };...
1
by: cedric.louyot | last post by:
Hi, I've written a schema that looks like : <xs:schema> <xs:complexType name="myType"> <xs:sequence> <xs:element name="e1" type="T1" maxOccurs="unbounded"/> <xs:element name="e2"...
5
by: =?GB2312?B?17/HvyBaaHVvLCBRaWFuZw==?= | last post by:
Hi, I would like to have someone comments on what's the best practice defining error codes in C. Here's what I think: solution A: using enum pros: type safe. better for debug (some debugger...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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 projectplanning, coding, testing,...
0
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...

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.