472,794 Members | 2,066 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 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 1755
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.