Home All Groups Group Topic Archive Search About

What scope is best for defining Enum type?

Author
15 Apr 2005 8:38 PM
Alex Feldman
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 ***

Author
15 Apr 2005 9:36 PM
Beth Massi [Architect MVP]
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 ***
Author
16 Apr 2005 2:48 AM
Jay B. Harlow [MVP - Outlook]
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

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 ***
Author
16 Apr 2005 4:28 PM
David
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
> 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

Did you mean to type "in the rest of the namespace"?  As you've typed
it, your statement isn't true.


> 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.

I think there's three situations here.

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
> 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.

If other classes are actually declaring new instances of the type, I'd
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.