-
Oct 2nd, 2012, 08:59 PM
#1
Thread Starter
Member
[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?
-
Oct 2nd, 2012, 09:33 PM
#2
Re: How to reference common enum in several classes
Originally Posted by franceint
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.
-
Oct 2nd, 2012, 10:30 PM
#3
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.
-
Oct 2nd, 2012, 10:53 PM
#4
Re: How to reference common enum in several classes
Originally Posted by 4x2y
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|