|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
What scope is best for defining Enum type?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 *** 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. Show quoteHide quote "Alex Feldman" <alex-feld***@hotmail.com> wrote in message news:OpRwhsfQFHA.3628@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 *** 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 I would expect members Normal & Charged, as the Enum name (ProductStatus) is | psNormal | psCharged | End Enum required to use the enum, ProductStatus.psNormal feels awkward, specifically the ps... Hope this helps Jay Show quoteHide quote "Alex Feldman" <alex-feld***@hotmail.com> wrote in message news:OpRwhsfQFHA.3628@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 *** On 2005-04-15, Alex Feldman <alex-feld***@hotmail.com> wrote:
Show quoteHide quote > Which of the following is better? Defining an enum type inside a class Did you mean to type "in the rest of the namespace"? As you've typed> 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 it, your statement isn't true. > The regular approach (defining in the namespace would) would be as such: I think there's three situations here.> > 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. 1. Only the product class uses the enum. In this case, nest the declaration and make it Private. Public Class Product Private Enum Status ... 2. Only the Product class stores the enum, but it's exposed through a property to other classes. In this case, I'd nest it and make it Public. Public Class Product Public Enum Status End Enum Private _status As Status Public ReadOnly Property Status As Status Get Return _status End Get End Property ... 3. Multiple classes use the enum or, and this is the tricky part, they might use it in the future. In this case don't nest the declaration in the class. Right now only Product might use status, but would it make sense for an invoice or some other group of Products to use the same Enum? It's often difficult to predict these things. > The framework engineers clearly seem to think that the namespace If other classes are actually declaring new instances of the type, I'd> 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. probably put it in the namespace rather than the class. In the above Product example, I don't really see why another class would ever need to declare a Product.Status variable. Of course, I also don't understand your statement about autocompletion here. AutoCompletion works fine with nested declarations for me.
An absence of IntelliSense in this situation
VB6 to VB.Net Conversion How to overload method of third party component? creating an array as property of a class Making trial version Question about Namespaces and the Object Browser. Limiting the directories somebody can browse to..... Passing parameters to Data Adapter Creating a custom log Hierarchal recordset |
|||||||||||||||||||||||