Results 1 to 4 of 4

Thread: [RESOLVED] How to reference common enum in several classes

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    43

    Resolved [RESOLVED] How to reference common enum in several classes

    I have several classes each of which I want to have as a property the same enum, for example:

    Public Enum Status As Integer
    Unchanged
    Changed
    Created
    Deleted
    End Enum

    I put the above in a module. but when I try to reference Status in a class, like

    property Mystatus() as Status

    I get the error message "Error 1 'Mystatus' cannot expose type 'stringsubs.RowStatus' outside the project through class 'invoice'. C:\Users\franceint\Documents\Visual Studio 2010\Projects\A3Main\invoice.vb 18 35 A3Main

    I can put the enum in each class but that doesn't seem like good programming practice as maintenance becomes an issue if the enum has to change. So how do I define this property in my classes?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: How to reference common enum in several classes

    Quote Originally Posted by franceint View Post
    I put the above in a module.
    That is your mistake. An enumeration is a type. A module is a type. Do not declare one type inside another type unless you have a very good reason for doing so. Would you declare a class inside a module? No, you wouldn't. You would declare a class in its own code file. An enumeration is a type just like a class, module or structure and they should all be declared in there own code files. The exception I tend to make to that is, because enumerations are generally short and always simple, I declare them all in the same code file, named Enumerations.vb. Never inside another type though, unless they are intended to be used only within that type, in which case they'd be declared Private.

    Also, while it's a small thing, there's actually no point specifying 'As Integer' because enumerations use Integer as a backing type by default. There's really no point ever changing that unless the values specifically correspond to some other data that requires another type, e.g. they correspond to IDs stored in a database reference table and you always use 64-bit integers for your IDs.

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to reference common enum in several classes

    Declare it in one class then you can refer it in all other classes with full name space path.



  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: How to reference common enum in several classes

    Quote Originally Posted by 4x2y View Post
    Declare it in one class then you can refer it in all other classes with full name space path.
    No! Bad, bad, bad! That will work as long as you qualify the name but then so would using a module. Unless the enumeration logically belongs to one of the classes it should NOT be declared inside any of them. As I said, an enumeration is a first class type in .NET so treat it like one. DO NOT declare one type inside another unless you have a specific reason for doing so, which usually means that the nested type will only ever be used by, or at least in the presence of, its containing type.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width